package interpreter;

import java.io.*;
import java.util.function.Function;

import environment.RuntimeEnvironment;
import error.*;

public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {

    private static LispInterpreterBuilder uniqueInstance = new LispInterpreterBuilderImpl();

    public static LispInterpreterBuilder getInstance() {
        return uniqueInstance;
    }

    private String inputName;
    private InputStream inputStream;
    private PrintStream outputStream;
    private PrintStream errorOutputStream;
    private Runnable terminationFunction;
    private Runnable errorTerminationFunction;
    private Function<String, String> outputDecorator;
    private Function<String, String> valueOutputDecorator;
    private Function<String, String> warningOutputDecorator;
    private Function<String, String> errorOutputDecorator;
    private Function<String, String> criticalOutputDecorator;
    private RuntimeEnvironment environment;
    private boolean isInteractive;
    private boolean isFileBased;
    protected boolean isBuilt;

    protected LispInterpreterBuilderImpl() {
        this.environment = RuntimeEnvironment.getInstance();
        this.inputName = "";
        this.isInteractive = true;
        this.isFileBased = false;
        this.isBuilt = false;
        this.outputDecorator = s -> s;
        this.valueOutputDecorator = s -> s;
        this.warningOutputDecorator = s -> s;
        this.errorOutputDecorator = s -> s;
        this.criticalOutputDecorator = s -> s;
    }

    @Override
    public void setInput(InputStream inputStream, String inputName) {
        this.inputStream = inputStream;
        this.inputName = inputName;
    }

    @Override
    public void setOutput(PrintStream outputStream) {
        this.outputStream = outputStream;

    }

    @Override
    public void setErrorOutput(PrintStream errorOutputStream) {
        this.errorOutputStream = errorOutputStream;
    }

    @Override
    public void setTerminationFunction(Runnable terminationFunction) {
        this.terminationFunction = terminationFunction;
    }

    @Override
    public void setErrorTerminationFunction(Runnable errorTerminationFunction) {
        this.errorTerminationFunction = errorTerminationFunction;
    }

    @Override
    public void setNotInteractive() {
        this.isInteractive = false;
    }

    @Override
    public void useFile(String fileName) {
        this.isFileBased = true;
        this.inputName = fileName;
        this.setNotInteractive();
    }

    @Override
    public void setOutputDecorator(Function<String, String> decorator) {
        this.outputDecorator = decorator;
    }

    @Override
    public void setValueOutputDecorator(Function<String, String> decorator) {
        this.valueOutputDecorator = decorator;
    }

    @Override
    public void setWarningOutputDecorator(Function<String, String> decorator) {
        this.warningOutputDecorator = decorator;

    }

    @Override
    public void setErrorOutputDecorator(Function<String, String> decorator) {
        this.errorOutputDecorator = decorator;

    }

    @Override
    public void setCriticalOutputDecorator(Function<String, String> decorator) {
        this.criticalOutputDecorator = decorator;
    }

    @Override
    public LispInterpreter build() {
        if (!isBuilt)
            return buildInterpreter();
        else
            throw new InterpreterAlreadyBuiltException();
    }

    private LispInterpreter buildInterpreter() {
        configureRuntimeEnvironment();
        LispInterpreter lispInterpreter = createInterpreter();
        isBuilt = true;

        return lispInterpreter;
    }

    private void configureRuntimeEnvironment() {
        ErrorManager errorManager = new ErrorManager();

        environment.setOutput(outputStream);
        environment.setErrorOutput(errorOutputStream);
        environment.setTerminationFunction(terminationFunction);
        environment.setErrorTerminationFunction(errorTerminationFunction);
        environment.setErrorManager(errorManager);
        environment.setOutputDecorator(outputDecorator);
        environment.setValueOutputDecorator(valueOutputDecorator);
        environment.setWarningOutputDecorator(warningOutputDecorator);
        environment.setErrorOutputDecorator(errorOutputDecorator);
        environment.setCriticalOutputDecorator(criticalOutputDecorator);
        configureInput(errorManager);
    }

    private void configureInput(ErrorManager errorManager) {
        environment.setInputName(inputName);

        try {
            environment.setInput(getInputStream());
        } catch (FileNotFoundException e) {
            errorManager.handle(new LispFileNotFoundException(e));
        }
    }

    private InputStream getInputStream() throws FileNotFoundException {
        return isFileBased ? new FileInputStream(inputName) : inputStream;
    }

    private LispInterpreter createInterpreter() {
        return isInteractive ? new InteractiveLispInterpreter() : new LispInterpreter();
    }

    public static class InterpreterAlreadyBuiltException extends CriticalLispException {

        private static final long serialVersionUID = 1L;

        @Override
        public String getMessage() {
            return "Refusing to build more than one interpreter.";
        }
    }

    public static class LispFileNotFoundException extends CriticalLispException {

        private static final long serialVersionUID = 1L;
        private String message;

        public LispFileNotFoundException(FileNotFoundException e) {
            this.message = e.getMessage();
        }

        @Override
        public String getMessage() {
            return message;
        }
    }

}
