/*
Copyright (C) 2017 Migeran

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.moe.gradle.tasks;

import org.gradle.api.GradleException;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.moe.gradle.MoeExtension;
import org.moe.gradle.MoePlugin;
import org.moe.gradle.anns.IgnoreUnused;
import org.moe.gradle.anns.Nullable;

import java.io.ByteArrayOutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

public class NatJGen extends AbstractBaseTask {

    private static final Logger LOG = Logging.getLogger(Launchers.class);

    private static final String CONVENTION_CONFIGURATION = "config";
    private static final String CONVENTION_NATJGEN_JAR = "natjgen-jar";

    @Nullable
    private String config;

    @Input
    @Optional
    @Nullable
    public String getConfig() {
        return nullableGetOrConvention(config, CONVENTION_CONFIGURATION);
    }

    @IgnoreUnused
    public void setConfig(@Nullable String config) {
        this.config = config;
    }

    @Nullable
    private String natjgen;

    @Input
    @Optional
    @Nullable
    public String getNatJGenJar() {
        return nullableGetOrConvention(natjgen, CONVENTION_NATJGEN_JAR);
    }

    @IgnoreUnused
    public void setNatJGenJar(@Nullable String natjgen) {
        this.natjgen = natjgen;
    }

    @Override
    protected void run() {

        String biningConfiguration = getConfig();

        if (biningConfiguration == null || biningConfiguration.isEmpty()) {
            throw new GradleException("Missing binding configuration settings");
        }



        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        javaexec(spec -> {
            spec.setMain("-jar");
            spec.setWorkingDir(getMoeExtension().getSDK().getToolsDir().getAbsolutePath());
            spec.args(getNatJGenJar());
            spec.args(getProject().getProjectDir().getParent());
            spec.args(getProject().getName());
            spec.args(biningConfiguration);
            spec.setStandardOutput(baos);
        });
    }

    protected final void setupMoeTask() {

        setSupportsRemoteBuild(false);

        final MoeExtension ext = getMoeExtension();

        // Construct default output path
        final Path out = Paths.get(MoePlugin.MOE);

        setDescription("Generate binding");

        addConvention(CONVENTION_CONFIGURATION, () ->
                ext.natjgen.getConfig()
        );

        addConvention(CONVENTION_NATJGEN_JAR, () ->
                ext.getSdk().getNatJGenJar().getAbsolutePath()
        );

        addConvention(CONVENTION_LOG_FILE, () -> resolvePathInBuildDir(out, "NatJGen.log"));
    }
}

@@ -958,16 +958,37 @@
 Stream;%0A
+import java.io.File;%0A
 import j
@@ -1088,83 +1088,8 @@
  %7B%0A%0A
-    private static final Logger LOG = Logging.getLogger(Launchers.class);%0A%0A
     
@@ -1528,30 +1528,16 @@
 %0A    %7D%0A%0A
-    @Nullable%0A
     priv
@@ -1540,22 +1540,20 @@
 private 
-String
+File
  natjgen
@@ -1570,36 +1570,8 @@
 put%0A
-    @Optional%0A    @Nullable%0A
     
@@ -1573,30 +1573,28 @@
 %0A    public 
-String
+File
  getNatJGenJ
@@ -1728,32 +1728,20 @@
 JGenJar(
-@Nullable String
+File
  natjgen
@@ -2044,18 +2044,16 @@
      %7D%0A%0A
-%0A%0A
         
@@ -2306,16 +2306,34 @@
 GenJar()
+.getAbsolutePath()
 );%0A     
@@ -2871,32 +2871,16 @@
 N, () -%3E
-%0A               
  ext.nat
@@ -2895,25 +2895,16 @@
 Config()
-%0A        
 );%0A%0A    
@@ -2954,24 +2954,8 @@
 ) -%3E
-%0A               
  ext
@@ -2983,35 +2983,8 @@
 ar()
-.getAbsolutePath()%0A        
 );%0A%0A

Remove unused log, reformat code