package com.jetbrains.edu.utils.generation;

import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix;
import com.intellij.execution.junit.JUnitExternalLibraryDescriptor;
import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.ide.util.projectWizard.JavaModuleBuilder;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.ModifiableModuleModel;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleWithNameAlreadyExists;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ExternalLibraryDescriptor;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.edu.learning.core.EduNames;
import com.jetbrains.edu.learning.courseFormat.Lesson;
import com.jetbrains.edu.learning.courseFormat.tasks.Task;
import com.jetbrains.edu.learning.courseGeneration.StudyGenerator;
import com.jetbrains.edu.utils.EduIntelliJNames;
import com.jetbrains.edu.utils.EduIntellijUtils;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

class EduUtilModuleBuilder extends JavaModuleBuilder {

  private final Lesson myAdditionalMaterials;

  public EduUtilModuleBuilder(String moduleDir, Lesson additionalMaterials) {
    myAdditionalMaterials = additionalMaterials;
    setName(EduIntelliJNames.UTIL);
    setModuleFilePath(FileUtil.join(moduleDir, EduIntelliJNames.UTIL, EduIntelliJNames.UTIL + ModuleFileType.DOT_DEFAULT_EXTENSION));
  }

  @NotNull
  @Override
  public Module createModule(@NotNull ModifiableModuleModel moduleModel) throws InvalidDataException, IOException, ModuleWithNameAlreadyExists, JDOMException, ConfigurationException {
    Module baseModule = super.createModule(moduleModel);
    String directory = getModuleFileDirectory();
    if (directory == null) {
      return baseModule;
    }
    VirtualFile moduleDir = VfsUtil.findFileByIoFile(new File(directory), true);
    if (moduleDir == null) {
      return baseModule;
    }
    VirtualFile src = moduleDir.findChild(EduNames.SRC);
    if (src == null) {
      return baseModule;
    }
    Project project = baseModule.getProject();
    StartupManager.getInstance(project).registerPostStartupActivity(() -> DumbService.getInstance(project).runWhenSmart(() -> ApplicationManager.getApplication().runWriteAction(() -> {
      EduIntellijUtils.addTemplate(project, src, "EduTestRunner.java");
    })));
    ExternalLibraryDescriptor descriptor = JUnitExternalLibraryDescriptor.JUNIT4;
    List<String> defaultRoots = descriptor.getLibraryClassesRoots();
    final List<String> urls = OrderEntryFix.refreshAndConvertToUrls(defaultRoots);
    ModuleRootModificationUtil.addModuleLibrary(baseModule, descriptor.getPresentableName(), urls, Collections.<String>emptyList());

    final List<Task> taskList = myAdditionalMaterials.getTaskList();
    if (taskList.size() == 1) {
      final Task task = taskList.get(0);
      for (Map.Entry<String, String> entry : task.getTestsText().entrySet()) {
        StudyGenerator.createChildFile(project.getBaseDir(), entry.getKey(), entry.getValue());
      }
    }
    return baseModule;
  }
}

@@ -3268,16 +3268,59 @@
 st());%0A%0A
+    if (myAdditionalMaterials != null) %7B%0A  
     fina
@@ -3380,24 +3380,26 @@
 List();%0A    
+  
 if (taskList
@@ -3416,24 +3416,26 @@
  1) %7B%0A      
+ 
+ 
 final Task t
@@ -3457,16 +3457,18 @@
 get(0);%0A
+  
       fo
@@ -3546,16 +3546,18 @@
         
+ 
+ 
 StudyGen
@@ -3632,24 +3632,34 @@
 etValue());%0A
+        %7D%0A
       %7D%0A    

fix util module creation for course creator projects