package com.jetbrains.edu.learning.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.jetbrains.edu.learning.StudyTaskManager;
import com.jetbrains.edu.learning.core.EduNames;
import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
import com.jetbrains.edu.learning.courseFormat.Course;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
import com.jetbrains.edu.learning.StudyState;
import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.editor.StudyEditor;

public class StudyFillPlaceholdersAction extends AnAction {
  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    if (project != null) {
      StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
      StudyState studyState = new StudyState(studyEditor);
      if (!studyState.isValid()) {
        return;
      }
      TaskFile taskFile = studyState.getTaskFile();
      final Document document = studyState.getEditor().getDocument();
      final TaskFile realTaskFile = taskFile;
      CommandProcessor.getInstance().runUndoTransparentAction(() -> ApplicationManager.getApplication().runWriteAction(() -> {
        for (AnswerPlaceholder placeholder : realTaskFile.getActivePlaceholders()) {
          String answer = placeholder.getPossibleAnswer();
          if (answer == null) {
            continue;
          }
          int offset = placeholder.getOffset();
          document.deleteString(offset, offset + placeholder.getRealLength());
          document.insertString(offset, answer);
        }
      }));
    }
  }

  @Override
  public void update(AnActionEvent e) {
    StudyUtils.updateAction(e);
    final Project project = e.getProject();
    if (project != null) {

      Course course = StudyTaskManager.getInstance(project).getCourse();
      Presentation presentation = e.getPresentation();
      if (course != null && !EduNames.STUDY.equals(course.getCourseMode())) {
        presentation.setEnabled(false);
        presentation.setVisible(true);
        return;
      }
      StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
      StudyState studyState = new StudyState(studyEditor);
      if (!studyState.isValid()) {
        presentation.setEnabledAndVisible(false);
        return;
      }
      TaskFile taskFile = studyState.getTaskFile();
      if (taskFile.getActivePlaceholders().isEmpty()) {
        presentation.setEnabledAndVisible(false);
      }
    }
  }
}
@@ -204,122 +204,8 @@
 on;%0A
-import com.intellij.openapi.application.ApplicationManager;%0Aimport com.intellij.openapi.command.CommandProcessor;%0A
 impo
@@ -290,16 +290,62 @@
 roject;%0A
+import com.jetbrains.edu.learning.StudyState;%0A
 import c
@@ -384,24 +384,70 @@
 askManager;%0A
+import com.jetbrains.edu.learning.StudyUtils;%0A
 import com.j
@@ -483,16 +483,65 @@
 uNames;%0A
+import com.jetbrains.edu.learning.core.EduUtils;%0A
 import c
@@ -714,100 +714,8 @@
 le;%0A
-import com.jetbrains.edu.learning.StudyState;%0Aimport com.jetbrains.edu.learning.StudyUtils;%0A
 impo
@@ -1277,183 +1277,8 @@
 ();%0A
-      final TaskFile realTaskFile = taskFile;%0A      CommandProcessor.getInstance().runUndoTransparentAction(() -%3E ApplicationManager.getApplication().runWriteAction(() -%3E %7B%0A  
     
@@ -1320,13 +1320,9 @@
 r : 
-realT
+t
 askF
@@ -1348,26 +1348,24 @@
 olders()) %7B%0A
-  
         Stri
@@ -1413,26 +1413,24 @@
 ();%0A        
- 
- 
 if (answer =
@@ -1449,18 +1449,16 @@
         
-  
 continue
@@ -1459,18 +1459,16 @@
 ntinue;%0A
-  
         
@@ -1481,165 +1481,101 @@
     
-  int offset = placeholder.getOffset();%0A          document.deleteString(offset, offset + placeholder.getRealLength());%0A          document.insertString(offset
+EduUtils.replaceAnswerPlaceholder(project, document, placeholder, placeholder.getRealLength()
 , an
@@ -1591,22 +1591,9 @@
     
-  %7D%0A      %7D));
+%7D
 %0A   

use util method