package com.jdroid.gradle.commons;

import com.jdroid.java.collections.Lists;

import org.gradle.api.Action;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.api.tasks.testing.Test;
import org.gradle.external.javadoc.CoreJavadocOptions;

public abstract class JavaBaseGradlePlugin extends BaseGradlePlugin {

	protected Boolean isJavaDocPublicationEnabled;
	public Boolean isKotlinEnabled;
	public Boolean isKtLintEnabled;

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

		// TODO This is not working for kotlin. Remove javadoc support and add kotlin doc support https://kotlinlang.org/docs/reference/kotlin-doc.html
		//isJavaDocPublicationEnabled = propertyResolver.getBooleanProp("JAVADOC_PUBLICATION_ENABLED", false);
		isJavaDocPublicationEnabled = false;

		project.afterEvaluate(new Action<Project>() {
			@Override
			public void execute(Project it) {
				for (Test each : it.getTasks().withType(Test.class)) {
					each.setScanForTestClasses(true);
					if (!propertyResolver.getBooleanProp("INTEGRATION_TESTS_ENABLED", true)) {
						each.exclude(((JavaBaseGradleExtension)jdroid).getIntegrationTestsPattern());
					}
				}
			}
		});

		if (JavaVersion.current().isJava8Compatible()) {
			project.afterEvaluate(new Action<Project>() {
				@Override
				public void execute(Project it) {
					for (Javadoc each : it.getTasks().withType(org.gradle.api.tasks.javadoc.Javadoc.class)) {
						((CoreJavadocOptions)each.getOptions()).addStringOption("Xdoclint:none", "-quiet");
					};
				}
			});
		}

		isKotlinEnabled = propertyResolver.getBooleanProp("KOTLIN_ENABLED", true);
		isKtLintEnabled = propertyResolver.getBooleanProp("KTLINT_ENABLED", isKotlinEnabled);
	}

	protected void configureKotlin() {
		addDependency("implementation", "org.jetbrains.kotlin", "kotlin-stdlib-jdk8", "1.3.31");
		addDependency("implementation", "org.jetbrains.kotlin", "kotlin-reflect", "1.3.31");
		// TODO See how to configure this
//		compileKotlin {
//			kotlinOptions {
//				jvmTarget = "1.8"
//			}
//		}
//
//		compileTestKotlin {
//			kotlinOptions {
//				jvmTarget = "1.8"
//			}
//		}
		if (isKtLintEnabled) {
			configureKtlint();
		}
	}

	protected void configureKtlint() {
		addConfiguration("ktlint");
		addDependency("ktlint", "com.pinterest", "ktlint", "0.33.0");
		Task ktlintTask = project.getTasks().create("ktlint", JavaExec.class, new Action<JavaExec>() {
			@Override
			public void execute(JavaExec javaExec) {
				javaExec.setDescription("Check Kotlin code style.");
				javaExec.setMain("com.pinterest.ktlint.Main");
				javaExec.setClasspath(project.getConfigurations().findByName("ktlint"));

				// to generate report in checkstyle format prepend following args:
				// "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
				javaExec.setArgs(Lists.newArrayList("src/**/*.kt"));
			}
		});
		ktlintTask.setGroup("verification");
		project.getTasks().findByName("check").dependsOn(ktlintTask);

		Task ktlintFormatTask = project.getTasks().create("ktlintFormat", JavaExec.class, new Action<JavaExec>() {
			@Override
			public void execute(JavaExec javaExec) {
				javaExec.setDescription("Fix Kotlin code style deviations.");
				javaExec.setMain("com.pinterest.ktlint.Main");
				javaExec.setClasspath(project.getConfigurations().findByName("ktlint"));
				javaExec.setArgs(Lists.newArrayList("-F", "src/**/*.kt"));
			}
		});
		ktlintFormatTask.setGroup("formatting");
	}

	protected String getJavaSourceCompatibility() {
		return JavaVersion.VERSION_1_8.toString();
	}

	protected String getJavaTargetCompatibility() {
		return JavaVersion.VERSION_1_8.toString();
	}

	protected Class<? extends JavaBaseGradleExtension> getExtensionClass() {
		return JavaBaseGradleExtension.class;
	}

}

@@ -630,299 +630,8 @@
 );%0A%0A
-%09%09// TODO This is not working for kotlin. Remove javadoc support and add kotlin doc support https://kotlinlang.org/docs/reference/kotlin-doc.html%0A%09%09//isJavaDocPublicationEnabled = propertyResolver.getBooleanProp(%22JAVADOC_PUBLICATION_ENABLED%22, false);%0A%09%09isJavaDocPublicationEnabled = false;%0A%0A
 %09%09pr
@@ -1539,16 +1539,387 @@
 abled);%0A
+%0A%09%09isJavaDocPublicationEnabled = !isKotlinEnabled;%0A%09%09isJavaDocPublicationEnabled = propertyResolver.getBooleanProp(%22JAVADOC_PUBLICATION_ENABLED%22, false);%0A%09%09if (isKotlinEnabled) %7B%0A%09%09%09// TODO This is not working for kotlin. Remove javadoc support and add kotlin doc support https://kotlinlang.org/docs/reference/kotlin-doc.html%0A%09%09%09isJavaDocPublicationEnabled = false;%0A%09%09%7D%0A%0A
 %09%7D%0A%0A%09pro

Enable javadoc for non jdroid kotlin projects