package com.jetbrains.python.module;

import com.intellij.ide.util.importProject.ModuleDescriptor;
import com.intellij.ide.util.importProject.ProjectDescriptor;
import com.intellij.ide.util.projectWizard.importSources.DetectedProjectRoot;
import com.intellij.ide.util.projectWizard.importSources.ProjectFromSourcesBuilder;
import com.intellij.ide.util.projectWizard.importSources.ProjectStructureDetector;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author yole
 */
public class PyProjectStructureDetector extends ProjectStructureDetector {
  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.module.PyProjectStructureDetector"); 
  
  
  @NotNull
  @Override
  public DirectoryProcessingResult detectRoots(@NotNull File dir,
                                               @NotNull File[] children,
                                               @NotNull File base,
                                               @NotNull List<DetectedProjectRoot> result) {
    LOG.info("Detecting roots under "  + dir);
    for (File child : children) {
      if (FileUtil.getExtension(child.getName()).equals("py")) {
        LOG.info("Found Python file " + child.getPath());
        result.add(new DetectedProjectRoot(dir) {
          @NotNull
          @Override
          public String getRootTypeName() {
            return "Python";
          }
        });
        return DirectoryProcessingResult.SKIP_CHILDREN;
      }
    }
    return DirectoryProcessingResult.PROCESS_CHILDREN;
  }

  @Override
  public void setupProjectStructure(@NotNull Collection<DetectedProjectRoot> roots,
                                    @NotNull ProjectDescriptor projectDescriptor,
                                    @NotNull ProjectFromSourcesBuilder builder) {
    if (!roots.isEmpty() && !hasRootsFromOtherDetectors(builder)) {
      List<ModuleDescriptor> modules = projectDescriptor.getModules();
      if (modules.isEmpty()) {
        modules = new ArrayList<ModuleDescriptor>();
        for (DetectedProjectRoot root : roots) {
          modules.add(new ModuleDescriptor(root.getDirectory(), PythonModuleType.getInstance(), root));
        }
        projectDescriptor.setModules(modules);
      }
    }
  }

  private static boolean hasRootsFromOtherDetectors(ProjectFromSourcesBuilder builder) {
    for (ProjectStructureDetector projectStructureDetector : Extensions.getExtensions(EP_NAME)) {
      if (!(projectStructureDetector instanceof PyProjectStructureDetector)) {
        final Collection<DetectedProjectRoot> roots = builder.getProjectRoots(projectStructureDetector);
        if (!roots.isEmpty()) {
          return true;
        }
      }
    }
    return false;
  }
}

@@ -146,32 +146,162 @@
 jectDescriptor;%0A
+import com.intellij.ide.util.projectWizard.ModuleWizardStep;%0Aimport com.intellij.ide.util.projectWizard.ProjectWizardStepFactory;%0A
 import com.intel
@@ -716,16 +716,38 @@
 tNull;%0A%0A
+import javax.swing.*;%0A
 import j
@@ -816,16 +816,46 @@
 ection;%0A
+import java.util.Collections;%0A
 import j
@@ -2644,16 +2644,293 @@
  %7D%0A  %7D%0A%0A
+  @Override%0A  public List%3CModuleWizardStep%3E createWizardSteps(ProjectFromSourcesBuilder builder, ProjectDescriptor projectDescriptor, Icon stepIcon) %7B%0A    return Collections.singletonList(ProjectWizardStepFactory.getInstance().createProjectJdkStep(builder.getContext()));%0A  %7D%0A%0A
   privat

create project from existing sources: select suitable project sdk