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 the interpreter
public class Language {

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

    public static void main(String[] args) {

        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];
        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;
        }

        try {
            runInterpreter(Files.readString(sourcefile), outname);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Function to take source code and output the result
    private static void runInterpreter(String sourceCode, String outName){
        //Extract tokens from the source code
        TokenScanner scanner = new TokenScanner();
        List<Token> tokens = scanner.extractTokens(sourceCode);
        //for (Token token : tokens) {
        //    System.out.println(token);
        //}
        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);
    }

    private static void interactiveMode() {
        Scanner input = new Scanner(System.in);
            String sourceCode = "1";
            while (sourceCode!=""){
                System.out.print("Code: ");
                sourceCode = input.nextLine();
                runInterpreter(sourceCode, "out");
                hadError=false;
            }
            input.close();
    }
    
    //Basic error reporting
    static void displayError(int line,String message){
        hadError=true;
        System.out.println("An error was encountered on line: "+line);
        System.out.println(message);
    }
    //Basic error reporting
    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);
    }
    
    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;

    }
}