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);
	}

}

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 {

	/*
	 * FIXME ATR, 27.02.2017: THINK ABOUT: maybe getTemplateFor(Type type) would
	 * be better! This would make inheritance possible - e.g. for Project and
	 * plugin aware!
	 */
	private static final String DEPENDENCY_HANDLER = "org.gradle.api.artifacts.dsl.DependencyHandler";
	private static final String PLUGIN_AWARE = "org.gradle.api.plugins.PluginAware";
	private static final String PROJECT = "org.gradle.api.Project";
	private Map<String, List<Template>> map = new TreeMap<>();

	public DefaultTypeTemplatesProvider() {
		addDefaultsForDependencies();
		addDefaultsForApplys();

	}

	private void addDefaultsForDependencies() {
		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'");
	}

	private void addDefaultsForApplys() {
		Template applyPluginTemplate = createTemplate("apply plugin:'$cursor'");
		addTemplate(PLUGIN_AWARE, applyPluginTemplate);
		addTemplate(PROJECT, applyPluginTemplate); // necessary, because
													// currently no inheritance
													// supported

		Template applyFromTemplate = createTemplate("apply from '$cursor'");
		addTemplate(PLUGIN_AWARE, applyFromTemplate);
		addTemplate(PROJECT, applyFromTemplate); // necessary, because currently
													// no inheritance supported
	}

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

	private Template createTemplate(String content) {
		return createTemplate(content, content);
	}

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

	private Template createTemplate(String name, String content) {
		Template template = new Template(name, content);
		return template;
	}

	private Template createAndAddTemplate(String type, String name, String content) {
		Template template = createTemplate(name, content);
		addTemplate(type, template);
		return template;
	}

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

		templates.add(template);
	}

}

apply from and plugin #120