package de.jcup.egradle.codeassist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DefaultTypeTemplatesProvider implements TypeTemplatesProvider {

	private static final String DEPENDENCY_HANDLER = "org.gradle.api.artifacts.dsl.DependencyHandler";
	private Map<String, List<Template>> map = new TreeMap<>();

	public DefaultTypeTemplatesProvider() {
		createAndAddTemplate(DEPENDENCY_HANDLER, "compile '$cursor'");
		createAndAddTemplate(DEPENDENCY_HANDLER, "compile project(':$cursor'");
		createAndAddTemplate(DEPENDENCY_HANDLER, "runtime '$cursor'");
		createAndAddTemplate(DEPENDENCY_HANDLER, "testCompile '$cursor'");
		createAndAddTemplate(DEPENDENCY_HANDLER, "testCompile project(':$cursor'");
		createAndAddTemplate(DEPENDENCY_HANDLER, "testRuntime '$cursor'");
	}

	@Override
	public List<Template> getTemplatesForType(String type) {
		List<Template> templates = map.get(type);
		if (templates == null) {
			return Collections.emptyList();
		}
		return templates;
	}

	private void createAndAddTemplate(String type, String content) {
		createAndAddTemplate(type, content, content);
	}

	private void createAndAddTemplate(String type, String name, String content) {
		List<Template> templates = map.get(type);
		if (templates == null) {
			templates = new ArrayList<>();
			map.put(type, templates);
		}
		Template template = new Template(name, content);

		templates.add(template);
	}

}

@@ -243,204 +243,639 @@
 %7B%0A%0A%09
-private static final String DEPENDENCY_HANDLER = %22org.gradle.api.artifacts.dsl.DependencyHandler%22;%0A%09private Map%3CString, List%3CTemplate%3E%3E map = new TreeMap%3C%3E();%0A%0A%09public DefaultTypeTemplatesProvider
+/*%0A%09 * FIXME ATR, 27.02.2017: THINK ABOUT: maybe getTemplateFor(Type type) would%0A%09 * be better! This would make inheritance possible - e.g. for Project and%0A%09 * plugin aware!%0A%09 */%0A%09private static final String DEPENDENCY_HANDLER = %22org.gradle.api.artifacts.dsl.DependencyHandler%22;%0A%09private static final String PLUGIN_AWARE = %22org.gradle.api.plugins.PluginAware%22;%0A%09private static final String PROJECT = %22org.gradle.api.Project%22;%0A%09private Map%3CString, List%3CTemplate%3E%3E map = new TreeMap%3C%3E();%0A%0A%09public DefaultTypeTemplatesProvider() %7B%0A%09%09addDefaultsForDependencies();%0A%09%09addDefaultsForApplys();%0A%0A%09%7D%0A%0A%09private void addDefaultsForDependencies
 () %7B
@@ -1002,32 +1002,33 @@
 oject(':$cursor'
+)
 %22);%0A%09%09createAndA
@@ -1215,32 +1215,33 @@
 oject(':$cursor'
+)
 %22);%0A%09%09createAndA
@@ -1301,16 +1301,554 @@
 %22);%0A%09%7D%0A%0A
+%09private void addDefaultsForApplys() %7B%0A%09%09Template applyPluginTemplate = createTemplate(%22apply plugin:'$cursor'%22);%0A%09%09addTemplate(PLUGIN_AWARE, applyPluginTemplate);%0A%09%09addTemplate(PROJECT, applyPluginTemplate); // necessary, because%0A%09%09%09%09%09%09%09%09%09%09%09%09%09// currently no inheritance%0A%09%09%09%09%09%09%09%09%09%09%09%09%09// supported%0A%0A%09%09Template applyFromTemplate = createTemplate(%22apply from '$cursor'%22);%0A%09%09addTemplate(PLUGIN_AWARE, applyFromTemplate);%0A%09%09addTemplate(PROJECT, applyFromTemplate); // necessary, because currently%0A%09%09%09%09%09%09%09%09%09%09%09%09%09// no inheritance supported%0A%09%7D%0A%0A
 %09@Overri
@@ -2038,24 +2038,122 @@
 plates;%0A%09%7D%0A%0A
+%09private Template createTemplate(String content) %7B%0A%09%09return createTemplate(content, content);%0A%09%7D%0A%0A
 %09private voi
@@ -2271,74 +2271,383 @@
 ate 
-void createAndAddTemplate(String type, String name, String content
+Template createTemplate(String name, String content) %7B%0A%09%09Template template = new Template(name, content);%0A%09%09return template;%0A%09%7D%0A%0A%09private Template createAndAddTemplate(String type, String name, String content) %7B%0A%09%09Template template = createTemplate(name, content);%0A%09%09addTemplate(type, template);%0A%09%09return template;%0A%09%7D%0A%0A%09private void addTemplate(String type, Template template
 ) %7B%0A
@@ -2787,59 +2787,8 @@
 %0A%09%09%7D
-%0A%09%09Template template = new Template(name, content);
 %0A%0A%09%09

apply from and plugin #120