package com.jetbrains.edu.learning.actions;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.jetbrains.edu.learning.StudyTaskManager;
import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.courseFormat.Course;
import com.jetbrains.edu.learning.courseFormat.Lesson;
import com.jetbrains.edu.learning.courseFormat.RemoteCourse;
import com.jetbrains.edu.learning.courseFormat.tasks.Task;
import com.jetbrains.edu.learning.navigation.StudyNavigator;
import com.jetbrains.edu.learning.stepic.EduAdaptiveStepicConnector;
import icons.EducationalCoreIcons;

public class StudyUpdateRecommendationAction extends DumbAwareAction {
  public static final String ACTION_ID = "Edu.UpdateRecommendation";

  public StudyUpdateRecommendationAction() {
    super("Update course", "Update course", EducationalCoreIcons.StepikRefresh);
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    assert project != null;

    Course course = StudyTaskManager.getInstance(project).getCourse();
    assert course != null;

    Lesson adaptiveLesson = course.getLessons().get(0);
    assert adaptiveLesson != null;

    int taskNumber = adaptiveLesson.getTaskList().size();
    Task lastRecommendationInCourse = adaptiveLesson.getTaskList().get(taskNumber - 1);
    Task lastRecommendationOnStepik = ProgressManager.getInstance().runProcessWithProgressSynchronously(
      () -> {
        ProgressManager.getInstance().getProgressIndicator().setIndeterminate(true);
        return StudyUtils.execCancelable(() -> EduAdaptiveStepicConnector.getNextRecommendation(project, (RemoteCourse)course));
      },
      "Updating Course", true, project);

    if (lastRecommendationOnStepik != null && lastRecommendationOnStepik.getStepId() != lastRecommendationInCourse.getStepId()) {
      lastRecommendationOnStepik.initTask(adaptiveLesson, false);
      EduAdaptiveStepicConnector.replaceCurrentTask(project, lastRecommendationOnStepik, adaptiveLesson);
      ApplicationManager.getApplication().invokeLater(() -> {
        VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
        ProjectView.getInstance(project).refresh();
        StudyNavigator.navigateToTask(project, lastRecommendationOnStepik);
      });
    }
  }

  @Override
  public void update(AnActionEvent e) {
    Presentation presentation = e.getPresentation();

    Project project = e.getProject();
    if (project == null) {
      presentation.setEnabledAndVisible(false);
      return;
    }

    Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null || !course.isAdaptive()) {
      presentation.setEnabledAndVisible(false);
      return;
    }

    presentation.setEnabledAndVisible(true);
  }
}

@@ -1030,77 +1030,8 @@
 on %7B
-%0A  public static final String ACTION_ID = %22Edu.UpdateRecommendation%22;
 %0A%0A  
@@ -1084,21 +1084,26 @@
  super(%22
-Updat
+Synchroniz
 e course
@@ -1106,21 +1106,26 @@
 urse%22, %22
-Updat
+Synchroniz
 e course
@@ -1983,21 +1983,26 @@
 %0A      %22
-Updat
+Synchroniz
 ing Cour

Change action text: Update course -> Synchronize course, Remove unused ACTION_ID var