package org.implab.gradle.common.exec;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;

import java.io.File;
import java.io.IOException;

/** Command line builder */
@NonNullByDefault
public abstract class ExecBuilder {

    private final List<String> commandLine = new ArrayList<>();

    private final Map<String, String> environment = new HashMap<>();

    private File directory;

    private RedirectFrom inputRedirect;

    private RedirectTo outputRedirect;

    private RedirectTo errorRedirect;

    /** Sets command line, clears previous one if any */
    public void command(String cmd, String... args) {
        commandLine.clear();
        commandLine.add(cmd);
        Stream.of(args).forEach(commandLine::add);
    }

    /** Adds an argument to the command line */
    public void argument(String arg) {
        Objects.requireNonNull(arg, "arg parameter can't be null");
        commandLine.add(arg);
    }

    /** Sets the working directory */
    public void directory(File directory) {
        Objects.requireNonNull(directory, "directory parameter can't be null");
        this.directory = directory;
    }

    /**
     * Sets the environment value. The value cannot be null.
     * 
     * @param envVar The name of the environment variable
     * @param value  The value to set,
     */
    public void setEnvironment(String envVar, String value) {
        Objects.requireNonNull(value, "Value can't be null");
        Objects.requireNonNull(envVar, "envVar parameter can't be null");
        environment.put(envVar, value);
    }

    public void unsetEnvironment(String envVar) {
        environment.remove(envVar);
    }

    /**
     * Sets redirection for the stdin, {@link RedirectFrom} will be applied
     * every time the process is started.
     * 
     * <p>
     * If redirection
     */
    public void stdin(RedirectFrom from) {
        Objects.requireNonNull(from, "from parameter can't be null");
        inputRedirect = from;
    }

    /**
     * Sets redirection for the stdout, {@link RedirectTo} will be applied
     * every time the process is started.
     */
    public void stdout(RedirectTo out) {
        Objects.requireNonNull(out, "out parameter can't be null");
        outputRedirect = out;
    }

    /**
     * Sets redirection for the stderr, {@link RedirectTo} will be applied
     * every time the process is started.
     */
    public void stderr(RedirectTo err) {
        Objects.requireNonNull(err, "err parameter can't be null");
        errorRedirect = err;
    }

    /** Implement this function to  */
    protected abstract CompletableFuture<Integer> startInternal(
            Map<String, String> environment,
            File directory,
            List<String> commandLine,
            Optional<RedirectFrom> stdinRedirect,
            Optional<RedirectTo> stdoutRedirect,
            Optional<RedirectTo> stderrRedirect) throws IOException;

    /**
     * Creates and starts new process and returns {@link CompletableFuture}. The
     * process may be a remote process, or a shell process or etc.
     * 
     * @return
     * @throws IOException
     */
    public CompletableFuture<Integer> start() throws IOException {
        return startInternal(
                environment,
                directory,
                commandLine,
                Optional.ofNullable(inputRedirect),
                Optional.ofNullable(outputRedirect),
                Optional.ofNullable(errorRedirect));
    }

}
