package command;

import cli.CommandLineInterface;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;

public class CommandSelector {

    private CommandLineInterface commandLineInterface;
    private String[] enteredCommand;

    public CommandSelector(String[] enteredCommand) {
        this.enteredCommand = enteredCommand;
        this.commandLineInterface = new CommandLineInterface();

    }

    public void executeCommand() {
        try {
            CommandLine userInput = commandLineInterface.getInput(enteredCommand);
            Command command;
            if (userInput.hasOption("new")) {
                command = createIssue(userInput.getOptionValues("new"));
            } else if (userInput.hasOption("init")) {
                command = init();
            } else if (userInput.hasOption("ls")) {
                command = ls();
            } else if (userInput.hasOption("show")) {
                command = show(userInput.getOptionValue("show"));
            } else if (userInput.hasOption("comment")) {
                command = comment(userInput.getOptionValues("comment"));
            } else {
                command = nullCommand();
            }
            command.execute();

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private Command comment(String[] args) {
        return new CommandComment(args[0], args[1]);
    }

    private Command init() {
        return new CommandInit();
    }

    private Command nullCommand() {
        return new CommandNull();
    }

    private Command createIssue(String[] args) {
        return new CommandCreate(args);
    }

    private Command ls() {
        return new CommandLs();
    }

    private Command show(String id) {
        return new CommandShow(id);
    }

}