package fi.evident.apina.gradle.tasks;

import fi.evident.apina.ApinaProcessor;
import fi.evident.apina.java.reader.Classpath;
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.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.intellij.lang.annotations.Language;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;
import static org.gradle.util.GFileUtils.writeFile;

public class ApinaTask extends DefaultTask {

    private File target;

    private FileCollection classpath;

    private List<String> blackBoxClasses = new ArrayList<>();

    public static final String GENERATE_API_CLIENT_TASK_NAME = "apina";

    @TaskAction
    public void generateTypeScript() throws IOException {
        requireNonNull(classpath, "classpath not initialized");
        requireNonNull(target, "target not initialized");

        Classpath myClasspath = new Classpath();
        for (File file : classpath)
            myClasspath.addRoot(file.toPath());

        ApinaProcessor processor = new ApinaProcessor(myClasspath);

        for (@Language("RegExp") String pattern : blackBoxClasses)
            processor.settings.blackBoxClasses.addPattern(pattern);

        String output = processor.process();

        writeFile(output, target, "UTF-8");
    }

    @InputFiles
    public FileCollection getClasspath() {
        return classpath;
    }

    public void setClasspath(FileCollection classpath) {
        this.classpath = classpath;
    }

    @OutputFile
    public File getTarget() {
        return target;
    }

    public void setTarget(File target) {
        this.target = target;
    }

    @Input
    public List<String> getBlackBoxClasses() {
        return blackBoxClasses;
    }

    public void setBlackBoxClasses(List<String> blackBoxTypePatterns) {
        this.blackBoxClasses = blackBoxTypePatterns;
    }
}

@@ -495,20 +495,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -821,24 +821,89 @@
 yList%3C%3E();%0A%0A
+    private Map%3CString,List%3CString%3E%3E imports = new HashMap%3C%3E();%0A%0A
     public s
@@ -1495,24 +1495,183 @@
 (pattern);%0A%0A
+        for (Map.Entry%3CString, List%3CString%3E%3E anImport : imports.entrySet())%0A            processor.settings.addImport(anImport.getKey(), anImport.getValue());%0A%0A
         Stri
@@ -2266,24 +2266,24 @@
 Patterns) %7B%0A
-
         this
@@ -2328,11 +2328,208 @@
 s;%0A    %7D
+%0A%0A    @Input%0A    public Map%3CString, List%3CString%3E%3E getImports() %7B%0A        return imports;%0A    %7D%0A%0A    public void setImports(Map%3CString, List%3CString%3E%3E imports) %7B%0A        this.imports = imports;%0A    %7D
 %0A%7D%0A

Support specifying imports from Gradle task