package com.intellij.localvcs.integration.revert;

import com.intellij.localvcs.core.ILocalVcs;
import com.intellij.localvcs.core.changes.Change;
import com.intellij.localvcs.core.changes.ChangeSet;
import com.intellij.localvcs.core.changes.RenameChange;
import com.intellij.localvcs.core.tree.Entry;
import com.intellij.localvcs.integration.IdeaGateway;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class ChangeReverter extends Reverter {
  private ILocalVcs myVcs;
  private IdeaGateway myGateway;
  private Change myChange;

  public ChangeReverter(ILocalVcs vcs, IdeaGateway gw, Change c) {

    myVcs = vcs;
    myGateway = gw;
    myChange = c;
  }

  @Override
  public List<String> checkCanRevert() throws IOException {
    final boolean[] result = new boolean[]{true};

    myVcs.accept(new ChangeRevertionVisitor(myVcs, myGateway) {
      @Override
      public void visit(ChangeSet c) throws StopVisitingException {
        if (!myVcs.isBefore(myChange, c, true)) stop();
      }

      @Override
      public void visit(RenameChange c) throws IOException, StopVisitingException {
        if (shouldProcess(c)) {
          Entry e = getAffectedEntry(c);
          if (e.getParent().findChild(c.getOldName()) != null) {
            result[0] = false;
            stop();
          }
        }
        super.visit(c);
      }

      protected boolean shouldProcess(Change c) {
        return myVcs.isInTheChain(myChange, c);
      }
    });

    return Collections.singletonList("some files already exist");
  }

  // todo refactor myChangeList.isInTheChain and test it

  @Override
  public void revert() throws IOException {
    myVcs.accept(new ChangeRevertionVisitor(myVcs, myGateway) {
      @Override
      public void visit(ChangeSet c) throws StopVisitingException {
        if (!myVcs.isBefore(myChange, c, true)) stop();
      }

      protected boolean shouldProcess(Change c) {
        return myVcs.isInTheChain(myChange, c);
      }
    });
  }
}

@@ -225,79 +225,30 @@
 vcs.
-core.changes.RenameChange;%0Aimport com.intellij.localvcs.core.tree.Entry
+integration.FormatUtil
 ;%0Aim
@@ -460,41 +460,8 @@
 cs;%0A
-  private IdeaGateway myGateway;%0A
   pr
@@ -551,16 +551,30 @@
 ge c) %7B%0A
+    super(gw);
 %0A    myV
@@ -587,28 +587,8 @@
 cs;%0A
-    myGateway = gw;%0A
     
@@ -680,16 +680,20 @@
 ion %7B%0A  
+  //
   final 
@@ -732,19 +732,29 @@
 %7Btrue%7D;%0A
-%0A
   
+  //%0A    //
   myVcs.
@@ -801,32 +801,36 @@
  myGateway) %7B%0A  
+  //
     @Override%0A  
@@ -822,35 +822,39 @@
    @Override%0A   
+ //
    
+ 
 public void visi
@@ -893,32 +893,36 @@
 ngException %7B%0A  
+  //
       if (!myVcs
@@ -959,31 +959,45 @@
 stop();%0A    
-  %7D%0A%0A  
+//    %7D%0A    //%0A    //
     @Overrid
@@ -992,32 +992,36 @@
     @Override%0A  
+  //
     public void 
@@ -1085,29 +1085,33 @@
 eption %7B%0A   
+ //
      
+ 
 if (shouldPr
@@ -1120,24 +1120,28 @@
 ess(c)) %7B%0A  
+  //
         Entr
@@ -1165,24 +1165,28 @@
 Entry(c);%0A  
+  //
         if (
@@ -1234,24 +1234,28 @@
 = null) %7B%0A  
+  //
           re
@@ -1269,24 +1269,28 @@
  = false;%0A  
+  //
           st
@@ -1299,26 +1299,34 @@
 ();%0A    
+//
       
+  
 %7D%0A  
+  //
       %7D%0A
@@ -1327,16 +1327,20 @@
     %7D%0A  
+  //
       su
@@ -1352,32 +1352,46 @@
 isit(c);%0A   
+ //
    
+ 
 %7D%0A
-%0A
   
+  //%0A    //
     protecte
@@ -1422,36 +1422,40 @@
 Change c) %7B%0A    
+//
     
+  
 return myVcs.isI
@@ -1472,32 +1472,36 @@
 myChange, c);%0A  
+  //
     %7D%0A    %7D);%0A%0A 
@@ -1498,15 +1498,29 @@
     
-%7D);%0A%0A  
+//  %7D);%0A    //%0A    //
   re
@@ -1579,16 +1579,52 @@
 xist%22);%0A
+    return Collections.emptyList();%0A
   %7D%0A%0A  /
@@ -1697,20 +1697,288 @@
 %0A  p
-ublic void r
+rotected String formatCommandName() %7B%0A    String name = myChange.getName();%0A    if (name != null) return %22Revert '%22 + name + %22'%22;%0A%0A    String date = FormatUtil.formatTimestamp(myChange.getTimestamp());%0A    return %22Revert change made %22 + date;%0A  %7D%0A%0A  @Override%0A  protected void doR
 ever
@@ -2215,16 +2215,32 @@
      %7D%0A%0A
+      @Override%0A
       pr

test fix