package net.minecraftforge.gradle.user;

import java.io.File;

import org.gradle.api.Action;
import org.gradle.listener.ActionBroadcast;
import org.gradle.plugins.ide.eclipse.model.Classpath;
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.eclipse.model.Library;
import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory;

import net.minecraftforge.gradle.common.Constants;
import net.minecraftforge.gradle.tasks.ProcessJarTask;
import net.minecraftforge.gradle.tasks.user.ApplyBinPatchesTask;
import static net.minecraftforge.gradle.user.UserConstants.*;

public class ForgeUserPlugin extends UserBasePlugin
{
    @Override
    public void applyPlugin()
    {
        super.applyPlugin();

        ApplyBinPatchesTask binTask = makeTask("applyBinPatches", ApplyBinPatchesTask.class);
        {
            binTask.setInJar(delayedFile(Constants.JAR_MERGED));
            binTask.setOutJar(delayedFile(UserConstants.FORGE_BINPATCHED));
            binTask.setPatches(delayedFile(UserConstants.BINPATCHES));
            binTask.setClassesJar(delayedFile(UserConstants.BINARIES_JAR));
            binTask.setResources(delayedFileTree(UserConstants.RES_DIR));
            binTask.dependsOn("mergeJars");
        }

        ProcessJarTask procTask = (ProcessJarTask) project.getTasks().getByName("deobfuscateJar");
        {
            procTask.dependsOn(binTask);
            procTask.setInJar(delayedFile(UserConstants.FORGE_BINPATCHED));
            procTask.setOutCleanJar(delayedFile(UserConstants.FORGE_DEOBF_MCP));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void afterEvaluate()
    {
        String depBase = "net.minecraftforge:forge:" + getExtension().getApiVersion();
        project.getDependencies().add(CONFIG_USERDEV,      depBase + ":userdev");
        project.getDependencies().add(CONFIG_API_JAVADOCS, depBase + ":javadoc@zip");

        super.afterEvaluate();

        final File deobf = delayedFile(FORGE_DEOBF_MCP).call();

        project.getDependencies().add(CONFIG, project.files(deobf));

        EclipseModel eclipseConv = (EclipseModel) project.getExtensions().getByName("eclipse");
        ((ActionBroadcast<Classpath>)eclipseConv.getClasspath().getFile().getWhenMerged()).add(new Action<Classpath>()
        {
            FileReferenceFactory factory = new FileReferenceFactory();
            @Override
            public void execute(Classpath classpath)
            {
                for (ClasspathEntry e : classpath.getEntries())
                {
                    if (e instanceof Library)
                    {
                        Library lib = (Library)e;
                        if (lib.getLibrary().getFile().equals(deobf))
                        {
                            lib.setJavadocPath(factory.fromFile(project.getConfigurations().getByName(UserConstants.CONFIG_API_JAVADOCS).getSingleFile()));
                            //TODO: Add the source attachment here....
                        }
                    }
                }
            }
        });

        fixEclipseProject(ECLIPSE_LOCATION);
    }

    @Override
    protected void addATs(ProcessJarTask task)
    {
        task.addTransformer(delayedFile(UserConstants.FML_AT));
        task.addTransformer(delayedFile(UserConstants.FORGE_AT));
    }
}

@@ -430,16 +430,291 @@
 Factory;
+%0Aimport org.gradle.plugins.ide.idea.model.Dependency;%0Aimport org.gradle.plugins.ide.idea.model.IdeaModel;%0Aimport org.gradle.plugins.ide.idea.model.Module;%0Aimport org.gradle.plugins.ide.idea.model.PathFactory;%0Aimport org.gradle.plugins.ide.idea.model.SingleEntryModuleLibrary;
 %0A%0Aimport
@@ -3427,24 +3427,995 @@
        %7D);%0A%0A
+        IdeaModel ideaConv = (IdeaModel) project.getExtensions().getByName(%22idea%22);%0A        ((ActionBroadcast%3CModule%3E) ideaConv.getModule().getIml().getWhenMerged()).add(new Action%3CModule%3E() %7B%0A%0A            PathFactory factory = new PathFactory();%0A            @Override%0A            public void execute(Module module) %7B%0A                for (Dependency d : module.getDependencies()) %7B%0A                    if (d instanceof SingleEntryModuleLibrary) %7B%0A                        SingleEntryModuleLibrary lib = (SingleEntryModuleLibrary) d;%0A                        if (lib.getLibraryFile().equals(deobf)) %7B%0A                            lib.getJavadoc().add(factory.path(%22jar://%22 + project.getConfigurations().getByName(UserConstants.CONFIG_API_JAVADOCS).getSingleFile().getAbsolutePath().replace('%5C%5C', '/') + %22!/%22));%0A                            //TODO: Add the source attachment here....%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);%0A%0A
         fixE

Fix javadoc on IDEA