package com.github.blindpirate.gogradle;

import com.github.blindpirate.gogradle.build.Configuration;
import com.github.blindpirate.gogradle.core.GolangConfigurationContainer;
import com.github.blindpirate.gogradle.core.dependency.GolangDependencyHandler;
import com.github.blindpirate.gogradle.core.dependency.parse.DefaultNotationParser;
import com.github.blindpirate.gogradle.ide.IdeaIntegration;
import com.github.blindpirate.gogradle.task.GolangTaskContainer;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.internal.reflect.Instantiator;

import javax.inject.Inject;
import java.util.stream.Stream;

import static com.github.blindpirate.gogradle.task.GolangTaskContainer.TASKS;


public class GolangPlugin implements Plugin<Project> {

    // injected by gradle
    private final Instantiator instantiator;

    private Injector injector;
    private GolangPluginSetting settings;
    private GolangTaskContainer golangTaskContainer;
    private Project project;
    private Action<? super Task> dependencyInjectionAction = new Action<Task>() {
        @Override
        public void execute(Task task) {
            injector.injectMembers(task);
        }
    };

    // This is invoked by gradle, not our Guice.
    @Inject
    public GolangPlugin(Instantiator instantiator) {
        this.instantiator = instantiator;
    }

    private Injector initGuice() {
        return Guice.createInjector(new GogradleModule(project, instantiator));
    }

    @Override
    public void apply(Project project) {
        init(project);
        customizeProjectInternalServices(project);
        configureSettings(project);
        configureConfigurations(project);
        configureTasks(project);
        configureGlobalInjector();
        hackIdeaPlugin();
    }

    private void hackIdeaPlugin() {
        project.getPlugins().withId("idea", plugin -> {
            injector.getInstance(IdeaIntegration.class).hack();
        });
    }

    private void init(Project project) {
        this.project = project;
        this.injector = initGuice();
        this.settings = injector.getInstance(GolangPluginSetting.class);
        this.golangTaskContainer = injector.getInstance(GolangTaskContainer.class);
    }

    private void configureGlobalInjector() {
        GogradleGlobal.INSTANCE.setInjector(injector);
    }

    private void configureTasks(Project project) {
        TaskContainer taskContainer = project.getTasks();
        TASKS.entrySet().forEach(entry -> {
            Task task = taskContainer.create(entry.getKey(), entry.getValue(), dependencyInjectionAction);
            golangTaskContainer.put((Class) entry.getValue(), task);
        });
    }

    /**
     * Here we cannot use Guice since we need GroovyObject to be mixed in
     * See {@link org.gradle.api.internal.AbstractClassGenerator}
     */
    private void customizeProjectInternalServices(Project project) {
        DefaultNotationParser parser = injector.getInstance(DefaultNotationParser.class);

        GolangConfigurationContainer configurationContainer =
                instantiator.newInstance(GolangConfigurationContainer.class,
                        instantiator);
        project.setProperty("configurationContainer", configurationContainer);

        project.setProperty("dependencyHandler",
                instantiator.newInstance(GolangDependencyHandler.class,
                        configurationContainer,
                        parser,
                        project));

        overwriteRepositoryHandler(project);
    }

    private void overwriteRepositoryHandler(Project project) {

        GitRepositoryHandler repositoryHandler = injector.getInstance(GitRepositoryHandler.class);

        ExtensionAware.class.cast(project.getRepositories()).getExtensions().add("git", repositoryHandler);

    }

    private void configureConfigurations(Project project) {
        ConfigurationContainer configurations = project.getConfigurations();
        Stream.of(Configuration.values()).map(Configuration::getName).forEach(configurations::create);
    }

    private void configureSettings(Project project) {
        project.getExtensions().add("golang", settings);
    }
}

@@ -437,35 +437,144 @@
 le.task.
-GolangTaskContainer
+BuildTask;%0Aimport com.github.blindpirate.gogradle.task.GolangTaskContainer;%0Aimport com.github.blindpirate.gogradle.task.TestTask
 ;%0Aimport
@@ -1080,21 +1080,196 @@
 ntainer.
-TASKS
+BUILD_TASK_NAME;%0Aimport static com.github.blindpirate.gogradle.task.GolangTaskContainer.TASKS;%0Aimport static com.github.blindpirate.gogradle.task.GolangTaskContainer.TEST_TASK_NAME
 ;%0A%0A%0Apubl
@@ -3229,32 +3229,247 @@
 sk);%0A        %7D);
+%0A%0A        BuildTask.class.cast(taskContainer.getByName(BUILD_TASK_NAME)).addDefaultActionIfNoCustomActions();%0A        TestTask.class.cast(taskContainer.getByName(TEST_TASK_NAME)).addDefaultActionIfNoCustomActions();
 %0A    %7D%0A%0A    /**%0A

Add default actions callback