/*
 * Copyright (c) 2004 JetBrains s.r.o. All  Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduct the above copyright
 *  notice, this list of conditions and the following disclaimer in
 *  the documentation and/or other materials provided with the distribution.
 *
 * Neither the name of JetBrains or IntelliJ IDEA
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. JETBRAINS AND ITS LICENSORS SHALL NOT
 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL JETBRAINS OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
 * IF JETBRAINS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 */
package org.jetbrains.idea.devkit.module;

import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.ProjectWizardStepFactory;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
import com.intellij.openapi.util.IconLoader;

import javax.swing.*;

public class PluginModuleType extends ModuleType<PluginModuleBuilder> {
  private static final Icon PLUGIN_MODULE_ICON = IconLoader.getIcon("/general/pluginManager.png");
  private static final Icon PLUGIN_MODULE_NODE_ICON = IconLoader.getIcon("/nodes/plugin.png");
  private static final Icon ADD_PLUGIN_MODULE_ICON = IconLoader.getIcon("/add_plugin_modulewizard.png");
  private static PluginModuleType ourInstance = new PluginModuleType();

  private PluginModuleType() {
    super("PLUGIN_MODULE");
  }

  public static PluginModuleType getInstance() {
    return ourInstance;
  }

  public final boolean isJ2EE() {
    return false;
  }

  public ModuleWizardStep[] createWizardSteps(WizardContext wizardContext, PluginModuleBuilder pluginModuleBuilder, ModulesProvider modulesProvider) {
    final ProjectWizardStepFactory stepFactory = ProjectWizardStepFactory.getInstance();
    final ModuleWizardStep nameAndLocationStep = stepFactory.createNameAndLocationStep(wizardContext, pluginModuleBuilder, modulesProvider, ADD_PLUGIN_MODULE_ICON, null);
    return new ModuleWizardStep[] {
      nameAndLocationStep,
      stepFactory.createSourcePathsStep(nameAndLocationStep, pluginModuleBuilder, ADD_PLUGIN_MODULE_ICON, null),
      stepFactory.createOutputPathPathsStep(nameAndLocationStep, pluginModuleBuilder, ADD_PLUGIN_MODULE_ICON, null)
    };
  }

  public PluginModuleBuilder createModuleBuilder() {
    return new PluginModuleBuilder();
  }

  public String getName() {
    return "Plugin Module";
  }

  public String getDescription() {
    return "<html>This module type is designed to ease <b>development of Plugins</b> for IntelliJ IDEA. It allows you to properly configure " +
           "target IntelliJ IDEA SDK and all necessary deployment settings. You can also configure running/debugging instance of IntelliJ IDEA " +
           "with your plugin enabled.</html>";
  }

  public Icon getBigIcon() {
    return PLUGIN_MODULE_ICON;
  }

  public Icon getNodeIcon(boolean isOpened) {
    return PLUGIN_MODULE_NODE_ICON;
  }
}
@@ -1876,16 +1876,69 @@
 leType;%0A
+import com.intellij.openapi.projectRoots.ProjectJdk;%0A
 import c
@@ -2034,41 +2034,169 @@
 til.
-IconLoader;%0A%0Aimport javax.swing.*
+Computable;%0Aimport com.intellij.openapi.util.IconLoader;%0Aimport org.jetbrains.idea.devkit.projectRoots.IdeaJdk;%0A%0Aimport javax.swing.*;%0Aimport java.util.ArrayList
 ;%0A%0Ap
@@ -2883,16 +2883,22 @@
 rdSteps(
+final 
 WizardCo
@@ -2921,49 +2921,141 @@
 ext,
- PluginModuleBuilder pluginModuleBuilder,
+%0A                                              PluginModuleBuilder pluginModuleBuilder,%0A                                             
  Mod
@@ -3175,16 +3175,91 @@
 ance();%0A
+    ArrayList%3CModuleWizardStep%3E steps = new ArrayList%3CModuleWizardStep%3E();%0A
     fina
@@ -3306,24 +3306,31 @@
  stepFactory
+%0A      
 .createNameA
@@ -3436,73 +3436,442 @@
     
-return new ModuleWizardStep%5B%5D %7B%0A      nameAndLocationStep,%0A      
+steps.add(nameAndLocationStep);%0A    steps.add(stepFactory.createProjectJdkStep(wizardContext, pluginModuleBuilder, new Computable%3CBoolean%3E() %7B%0A      public Boolean compute() %7B%0A        final ProjectJdk projectJdk = wizardContext.getProjectJdk();%0A        return projectJdk == null %7C%7C ! (projectJdk.getSdkType() instanceof IdeaJdk) ? Boolean.TRUE : Boolean.FALSE;%0A      %7D%0A    %7D, ADD_PLUGIN_MODULE_ICON, null)); //todo help%0A    steps.add(
 step
@@ -3971,24 +3971,33 @@
 N, null)
-,
+);
 %0A    
-  
+steps.add(
 stepFact
@@ -4097,22 +4097,79 @@
 N, null)
+);
 %0A    
-%7D
+return steps.toArray(new ModuleWizardStep%5Bsteps.size()%5D)
 ;%0A  %7D%0A%0A 

add project jdk step to appropriate module types