package main;

import java.util.function.Function;

import interpreter.*;
import terminal.LispTerminal;

public class LispMain {

    private static final String ANSI_RESET = "\u001B[0m";
    private static final String ANSI_RED = "\u001B[31m";
    private static final String ANSI_GREEN = "\u001B[32m";
    private static final String ANSI_YELLOW = "\u001B[33m";
    private static final String ANSI_PURPLE = "\u001B[35m";

    private LispMain() {}

    public static void main(String[] args) {
        LispTerminal terminal = new LispTerminal();

        terminal.run();

        // LispInterpreter interpreter = buildInterpreter(args);
        // interpreter.interpret();
    }

    private static LispInterpreter buildInterpreter(String[] args) {
        LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
        configureInput(args, builder);
        builder.setOutput(System.out);
        builder.setErrorOutput(System.err);
        builder.setTerminationFunction(() -> System.exit(0));
        builder.setErrorTerminationFunction(() -> System.exit(1));
        builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN));
        builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW));
        builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED));
        builder.setCriticalOutputDecorator(makeColorDecorator(ANSI_PURPLE));

        return builder.build();
    }

    private static void configureInput(String[] args, LispInterpreterBuilder builder) {
        if (args.length > 0)
            builder.useFile(args[0]);
        else
            builder.setInput(System.in, "stdin");
    }

    private static Function<String, String> makeColorDecorator(String color) {
        return new Function<String, String>() {

            @Override
            public String apply(String s) {
                return color + s + ANSI_RESET;
            }
        };
    }

}
