/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.jvm.toolchain.internal;

import org.gradle.api.Describable;
import org.gradle.api.JavaVersion;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavadocTool;
import org.gradle.util.VersionNumber;

import javax.inject.Inject;
import java.io.File;

public class JavaToolchain implements Describable {

    private final JavaInstallationProbe.ProbeResult probe;
    private final JavaCompilerFactory compilerFactory;
    private final ToolchainToolFactory toolFactory;
    private final File javaHome;

    @Inject
    public JavaToolchain(JavaInstallationProbe.ProbeResult probe, JavaCompilerFactory compilerFactory, ToolchainToolFactory toolFactory) {
        this.probe = probe;
        this.javaHome = computeJavaHome(probe);
        this.compilerFactory = compilerFactory;
        this.toolFactory = toolFactory;
    }

    @Internal
    public JavaCompiler getJavaCompiler() {
        return new DefaultToolchainJavaCompiler(this, compilerFactory);
    }

    @Internal
    public JavaLauncher getJavaLauncher() {
        return new DefaultToolchainJavaLauncher(findExecutable("java"));
    }

    @Internal
    public JavadocTool getJavadocTool() {
        return toolFactory.create(JavadocTool.class, this);
    }

    @Input
    public JavaVersion getJavaMajorVersion() {
        return probe.getJavaVersion();
    }

    @Internal
    public VersionNumber getToolVersion() {
        return VersionNumber.parse(probe.getImplementationJavaVersion());
    }

    @Internal
    public File getJavaHome() {
        return probe.getJavaHome();
    }

    @Internal
    public boolean isJdk() {
        return probe.getInstallType() == JavaInstallationProbe.InstallType.IS_JDK;
    }

    @Internal
    @Override
    public String getDisplayName() {
        return javaHome.getAbsolutePath();
    }

    public File findExecutable(String toolname) {
        return new File(getJavaHome(), getBinaryPath(toolname));
    }

    private File computeJavaHome(JavaInstallationProbe.ProbeResult probe) {
        final File home = probe.getJavaHome();
        final File parentFile = home.getParentFile();
        final boolean isEmbeddedJre = home.getName().equalsIgnoreCase("jre");
        if (isEmbeddedJre && new File(parentFile, getBinaryPath("java")).exists()) {
            return parentFile;
        }
        return home;
    }

    private String getBinaryPath(String java) {
        return "bin/" + OperatingSystem.current().getExecutableName(java);
    }
}

@@ -1145,47 +1145,21 @@
 nal 
-JavaInstallationProbe.ProbeResult probe
+boolean isJdk
 ;%0A  
@@ -1295,16 +1295,114 @@
 avaHome;
+%0A    private final VersionNumber implementationVersion;%0A    private final JavaVersion javaVersion;
 %0A%0A    @I
@@ -1562,70 +1562,496 @@
 this
-.probe = probe;%0A        this.javaHome = computeJavaHome(probe)
+(probe.getJavaHome(), probe.getJavaVersion(), probe.getImplementationJavaVersion(), probe.getInstallType() == JavaInstallationProbe.InstallType.IS_JDK, compilerFactory, toolFactory);%0A    %7D%0A%0A    JavaToolchain(File javaHome, JavaVersion version, String implementationJavaVersion, boolean isJdk, JavaCompilerFactory compilerFactory, ToolchainToolFactory toolFactory) %7B%0A        this.javaHome = computeEnclosingJavaHome(javaHome);%0A        this.javaVersion = version;%0A        this.isJdk = isJdk
 ;%0A  
@@ -2132,24 +2132,109 @@
 oolFactory;%0A
+        this.implementationVersion = VersionNumber.parse(implementationJavaVersion);%0A
     %7D%0A%0A    @
@@ -2699,26 +2699,17 @@
  return 
-probe.getJ
+j
 avaVersi
@@ -2710,18 +2710,16 @@
 aVersion
-()
 ;%0A    %7D%0A
@@ -2796,38 +2796,9 @@
 urn 
-VersionNumber.parse(probe.getI
+i
 mple
@@ -2810,22 +2810,15 @@
 tion
-Java
 Version
-())
 ;%0A  
@@ -2883,35 +2883,24 @@
  return 
-probe.getJ
+j
 avaHome
-()
 ;%0A    %7D%0A
@@ -2962,74 +2962,13 @@
 urn 
-probe.getInstallType() == JavaInstallationProbe.InstallType.IS_JDK
+isJdk
 ;%0A  
@@ -3237,106 +3237,38 @@
 pute
-JavaHome(JavaInstallationProbe.ProbeResult probe) %7B%0A        final File home = probe.getJavaHome();
+EnclosingJavaHome(File home) %7B
 %0A   

Use correct javahome to lookup tools