package Compiler;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

/** 
 *Base class for running the Compiler 
 *Can run either from a file, or in an interactive mode
 */
public class Language {

    static boolean leaveCFile = false;
    static boolean hadError = false;
    static boolean printC = false;
    static boolean executeAfter = false;
    static Path sourcefile;

    //Main function for the compiler
    public static void main(String[] args) {

        //Extract required command line arguments
        try {
            sourcefile = Paths.get(args[0]);
        } catch (java.lang.ArrayIndexOutOfBoundsException e) {
            interactiveMode();
            return;
        }

        if (args[0].equals("-h") || args[0].equals("--help")) {
            System.out.println(getHelpText());
            return;
        }
    
        if (!(Files.exists(sourcefile))) {
            System.err.println("Could not find source code path.");
            return;
        }
        
        Path initOutPath = Paths.get(args[0]);
        String outname = initOutPath.getName(initOutPath.getNameCount() - 1).toString().split("\\.(?=[^\\.]+$)")[0];

        //Extract optional command line arguments
        ArrayList<String> arrayArgs = new ArrayList<>();
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            arrayArgs.add(arg);
            if (arg.equals("-o") || arg.equals("--out")) {
                try {
                    outname = args[i + 1];
                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    System.err.println("Invalid output name provided");
                    return;
                }
            }
            if (arg.equals("-c") || arg.equals("--keep-c-file")) {
                leaveCFile = true;
            }
            if (arg.equals("-pc") || arg.equals("--print-c")) {
                printC = true;
            }
            if (arg.equals("-e") || arg.equals("--execute")) {
                executeAfter = true;
            }
        }

        if (outname.startsWith("-")) {
            System.err.println("Invalid output name provided");
            return;
        }

        //Run the compiler on the source code
        try {
            runCompiler(Files.readString(sourcefile), outname);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** 
     * Function to take source code, run the compiler and write to C
     *
     *  @param sourcecode the full source code as a string
     *  @param outName the name to write the compiled code to
     */
    private static void runCompiler(String sourceCode, String outName){
        //Extract tokens from the source code
        TokenScanner scanner = new TokenScanner();
        List<Token> tokens = scanner.extractTokens(sourceCode);
        if (hadError) return;

        //Parse into AST
        Parser parser = new Parser(tokens);
        List<Statement> ast = parser.parse();
        if (hadError) return;

        //Translate AST into equivalent C code
        Translator translator = new Translator();
        List<String> code = translator.compileToC(ast, printC);
        if (hadError) return;

        //Execute created C code
        ExecuteC cExecutor = new ExecuteC();
        cExecutor.compileAndExecuteC(code, outName, executeAfter, leaveCFile);
    }

    /**
     * Method for running the compiler in an interactive mode
     */
    private static void interactiveMode() {
        Scanner input = new Scanner(System.in);
        String sourceCode = "1";
        //Run compiler line by line
        while (sourceCode!=""){
            System.out.print("Code: ");
            sourceCode = input.nextLine();
            runCompiler(sourceCode, "out");
            hadError=false;
        }
        input.close();
    }
    
    /**
     * Method for displaying an error to the user
     * @param line the line the error occured on
     * @param message an error message to display to the user
     */
    static void displayError(int line,String message){
        hadError=true;
        System.out.println("An error was encountered on line: "+line);
        System.out.println(message);
    }


    static void displayError(String message){
        hadError=true;
        System.out.println("An error was encountered");
        System.out.println(message);
    }
    /**
     * Method for displaying error based on a specific token
     * @param token the token the parser detected the error on
     * @param message an error message to display to the user
     */
    static void displayError(Token token,String message){
        hadError=true;
        System.out.println("An error was encountered on line: "+token.line);
        System.out.println("ERROR: "+token.text);
        System.out.println(message);
    }
    
    /**
     * Method for getting the helpfile text
     * @return the text for the helpfile
     */
    private static String getHelpText(){
        String helpText = "";
        try {
            helpText = Utils.readFile("Compiler/helpfile.txt");
        }
        // Catch any IO exceptions
        catch (IOException e) {
            System.out.println(e);
        }

        // Catch anything else
        catch (Exception e) {
            System.out.println(e);
        }

        return helpText;

    }
}