package com.jetbrains.edu.learning.editor;


import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.event.EditorFactoryEvent;
import com.intellij.openapi.editor.event.EditorFactoryListener;
import com.intellij.openapi.editor.event.EditorMouseAdapter;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.problems.WolfTheProblemSolver;
import com.jetbrains.edu.learning.core.EduDocumentListener;
import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.navigation.StudyNavigator;
import com.jetbrains.edu.learning.ui.StudyToolWindowFactory;
import org.jetbrains.annotations.NotNull;

import java.awt.*;


public class StudyEditorFactoryListener implements EditorFactoryListener {

  private static class WindowSelectionListener extends EditorMouseAdapter {
    private final TaskFile myTaskFile;

    WindowSelectionListener(@NotNull final TaskFile taskFile) {
      myTaskFile = taskFile;
    }

    @Override
    public void mouseClicked(EditorMouseEvent e) {
      final Editor editor = e.getEditor();
      final Point point = e.getMouseEvent().getPoint();
      final LogicalPosition pos = editor.xyToLogicalPosition(point);
      final AnswerPlaceholder answerPlaceholder = myTaskFile.getAnswerPlaceholder(editor.getDocument(), pos);
      if (answerPlaceholder == null || answerPlaceholder.getSelected()) {
        return;
      }
      int startOffset = answerPlaceholder.getRealStartOffset(editor.getDocument());
      editor.getSelectionModel().setSelection(startOffset, startOffset + answerPlaceholder.getLength());
      answerPlaceholder.setSelected(true);
    }
  }

  @Override
  public void editorCreated(@NotNull final EditorFactoryEvent event) {
    final Editor editor = event.getEditor();

    final Project project = editor.getProject();
    if (project == null) {
      return;
    }
    ApplicationManager.getApplication().invokeLater(
      new Runnable() {
        @Override
        public void run() {
          ApplicationManager.getApplication().runWriteAction(new Runnable() {
            @Override
            public void run() {
              final Document document = editor.getDocument();
              final VirtualFile openedFile = FileDocumentManager.getInstance().getFile(document);
              if (openedFile != null) {
                final TaskFile taskFile = StudyUtils.getTaskFile(project, openedFile);
                if (taskFile != null) {
                  WolfTheProblemSolver.getInstance(project).clearProblems(openedFile);
                  final ToolWindow studyToolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
                  if (studyToolWindow != null) {
                    StudyUtils.updateToolWindows(project);
                    studyToolWindow.show(null);
                  }
                  if (!taskFile.getAnswerPlaceholders().isEmpty()) {
                    StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
                    StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile));
                    StudyUtils.drawAllWindows(editor, taskFile);
                    editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
                  }
                }
              }
            }
          });
        }
      }
    );
  }

  @Override
  public void editorReleased(@NotNull EditorFactoryEvent event) {
    final Editor editor = event.getEditor();
    final Document document = editor.getDocument();
    StudyEditor.removeListener(document);
    editor.getMarkupModel().removeAllHighlighters();
    editor.getSelectionModel().removeSelection();
  }
}

@@ -42,68 +42,8 @@
 ;%0A%0A%0A
-import com.intellij.openapi.application.ApplicationManager;%0A
 impo
@@ -2236,17 +2236,16 @@
 itor();%0A
-%0A
     fina
@@ -2336,272 +2336,9 @@
   %7D%0A
-    ApplicationManager.getApplication().invokeLater(%0A      new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A          ApplicationManager.getApplication().runWriteAction(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A          
+%0A
     
@@ -2385,26 +2385,16 @@
 ment();%0A
-          
     fina
@@ -2469,34 +2469,24 @@
 (document);%0A
-          
     if (open
@@ -2499,34 +2499,24 @@
  != null) %7B%0A
-          
       final 
@@ -2576,34 +2576,24 @@
 penedFile);%0A
-          
       if (ta
@@ -2618,26 +2618,16 @@
         
-       
-   
 WolfTheP
@@ -2683,34 +2683,24 @@
 penedFile);%0A
-          
         fina
@@ -2826,26 +2826,16 @@
 INDOW);%0A
-          
         
@@ -2871,34 +2871,24 @@
 %7B%0A          
-        
-  
 StudyUtils.u
@@ -2914,26 +2914,16 @@
 oject);%0A
-          
         
@@ -2960,38 +2960,18 @@
         
-    
-      
 %7D%0A
-          
         
@@ -3021,26 +3021,16 @@
 ty()) %7B%0A
-          
         
@@ -3104,34 +3104,24 @@
 ;%0A          
-     
-     
 StudyEditor.
@@ -3182,34 +3182,24 @@
 taskFile));%0A
-          
           St
@@ -3241,26 +3241,16 @@
 kFile);%0A
-          
         
@@ -3325,90 +3325,8 @@
 ));%0A
-                  %7D%0A                %7D%0A              %7D%0A            %7D%0A          %7D);%0A
     
@@ -3343,18 +3343,17 @@
   %7D%0A    
-);
+%7D
 %0A  %7D%0A%0A  

remove outdated write action (EDU-591)