package net.onedaybeard.gradle;

import com.artemis.FluidGenerator;
import com.artemis.FluidGeneratorPreferences;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

/**
 * Weaving wrapper for gradle.
 *
 * @author Adrian Papari
 * @author Daan van Yperen
 */
public class FluidApiGenerationTask extends DefaultTask {


    @Input
    private File generatedSourcesDirectory;

    @Input
    private FileCollection classpath;

    @Input
    public FluidGeneratorPreferences preferences = new FluidGeneratorPreferences();

    private Logger log = getLogger();

    @TaskAction
    public void fluid() {
        log.info("Artemis Fluid api plugin started.");


        prepareGeneratedSourcesFolder();
        includeGeneratedSourcesInCompilation();

        new FluidGenerator().generate(
                classpathAsUrls(preferences),
                generatedSourcesDirectory, createLogAdapter(), preferences);
    }

    /**
     * bridge maven/internal logging.
     */
    private com.artemis.generator.util.Log createLogAdapter() {
        return new com.artemis.generator.util.Log() {
            @Override
            public void info(String msg) {
                log.info(msg);
            }

            @Override
            public void error(String msg) {
                log.error(msg);
            }
        };
    }

    /**
     * Setup generated sources folder if missing.
     */
    private void prepareGeneratedSourcesFolder() {
        if (!generatedSourcesDirectory.exists() && !generatedSourcesDirectory.mkdirs()) {
            log.error("Could not create " + generatedSourcesDirectory);
        }
    }

    /**
     * Must include manually, or maven buids will fail.
     */
    private void includeGeneratedSourcesInCompilation() {
//		getProject().addCompileSourceRoot(generatedSourcesDirectory().getPath());
    }

    private Set<URL> classpathAsUrls(FluidGeneratorPreferences preferences) {
        try {
            Set<URL> urls = new HashSet<>();
            for (File element : classpath) {
                URL url = element.toURI().toURL();
                if (!preferences.matchesIgnoredClasspath(url.toString())) {
                    urls.add(url);
                    log.info("Including: " + url);
                }
            }
            return urls;
        } catch (MalformedURLException e) {
            throw new RuntimeException("failed to complete classpathAsUrls.", e);
        }
    }

    public File getGeneratedSourcesDirectory() {
        return generatedSourcesDirectory;
    }

    public void setGeneratedSourcesDirectory(File generatedSourcesDirectory) {
        this.generatedSourcesDirectory = generatedSourcesDirectory;
    }

    public FileCollection getClasspath() {
        return classpath;
    }

    public void setClasspath(FileCollection classpath) {
        this.classpath = classpath;
    }
}
@@ -143,16 +143,44 @@
 ltTask;%0A
+import org.gradle.api.Task;%0A
 import o
@@ -252,16 +252,55 @@
 Logger;%0A
+import org.gradle.api.logging.Logging;%0A
 import o
@@ -650,17 +650,16 @@
 Task %7B%0A%0A
-%0A
     @Inp
@@ -856,47 +856,8 @@
 );%0A%0A
-    private Logger log = getLogger();%0A%0A
     
@@ -898,27 +898,35 @@
 ) %7B%0A        
-log
+getLogger()
 .info(%22Artem
@@ -958,17 +958,16 @@
 ed.%22);%0A%0A
-%0A
         
@@ -1466,27 +1466,35 @@
             
-log
+getLogger()
 .info(msg);%0A
@@ -1586,27 +1586,35 @@
             
-log
+getLogger()
 .error(msg);
@@ -1865,19 +1865,27 @@
         
-log
+getLogger()
 .error(%22
@@ -2529,19 +2529,27 @@
         
-log
+getLogger()
 .info(%22I

Fix #577: Fluid gradle plugin logging does not work.