// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.idea.svn.diff;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.api.Url;
import org.jetbrains.idea.svn.branchConfig.SelectBranchPopup;
import org.jetbrains.idea.svn.branchConfig.SvnBranchConfigurationNew;

/**
 * @author yole
 */
public class CompareWithBranchAction extends AnAction implements DumbAware {

  public void actionPerformed(AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);
    assert project != null;
    final VirtualFile virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE);

    SelectBranchPopup
      .show(project, virtualFile, new MyBranchSelectedCallback(virtualFile), SvnBundle.message("compare.with.branch.popup.title"));
  }

  @Override
  public void update(final AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);
    VirtualFile virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
    e.getPresentation().setEnabled(isEnabled(project, virtualFile));
  }

  private static boolean isEnabled(final Project project, final VirtualFile virtualFile) {
    if (project == null || virtualFile == null) {
      return false;
    }
    final FileStatus fileStatus = FileStatusManager.getInstance(project).getStatus(virtualFile);
    if (fileStatus == FileStatus.UNKNOWN || fileStatus == FileStatus.ADDED || fileStatus == FileStatus.IGNORED) {
      return false;
    }
    return true;
  }

  private static class MyBranchSelectedCallback implements SelectBranchPopup.BranchSelectedCallback {

    @NotNull private final VirtualFile myVirtualFile;

    public MyBranchSelectedCallback(@NotNull VirtualFile virtualFile) {
      myVirtualFile = virtualFile;
    }

    public void branchSelected(Project project, SvnBranchConfigurationNew configuration, @NotNull Url url, long revision) {
      ElementWithBranchComparer comparer =
        myVirtualFile.isDirectory()
        ? new DirectoryWithBranchComparer(project, myVirtualFile, url, revision)
        : new FileWithBranchComparer(project, myVirtualFile, url, revision);

      comparer.run();
    }
  }
}

@@ -177,59 +177,8 @@
 f;%0A%0A
-import com.intellij.openapi.actionSystem.AnAction;%0A
 impo
@@ -331,16 +331,22 @@
 umbAware
+Action
 ;%0Aimport
@@ -570,88 +570,8 @@
 ll;%0A
-import org.jetbrains.idea.svn.SvnBundle;%0Aimport org.jetbrains.idea.svn.api.Url;%0A
 impo
@@ -628,23 +628,31 @@
 hPopup;%0A
+%0A
 import 
+static 
 org.jetb
@@ -670,72 +670,27 @@
 svn.
-branchConfig.SvnBranchConfigurationNew;%0A%0A/**%0A * @author yole%0A */
+SvnBundle.message;%0A
 %0Apub
@@ -735,28 +735,8 @@
 nds 
-AnAction implements 
 Dumb
@@ -740,16 +740,22 @@
 umbAware
+Action
  %7B%0A%0A  pu
@@ -780,16 +780,25 @@
 rformed(
+@NotNull 
 AnAction
@@ -827,32 +827,40 @@
  project = e.get
+Required
 Data(CommonDataK
@@ -881,42 +881,8 @@
     
-assert project != null;%0A    final 
 Virt
@@ -885,32 +885,25 @@
 VirtualFile 
-virtualF
+f
 ile = e.getD
@@ -893,32 +893,40 @@
 ile file = e.get
+Required
 Data(CommonDataK
@@ -966,23 +966,16 @@
 nchPopup
-%0A      
 .show(pr
@@ -985,74 +985,355 @@
 ct, 
-virtualFile, new MyBranchSelectedCallback(virtualF
+file, (p, configuration, url, revision) -%3E %7B%0A      ElementWithBranchComparer comparer = file.isDirectory()%0A                                           ? new DirectoryWithBranchComparer(project, file, url, revision)%0A                                           : new FileWithBranchComparer(project, f
 ile
-)
 , 
-SvnBundle.
+url, revision);%0A%0A      comparer.run();%0A    %7D, 
 mess
@@ -1411,20 +1411,23 @@
  update(
-fina
+@NotNul
 l AnActi
@@ -1512,24 +1512,17 @@
 ualFile 
-virtualF
+f
 ile = e.
@@ -1559,16 +1559,17 @@
 _FILE);%0A
+%0A
     e.ge
@@ -1599,248 +1599,53 @@
 led(
-isEnabled(project, virtualFile));%0A  %7D%0A%0A  private static boolean isEnabled(final Project project, final VirtualFile virtualFile) %7B%0A    if (project == null %7C%7C virtualFile == null) %7B%0A      return false;%0A    %7D%0A    final FileStatus fileStatus = 
+project != null && file != null && isEnabled(
 File
@@ -1693,101 +1693,58 @@
 tus(
-virtualF
+f
 ile)
+))
 ;%0A  
-  if (fileStatus == FileStatus.UNKNOWN %7C%7C fileStatus == FileStatus.ADDED %7C%7C f
+%7D%0A%0A  private static boolean isEnabled(F
 ileS
@@ -1753,12 +1753,9 @@
 tus 
-== F
+f
 ileS
@@ -1763,24 +1763,16 @@
 atus
-.IGNORED
 ) %7B%0A    
   re
@@ -1771,18 +1771,16 @@
     
- 
- 
 return f
 alse
@@ -1779,703 +1779,110 @@
 rn f
-alse;%0A    %7D%0A    return true;%0A  %7D%0A%0A  private static class MyBranchSelectedCallback implements SelectBranchPopup.BranchSelectedCallback %7B%0A%0A    @NotNull private final VirtualFile myVirtualFile;%0A%0A    public MyBranchSelectedCallback(@NotNull VirtualFile virtualFile) %7B%0A      myVirtualFile = virtualFile;%0A    %7D%0A%0A    public void branchSelected(Project project, SvnBranchConfigurationNew configuration, @NotNull Url url, long revision) %7B%0A      ElementWithBranchComparer comparer =%0A        myVirtualFile.isDirectory()%0A        ? new DirectoryWithBranchComparer(project, myVirtualFile, url, revision)%0A        : new FileWithBranchComparer(project, myVirtualFile, url, revision);%0A%0A      comparer.run();%0A    %7D
+ileStatus != FileStatus.UNKNOWN && fileStatus != FileStatus.ADDED && fileStatus != FileStatus.IGNORED;
 %0A  %7D

Refactor "CompareWithBranchAction"