/*
 * 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.
 */

/*
 * @author max
 */
package org.jetbrains.java.generate.template;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiType;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.generate.element.FieldElement;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public abstract class TemplatesManager implements PersistentStateComponent<TemplatesState> {

  public static final Key<Map<String, PsiType>> TEMPLATE_IMPLICITS = Key.create("TEMPLATE_IMPLICITS");
  
  private TemplatesState myState = new TemplatesState();

  public abstract TemplateResource[] getDefaultTemplates();
  /**
   * Reads the content of the resource and return it as a String.
   * <p/>Uses the class loader that loaded this class to find the resource in its classpath.
   *
   * @param resource the resource name. Will lookup using the classpath.
   * @return the content if the resource
   * @throws java.io.IOException error reading the file.
   */
  protected static String readFile(String resource, Class<? extends TemplatesManager> templatesManagerClass) throws IOException {
    BufferedInputStream in = new BufferedInputStream(templatesManagerClass.getResourceAsStream(resource));
    return StringUtil.convertLineSeparators(FileUtil.loadTextAndClose(new InputStreamReader(in, CharsetToolkit.UTF8_CHARSET)));
  }

  public TemplatesState getState() {
        return myState;
    }

    public void loadState(TemplatesState state) {
        myState = state;
    }

    public void addTemplate(TemplateResource template) {
        myState.templates.add(template);
    }

    public void removeTemplate(TemplateResource template) {
        final Iterator<TemplateResource> it = myState.templates.iterator();
        while (it.hasNext()) {
            TemplateResource resource = it.next();
            if (Comparing.equal(resource.getFileName(), template.getFileName())) {
                it.remove();
            }
        }
    }

  public Collection<TemplateResource> getAllTemplates() {
    HashSet<String> names = new HashSet<>();
    Collection<TemplateResource> templates = new LinkedHashSet<>(Arrays.asList(getDefaultTemplates()));
    for (TemplateResource template : myState.templates) {
      if (names.add(template.getFileName())) {
        templates.add(template);
      }
    }
    return templates;
  }

  public TemplateResource getDefaultTemplate() {
    TemplateResource resource = findTemplateByName(myState.defaultTempalteName);
    if (resource != null) return resource;

    String initialTemplateName = getInitialTemplateName();
    resource = initialTemplateName != null ? findTemplateByName(initialTemplateName) : null;
    return ObjectUtils.notNull(resource, getAllTemplates().iterator().next());
  }

  protected String getInitialTemplateName () {
    return null;
  }

  @Nullable
  public TemplateResource findTemplateByName(String templateName) {
    for (TemplateResource template : getAllTemplates()) {
      if (Comparing.equal(template.getFileName(), templateName)) {
        return template;
      }
    }

    return null;
  }


    public void setDefaultTemplate(TemplateResource res) {
        myState.defaultTempalteName = res.getFileName();
    }

    public void setTemplates(List<TemplateResource> items) {
        myState.templates.clear();
        for (TemplateResource item : items) {
            if (!item.isDefault()) {
                myState.templates.add(item);
            }
        }
    }

    @NotNull
    public static PsiType createFieldListElementType(Project project) {
      final PsiType classType = createElementType(project, FieldElement.class);
      final PsiClass listClass = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_UTIL_LIST, GlobalSearchScope.allScope(project));
      return listClass != null ? JavaPsiFacade.getElementFactory(project).createType(listClass, classType) : PsiType.NULL;
    }

    @NotNull
    public static PsiType createElementType(Project project, Class<?> elementClass) {
      final List<String> methodNames = 
        ContainerUtil.mapNotNull(elementClass.getMethods(),
                                 method -> {
                                   final String methodName = method.getName();
                                   if (methodName.startsWith("set")) {
                                     //hide setters from completion list
                                     return null;
                                   }
                                   return method.getGenericReturnType().toString() + " " + methodName + "();";
                                 });
      final String text = "interface " + elementClass.getSimpleName() + " {\n" + StringUtil.join(methodNames, "\n") + "}";
      final PsiClass aClass = JavaPsiFacade.getElementFactory(project).createClassFromText(text, null).getInnerClasses()[0];
      return JavaPsiFacade.getElementFactory(project).createType(aClass);
    }
}
@@ -5545,20 +5545,65 @@
 h(%22set%22)
+ %7C%7C method.isSynthetic() %7C%7C method.isBridge()
 ) %7B%0A
-
         
@@ -5746,32 +5746,544 @@
               %7D%0A
+                                   String parametersString = StringUtil.join(method.getParameters(),%0A                                                                             param -%3E param.getParameterizedType().getTypeName() +%0A                                                                                      %22 %22 +%0A                                                                                      param.getName(), %0A                                                                             %22, %22);%0A
                 
@@ -6342,16 +6342,19 @@
 e().
-toString
+getTypeName
 () +
@@ -6375,16 +6375,40 @@
 ame + %22(
+%22 + parametersString + %22
 );%22;%0A   

fix generated member (IDEA-178875)