package io.freefair.gradle.plugins.android;

import com.android.build.gradle.TestedExtension;
import com.android.build.gradle.api.BaseVariant;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.external.javadoc.StandardJavadocDocletOptions;
import org.gradle.internal.jvm.Jvm;
import org.gradle.util.GUtil;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static org.codehaus.groovy.runtime.StringGroovyMethods.capitalize;

public class AndroidJavadocPlugin extends AndroidProjectPlugin {

    private Map<String, Javadoc> javadocTasks = new HashMap<>();

    private Task allJavadocTask;

    @Override
    public void apply(Project project) {
        super.apply(project);

        allJavadocTask = project.getTasks().create("javadoc", ajdTasks -> {
            ajdTasks.setDescription("Generate Javadoc for all variants");
            ajdTasks.setGroup(JavaBasePlugin.DOCUMENTATION_GROUP);
        });

    }

    @Override
    protected void withAndroid(TestedExtension extension) {
        super.withAndroid(extension);
        getAndroidVariants().all(variant -> {
            Javadoc javadocTask = getJavadocTask(getProject(), variant);

            allJavadocTask.dependsOn(javadocTask);
        });
    }

    public Javadoc getJavadocTask(Project project, BaseVariant variant) {

        if (!javadocTasks.containsKey(variant.getName())) {

            Javadoc task = project.getTasks().create("javadoc" + capitalize((CharSequence) variant.getName()), Javadoc.class, javadoc -> {
                javadoc.setDescription("Generate Javadoc for the " + variant.getName() + " variant");
                javadoc.setGroup(JavaBasePlugin.DOCUMENTATION_GROUP);

                javadoc.dependsOn(variant.getJavaCompiler());

                JavaCompile javacTask = getJavaCompile(variant);

                javadoc.setSource(javacTask.getSource());

                javadoc.setClasspath(project.files(javacTask.getDestinationDir()).plus(javacTask.getClasspath()));
                javadoc.getOptions().setSource(javacTask.getSourceCompatibility());

                //javadoc.exclude '**/BuildConfig.java'
                javadoc.exclude("**/R.java");

                javadoc.getOptions().encoding("UTF-8");
                javadoc.getOptions().setBootClasspath(getAndroidExtension().getBootClasspath());

                if (javadoc.getOptions() instanceof StandardJavadocDocletOptions) {
                    StandardJavadocDocletOptions realOptions = (StandardJavadocDocletOptions) javadoc.getOptions();

                    realOptions.docEncoding("UTF-8");
                    realOptions.charSet("UTF-8");

                    Serializable javaVersion = GUtil.elvis(Jvm.current().getJavaVersion().getMajorVersion(), '8');
                    realOptions.links("http://docs.oracle.com/javase/" + javaVersion + "/docs/api/");
                    realOptions.linksOffline("http://developer.android.com/reference/", getAndroidExtension().getSdkDirectory() + "/docs/reference");
                }

                javadoc.setFailOnError(false);

                File docsDir;
                if (project.hasProperty("docsDir")) {
                    docsDir = (File) project.property("docsDir");
                } else {
                    docsDir = new File(project.getBuildDir(), "docs");
                }
                File javadocDir = new File(docsDir, "javadoc");
                javadoc.setDestinationDir(new File(javadocDir, variant.getDirName()));
            });

            javadocTasks.put(variant.getName(), task);
        }

        return javadocTasks.get(variant.getName());
    }
}

@@ -136,16 +136,51 @@
 ariant;%0A
+import org.gradle.api.JavaVersion;%0A
 import o
@@ -439,121 +439,26 @@
 ns;%0A
-import org.gradle.internal.jvm.Jvm;%0Aimport org.gradle.util.GUtil;%0A%0Aimport java.io.File;%0Aimport java.io.Serializab
+%0Aimport java.io.Fi
 le;%0A
@@ -2796,49 +2796,40 @@
    S
-erializable javaVersion = GUtil.elvis(Jvm
+tring majorVersion = JavaVersion
 .cur
@@ -2838,20 +2838,21 @@
 nt().get
-Java
+Major
 Version(
@@ -2852,21 +2852,60 @@
 ersion()
-.getM
+;%0A                    if (Integer.parseInt(m
 ajorVers
@@ -2911,17 +2911,17 @@
 sion
-(), '8');
+) %3C 11) %7B
 %0A   
@@ -2929,32 +2929,36 @@
                 
+    
 realOptions.link
@@ -2960,24 +2960,25 @@
 .links(%22http
+s
 ://docs.orac
@@ -2995,20 +2995,21 @@
 ase/%22 + 
-java
+major
 Version 
@@ -3021,24 +3021,57 @@
 ocs/api/%22);%0A
+                    %7D else %7B%0A    
             
@@ -3099,107 +3099,173 @@
 inks
-Offline
 (%22http
+s
 ://d
-eveloper.android.com/reference/%22, getAndroidExtension().getSdkDirectory() + %22/docs
+ocs.oracle.com/en/java/javase/%22 + majorVersion + %22/docs/api/%22);%0A                    %7D%0A                    realOptions.links(%22https://developer.android.com
 /ref
@@ -3270,16 +3270,17 @@
 eference
+/
 %22);%0A    

Update Javadoc links