package nl.rubensten.texifyidea.run;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Ruben Schellekens, Sten Wessel
 */
public enum LatexCompiler {

    PDFLATEX("pdfLaTeX", "pdflatex"),
    LUALATEX("LuaLaTeX", "lualatex");

    private String displayName;
    private String executableName;

    LatexCompiler(String displayName, String executableName) {
        this.displayName = displayName;
        this.executableName = executableName;
    }

    @Nullable
    public List<String> getCommand(LatexRunConfiguration runConfig, Project project) {
        List<String> command = new ArrayList<>();

        ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
        ProjectFileIndex fileIndex = rootManager.getFileIndex();
        VirtualFile mainFile = runConfig.getMainFile();
        VirtualFile moduleRoot = fileIndex.getContentRootForFile(runConfig.getMainFile());
        VirtualFile[] moduleRoots = rootManager.getContentSourceRoots();
        if (moduleRoot == null || mainFile == null) {
            return null;
        }

        if (this == PDFLATEX) {
            if (runConfig.getCompilerPath() != null) {
                command.add(runConfig.getCompilerPath());
            } else {
                command.add("pdflatex");
            }
            command.add("-file-line-error");
            command.add("-interaction=nonstopmode");
            command.add("-synctex=1");
            command.add("-output-format=" + runConfig.getOutputFormat().name().toLowerCase());
            
            if (runConfig.hasOutputDirectories() && System.getProperty("os.name").contains("Windows")) {
                command.add("-output-directory=" + moduleRoot.getPath() + "/out");
            }

            if (runConfig.hasAuxiliaryDirectories() && System.getProperty("os.name").contains("Windows")) {
                command.add("-aux-directory=" + moduleRoot.getPath() + "/auxil");
            }

            if (System.getProperty("os.name").contains("Windows")) {
                for (VirtualFile root : moduleRoots) {
                    command.add("-include-directory=" + root.getPath());
                }
            }

        } else if (this == LUALATEX) {
            if (runConfig.getCompilerPath() != null) {
                command.add(runConfig.getCompilerPath());
            } else {
                command.add("lualatex");
            }

            // Some commands are the same as for pdflatex
            command.add("-file-line-error");
            command.add("-interaction=nonstopmode");
            command.add("-synctex=1");
            command.add("-output-format=" + runConfig.getOutputFormat().name().toLowerCase());
            command.add("-output-directory=" + moduleRoot.getPath() + "/out");

            // But lualatex has no -aux-directory
        }

        // Custom compiler arguments specified by the user
        if (runConfig.getCompilerArguments() != null) {
            String[] args = runConfig.getCompilerArguments().split("\\s+");
            command.addAll(Arrays.asList(args));
        }

        command.add(mainFile.getName());

        return command;
    }

    public String getExecutableName() {
        return executableName;
    }

    @Override
    public String toString() {
        return this.displayName;
    }

    /**
     * @author Ruben Schellekens
     */
    public enum Format {

        PDF,
        DVI;

        @Nullable
        public static Format byNameIgnoreCase(@Nullable String name) {
            for (Format format : values()) {
                if (format.name().equalsIgnoreCase(name)) {
                    return format;
                }
            }

            return null;
        }
    }
}

@@ -1790,28 +1790,16 @@
 ase());%0A
-            
 %0A       
@@ -2944,24 +2944,134 @@
 werCase());%0A
+%0A            if (runConfig.hasOutputDirectories() && System.getProperty(%22os.name%22).contains(%22Windows%22)) %7B%0A    
             
@@ -3136,16 +3136,30 @@
 %22/out%22);
+%0A            %7D
 %0A%0A      

Add support for out/ directory option with lualatex