package com.jetbrains.python.edu;

import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.ide.RecentProjectsManager;
import com.intellij.ide.ui.UISettings;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.ex.ProjectManagerEx;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.platform.templates.github.ZipUtil;
import com.intellij.util.PathUtil;
import com.intellij.util.messages.MessageBus;
import org.jetbrains.annotations.NonNls;

import java.io.File;
import java.io.IOException;

@SuppressWarnings({"UtilityClassWithoutPrivateConstructor", "UtilityClassWithPublicConstructor"})
public class StudyInitialConfigurator {
  private static final Logger LOG = Logger.getInstance(StudyInitialConfigurator.class.getName()
  );
  @NonNls private static final String CONFIGURED = "StudyPyCharm.InitialConfiguration";


  /**
   * @noinspection UnusedParameters
   */
  public StudyInitialConfigurator(MessageBus bus,
                                  UISettings uiSettings,
                                  CodeInsightSettings codeInsightSettings,
                                  final PropertiesComponent propertiesComponent,
                                  FileTypeManager fileTypeManager,
                                  final ProjectManagerEx projectManager,
                                  RecentProjectsManager recentProjectsManager) {
    if (!propertiesComponent.getBoolean(CONFIGURED, false)) {
      final File file = new File(getCoursesRoot(), "introduction_course.zip");
      final File newCourses = new File(PathManager.getConfigPath(), "courses");
      try {
        FileUtil.createDirectory(newCourses);
        String fileName = file.getName();
        String unzippedName = fileName.substring(0, fileName.indexOf("."));
        File courseDir = new File(newCourses, unzippedName);
        ZipUtil.unzip(null, courseDir, file, null, null, true);

      }
      catch (IOException e) {
        LOG.warn("Couldn't copy bundled courses " + e);
      }
    }
  }

  public static File getCoursesRoot() {
    @NonNls String jarPath = PathUtil.getJarPathForClass(StudyInitialConfigurator.class);
    if (jarPath.endsWith(".jar")) {
      final File jarFile = new File(jarPath);


      File pluginBaseDir = jarFile.getParentFile();
      return new File(pluginBaseDir, "courses");
    }

    return new File(jarPath , "courses");
  }

}

@@ -990,16 +990,19 @@
 NFIGURED
+_V1
  = %22Stud
@@ -1033,16 +1033,111 @@
 ation%22;%0A
+  @NonNls private static final String CONFIGURED_V11 = %22StudyPyCharm.InitialConfiguration1.1%22;%0A
 %0A%0A  /**%0A
@@ -1658,24 +1658,101 @@
 sManager) %7B%0A
+    final File file = new File(getCoursesRoot(), %22introduction_course.zip%22);%0A
     if (!pro
@@ -1789,16 +1789,19 @@
 NFIGURED
+_V1
 , false)
@@ -1813,36 +1813,42 @@
      final File 
-file
+newCourses
  = new File(getC
@@ -1847,93 +1847,8 @@
 ile(
-getCoursesRoot(), %22introduction_course.zip%22);%0A      final File newCourses = new File(
 Path
@@ -1942,24 +1942,748 @@
 urses);%0A    
+    copyCourse(file, newCourses);%0A        propertiesComponent.setValue(CONFIGURED_V1, %22true%22);%0A%0A      %7D%0A      catch (IOException e) %7B%0A        LOG.warn(%22Couldn't copy bundled courses %22 + e);%0A      %7D%0A    %7D%0A    if (!propertiesComponent.getBoolean(CONFIGURED_V11, false)) %7B%0A      final File newCourses = new File(PathManager.getConfigPath(), %22courses%22);%0A      if (newCourses.exists()) %7B%0A        try %7B%0A          copyCourse(file, newCourses);%0A          propertiesComponent.setValue(CONFIGURED_V11, %22true%22);%0A        %7D%0A        catch (IOException e) %7B%0A          LOG.warn(%22Couldn't copy bundled courses %22 + e);%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  private static void copyCourse(File bundledCourse, File userCourseDir) throws IOException %7B%0A
     String f
@@ -2692,19 +2692,28 @@
 eName = 
-fil
+bundledCours
 e.getNam
@@ -2717,20 +2717,16 @@
 Name();%0A
-    
     Stri
@@ -2789,20 +2789,16 @@
 (%22.%22));%0A
-    
     File
@@ -2819,26 +2819,29 @@
 ew File(
-new
+user
 Course
-s
+Dir
 , unzipp
@@ -2845,28 +2845,24 @@
 ippedName);%0A
-    
     ZipUtil.
@@ -2884,19 +2884,28 @@
 rseDir, 
-fil
+bundledCours
 e, null,
@@ -2921,117 +2921,8 @@
 ue);
-%0A%0A      %7D%0A      catch (IOException e) %7B%0A        LOG.warn(%22Couldn't copy bundled courses %22 + e);%0A      %7D%0A    %7D
 %0A  %7D

update course for version 1.0.1