package com.jetbrains.edu.learning;

import com.intellij.ide.IdeView;
import com.intellij.ide.util.DirectoryUtil;
import com.intellij.lang.LanguageExtension;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.jetbrains.edu.learning.actions.*;
import com.jetbrains.edu.learning.core.EduNames;
import com.jetbrains.edu.learning.courseFormat.Course;
import com.jetbrains.edu.learning.courseFormat.StudyItem;
import com.jetbrains.edu.learning.courseFormat.Task;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public interface EduPluginConfigurator {
  LanguageExtension<EduPluginConfigurator> INSTANCE = new LanguageExtension<>("Edu.pluginConfigurator");

  @NotNull
  String getTestFileName();

  default PsiDirectory createLesson(@NotNull Project project,
                                    @NotNull StudyItem item,
                                    @Nullable IdeView view,
                                    @NotNull PsiDirectory parentDirectory) {
    final PsiDirectory[] lessonDirectory = new PsiDirectory[1];
    ApplicationManager.getApplication().runWriteAction(() -> {
      lessonDirectory[0] = DirectoryUtil.createSubdirectories(EduNames.LESSON + item.getIndex(), parentDirectory, "\\/");
    });
    if (lessonDirectory[0] != null) {
      if (view != null) {
        view.selectElement(lessonDirectory[0]);
      }
    }
    return lessonDirectory[0];
  }


  PsiDirectory createTask(@NotNull final Project project, @NotNull final StudyItem item,
                          @Nullable final IdeView view, @NotNull final PsiDirectory parentDirectory,
                          @NotNull final Course course);

  void createTaskContent(@NotNull Project project,
                         @Nullable IdeView view,
                         PsiDirectory taskDirectory);

  boolean excludeFromArchive(File pathname);

  default boolean isTestFile(VirtualFile file) {
    return false;
  }

  default void createTestsForNewSubtask(@NotNull Project project, @NotNull Task task) {}

  /**
   * @return parameter for CodeMirror script. Available languages: @see <@linktourl http://codemirror.net/mode/>
   */
  @NotNull
  default String getDefaultHighlightingMode(){return "";}

  @NotNull
  default String getLanguageScriptUrl(){return "";}

  StudyCheckAction getCheckAction();

  @NotNull
  default DefaultActionGroup getTaskDescriptionActionGroup() {
    final DefaultActionGroup group = new DefaultActionGroup();
    group.add(getCheckAction());
    group.add(new StudyPreviousTaskAction());
    group.add(new StudyNextTaskAction());
    group.add(new StudyRefreshTaskFileAction());
    group.add(new StudyShowHintAction());

    group.add(new StudyRunAction());
    group.add(new StudyEditInputAction());
    return group;
  }
}

@@ -151,16 +151,123 @@
 ension;%0A
+import com.intellij.openapi.actionSystem.ActionManager;%0Aimport com.intellij.openapi.actionSystem.AnAction;%0A
 import c
@@ -2534,16 +2534,17 @@
 ngMode()
+ 
 %7Breturn 
@@ -2599,16 +2599,17 @@
 iptUrl()
+ 
 %7Breturn 
@@ -2830,183 +2830,357 @@
     
-group.add(new StudyPreviousTaskAction());%0A    group.add(new StudyNextTaskAction());%0A    group.add(new StudyRefreshTaskFileAction());%0A    group.add(new StudyShowHintAction());%0A
+String%5B%5D ids = new String%5B%5D%7BStudyPreviousTaskAction.ACTION_ID, StudyNextTaskAction.ACTION_ID, StudyRefreshTaskFileAction.ACTION_ID,%0A      StudyShowHintAction.ACTION_ID%7D;%0A    for (String id : ids) %7B%0A      AnAction action = ActionManager.getInstance().getAction(id);%0A      if (action == null) %7B%0A        continue;%0A      %7D%0A      group.add(action);%0A    %7D
 %0A   

add actions by id in order to make them removable in language plugins (EDU-895)