/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.platform.templates;

import com.intellij.ide.util.projectWizard.EmptyModuleBuilder;
import com.intellij.ide.util.projectWizard.ModuleBuilder;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.platform.ProjectTemplate;
import com.intellij.platform.ProjectTemplatesFactory;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.NullableFunction;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author Dmitry Avdeev
 *         Date: 10/9/12
 */
public class PlainModuleTemplatesFactory implements ProjectTemplatesFactory {

  @NotNull
  @Override
  public String[] getGroups() {
    List<ModuleBuilder> builders = ModuleBuilder.getAllBuilders();
    List<String> groups = ContainerUtil.map(builders, new Function<ModuleBuilder, String>() {
      @Override
      public String fun(ModuleBuilder builder) {
        return builder.getGroupName();
      }
    });
    groups.add(OTHER_GROUP);
    return ArrayUtil.toStringArray(groups);
  }

  @NotNull
  @Override
  public ProjectTemplate[] createTemplates(final String group, WizardContext context) {
    if (OTHER_GROUP.equals(group)) {
      return context.isCreatingNewProject() ? new ProjectTemplate[] { new PlainModuleTemplate(new EmptyModuleBuilder() {
        @Override
        public String getPresentableName() {
          return "Empty Project";
        }

        @Override
        public String getDescription() {
          return "Empty project without modules. Use it to create free-style module structure.";
        }
      })} : ProjectTemplate.EMPTY_ARRAY;
    }
    ModuleBuilder[] builders = context.getAllBuilders();
    return ContainerUtil.mapNotNull(builders, new NullableFunction<ModuleBuilder, ProjectTemplate>() {
      @Nullable
      @Override
      public ProjectTemplate fun(ModuleBuilder builder) {
        return builder.getGroupName().equals(group) ? new PlainModuleTemplate(builder) : null;
      }
    }, ProjectTemplate.EMPTY_ARRAY);
  }

  private static class PlainModuleTemplate implements ProjectTemplate {
    private final ModuleBuilder myBuilder;

    public PlainModuleTemplate(ModuleBuilder builder) {
      myBuilder = builder;
    }

    @NotNull
    @Override
    public String getName() {
      return myBuilder.getPresentableName();
    }

    @Nullable
    @Override
    public String getDescription() {
      return myBuilder.getDescription();
    }

    @NotNull
    @Override
    public ModuleBuilder createModuleBuilder() {
      return myBuilder;
    }

    @Nullable
    @Override
    public ValidationInfo validateSettings() {
      return null;
    }
  }
}

@@ -1942,23 +1942,21 @@
 %7B%0A      
-return 
+if (!
 context.
@@ -1977,18 +1977,84 @@
 roject()
- ?
+) %7B%0A        return ProjectTemplate.EMPTY_ARRAY;%0A      %7D%0A      return
  new Pro
@@ -2071,11 +2071,9 @@
 te%5B%5D
- %7B 
+%7B
 new 
@@ -2406,38 +2406,8 @@
  %7D)%7D
- : ProjectTemplate.EMPTY_ARRAY
 ;%0A  

remove empty project from templates list