package org.anarres.gradle.plugin.jnaerator;

import com.ochafik.lang.jnaerator.JNAerator;
import com.ochafik.lang.jnaerator.JNAeratorCommandLineArgs;
import com.ochafik.lang.jnaerator.JNAeratorConfig;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author shevek
 */
public class JNAeratorTask extends DefaultTask {

    private static final Logger LOG = LoggerFactory.getLogger(JNAeratorTask.class);
    private File outputDir;
    private String libraryName;
    private String packageName;
    private FileCollection headerFiles;
    private JNAeratorConfig.Runtime runtimeMode = JNAeratorConfig.Runtime.JNA;

    @OutputDirectory
    public File getOutputDir() {
        return outputDir;
    }

    public void setOutputDir(Object outputDir) {
        this.outputDir = getProject().file(outputDir);
    }

    public void outputDir(Object outputDir) {
        setOutputDir(outputDir);
    }

    @Input
    public String getLibraryName() {
        return libraryName;
    }

    public void setLibraryName(String libraryName) {
        LOG.info("setLibraryName: " + libraryName);
        this.libraryName = libraryName;
    }

    public void libraryName(String libraryName) {
        setLibraryName(libraryName);
    }

    @Input
    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public void packageName(String packageName) {
        setPackageName(packageName);
    }

    @InputFiles
    public FileCollection getHeaderFiles() {
        LOG.info("getHeaderFiles = " + headerFiles);
        return headerFiles;
    }

    public void setHeaderFiles(Object... headerFiles) {
        LOG.info("setHeaderFiles = " + headerFiles);
        this.headerFiles = getProject().files(headerFiles);
    }

    public void headerFiles(Object... headerFiles) {
        setHeaderFiles(headerFiles);
    }

    public JNAeratorConfig.Runtime getRuntimeMode() {
        return runtimeMode;
    }

    public void setRuntimeMode(JNAeratorConfig.Runtime runtimeMode) {
        this.runtimeMode = runtimeMode;
    }

    public void runtimeMode(Object runtimeMode) {
        if (runtimeMode instanceof JNAeratorConfig.Runtime)
            this.runtimeMode = (JNAeratorConfig.Runtime) runtimeMode;
        else
            this.runtimeMode = JNAeratorConfig.Runtime.valueOf(String.valueOf(runtimeMode));
    }

    @TaskAction
    public void run() {
        List<String> args = new ArrayList<String>();

        args.add(JNAeratorCommandLineArgs.OptionDef.CurrentLibrary.clSwitch);
        args.add(getLibraryName());
        args.add(JNAeratorCommandLineArgs.OptionDef.CurrentPackage.clSwitch);
        args.add(getPackageName());

        for (File file : getHeaderFiles())
            args.add(file.getAbsolutePath());

        args.add(JNAeratorCommandLineArgs.OptionDef.OutputMode.clSwitch);
        args.add(JNAeratorConfig.OutputMode.Directory.name());

        args.add(JNAeratorCommandLineArgs.OptionDef.Runtime.clSwitch);
        args.add(getRuntimeMode().name());

        args.add(JNAeratorCommandLineArgs.OptionDef.OutputDir.clSwitch);
        // String outputPath = getPackageName().replace('.', File.separatorChar);
        // File outputDir = new File(getOutputDir(), outputPath);
        args.add(getOutputDir().getAbsolutePath());

        args.add(JNAeratorCommandLineArgs.OptionDef.ForceOverwrite.clSwitch);
        args.add(JNAeratorCommandLineArgs.OptionDef.Verbose.clSwitch);

        DefaultGroovyMethods.deleteDir(outputDir);
        outputDir.mkdirs();
        JNAerator.main(args.toArray(new String[args.size()]));
    }
}

@@ -565,65 +565,8 @@
 ion;
-%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/*
@@ -641,24 +641,27 @@
 tTask %7B%0A%0A   
+ //
  private sta
@@ -991,24 +991,45 @@
 tputDir() %7B%0A
+        getLogger();%0A
         retu
@@ -1381,32 +1381,35 @@
 yName) %7B%0A       
+ //
  LOG.info(%22setLi
@@ -1915,32 +1915,35 @@
 iles() %7B%0A       
+ //
  LOG.info(%22getHe
@@ -2063,23 +2063,26 @@
 iles) %7B%0A
-
        
+ //
  LOG.inf

Remove unnecessary logging.