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

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

        overwriteRepositoryHandler(project);
    }

    private void overwriteRepositoryHandler(Project project) {

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

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

//        DefaultProject defaultProject = (DefaultProject) project;
//        DefaultServiceRegistry serviceRegistry = (DefaultServiceRegistry) defaultProject.getServices();
//
//        try {
//            // 'cause it is package-private
//            Class serviceProviderClass =
//                    Class.forName("org.gradle.internal.service.DefaultServiceRegistry$ServiceProvider");
//
//            Object proxy = Proxy.newProxyInstance(
//                    serviceProviderClass.getClassLoader(),
//                    new Class[]{serviceProviderClass},
//                    (proxy1, method, args) -> {
//                        if ("get".equals(method.getName())) {
//                            return injector.getInstance(GitRepositoryHandler.class);
//                        } else {
//                            return null;
//                        }
//                    });
//
//            Map providerCache = (Map) ReflectionUtils.getValueIncludingSuperclasses("providerCache",
//                    serviceRegistry);
//            synchronized (serviceRegistry) {
//                providerCache.put(RepositoryHandler.class, proxy);
//            }
//
//        } catch (IllegalAccessException | ClassNotFoundException e) {
//            throw ExceptionHandler.uncheckException(e);
//        }
    }

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

@@ -3807,1327 +3807,8 @@
 );%0A%0A
-//        DefaultProject defaultProject = (DefaultProject) project;%0A//        DefaultServiceRegistry serviceRegistry = (DefaultServiceRegistry) defaultProject.getServices();%0A//%0A//        try %7B%0A//            // 'cause it is package-private%0A//            Class serviceProviderClass =%0A//                    Class.forName(%22org.gradle.internal.service.DefaultServiceRegistry$ServiceProvider%22);%0A//%0A//            Object proxy = Proxy.newProxyInstance(%0A//                    serviceProviderClass.getClassLoader(),%0A//                    new Class%5B%5D%7BserviceProviderClass%7D,%0A//                    (proxy1, method, args) -%3E %7B%0A//                        if (%22get%22.equals(method.getName())) %7B%0A//                            return injector.getInstance(GitRepositoryHandler.class);%0A//                        %7D else %7B%0A//                            return null;%0A//                        %7D%0A//                    %7D);%0A//%0A//            Map providerCache = (Map) ReflectionUtils.getValueIncludingSuperclasses(%22providerCache%22,%0A//                    serviceRegistry);%0A//            synchronized (serviceRegistry) %7B%0A//                providerCache.put(RepositoryHandler.class, proxy);%0A//            %7D%0A//%0A//        %7D catch (IllegalAccessException %7C ClassNotFoundException e) %7B%0A//            throw ExceptionHandler.uncheckException(e);%0A//        %7D%0A
     

Remove commented code