package zielu.gittoolbox.ui.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import zielu.gittoolbox.blame.Blame;
import zielu.gittoolbox.blame.BlameService;
import zielu.gittoolbox.cache.VirtualFileRepoCache;
import zielu.gittoolbox.config.GitToolBoxConfig2;
import zielu.gittoolbox.ui.blame.BlameUi;

public class BlameDetailsAction extends AnAction {
  @Override
  public void update(@NotNull AnActionEvent e) {
    super.update(e);
    e.getPresentation().setVisible(isVisible(e));
  }

  private boolean isVisible(@NotNull AnActionEvent e) {
    if (!GitToolBoxConfig2.getInstance().showBlame) {
      return false;
    }
    Project project = e.getData(CommonDataKeys.PROJECT);
    if (project == null) {
      return false;
    }
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (editor != null) {
      VirtualFile editorFile = FileDocumentManager.getInstance().getFile(editor.getDocument());
      if (editorFile != null) {
        return VirtualFileRepoCache.getInstance(project).isUnderGitRoot(editorFile);
      }
    }
    return false;
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (project != null && editor != null) {
      VirtualFile editorFile = FileDocumentManager.getInstance().getFile(editor.getDocument());
      if (editorFile != null) {
        BlameService blameService = BlameService.getInstance(project);
        Blame blame = blameService.getCurrentLineBlame(editor, editorFile);
        if (blame != null) {
          String detailsText = blame.getDetailedText();
          if (detailsText != null) {
            BlameUi.showBlamePopup(editor, blame);
          }
        }
      }
    }
  }
}

@@ -195,16 +195,61 @@
 taKeys;%0A
+import com.intellij.openapi.editor.Document;%0A
 import c
@@ -1207,32 +1207,137 @@
 itor != null) %7B%0A
+      Document document = editor.getDocument();%0A      if (!BlameUi.isDocumentInBulkUpdate(document)) %7B%0A  
       VirtualFil
@@ -1393,39 +1393,29 @@
 getFile(
-editor.getD
+d
 ocument
-()
 );%0A
+  
       if
@@ -1430,32 +1430,34 @@
 File != null) %7B%0A
+  
         return V
@@ -1521,24 +1521,34 @@
 ditorFile);%0A
+        %7D%0A
       %7D%0A    

Handle document bulk updates (#118)