package com.jetbrains.edu.learning;

import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.edu.learning.actions.*;
import com.jetbrains.edu.learning.courseFormat.Task;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
import com.jetbrains.edu.learning.editor.StudyChoiceVariantsPanel;
import com.jetbrains.edu.learning.ui.StudyToolWindow;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collections;
import java.util.Map;

public abstract class StudyBasePluginConfigurator implements StudyPluginConfigurator {
  @NotNull
  @Override
  public DefaultActionGroup getActionGroup(Project project) {
    return getDefaultActionGroup();
  }

  @NotNull
  public static DefaultActionGroup getDefaultActionGroup() {
    final DefaultActionGroup group = new DefaultActionGroup();
    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;
  }

  @NotNull
  @Override
  public Map<String, JPanel> getAdditionalPanels(Project project) {
    return Collections.emptyMap();
  }

  @NotNull
  @Override
  public FileEditorManagerListener getFileEditorManagerListener(@NotNull Project project, @NotNull StudyToolWindow toolWindow) {

    return new FileEditorManagerListener() {
      @Override
      public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
        Task task = getTask(file);
        setTaskText(task, StudyUtils.getTaskDir(file));
        if (task != null && task.isChoiceTask()) {
          final StudyChoiceVariantsPanel choicePanel = new StudyChoiceVariantsPanel(task);
          toolWindow.setBottomComponent(choicePanel);
        }
      }

      @Override
      public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
        for (VirtualFile openedFile : source.getOpenFiles()) {
          if (StudyUtils.getTaskFile(project, openedFile) != null) {
            return;
          }
        }
        toolWindow.setEmptyText(project);
      }

      @Override
      public void selectionChanged(@NotNull FileEditorManagerEvent event) {
        VirtualFile file = event.getNewFile();
        if (file != null) {
          Task task = getTask(file);
          setTaskText(task, StudyUtils.getTaskDir(file));
        }
        toolWindow.setBottomComponent(null);
      }

      @Nullable
      private Task getTask(@NotNull VirtualFile file) {
        return StudyUtils.getTaskForFile(project, file);
      }

      private void setTaskText(@Nullable final Task task, @Nullable final VirtualFile taskDirectory) {
        String text = StudyUtils.getTaskTextFromTask(taskDirectory, task);
        if (text == null) {
          toolWindow.setEmptyText(project);
          return;
        }
        toolWindow.setTaskText(text, taskDirectory, project);
      }
    };
  }

  @Nullable
  @Override
  public StudyAfterCheckAction[] getAfterCheckActions() {
    return null;
  }
}

@@ -470,65 +470,8 @@
 sk;%0A
-import com.jetbrains.edu.learning.courseFormat.TaskFile;%0A
 impo
@@ -1936,12 +1936,26 @@
 null
- && 
+) %7B%0A          if (
 task
@@ -1969,24 +1969,26 @@
 ceTask()) %7B%0A
+  
           fi
@@ -2068,32 +2068,34 @@
 ask);%0A          
+  
 toolWindow.setBo
@@ -2118,24 +2118,114 @@
 oicePanel);%0A
+          %7D%0A          else %7B%0A            toolWindow.setBottomComponent(null);%0A          %7D%0A
         %7D%0A  

Remove choice variants panel for non-choice task