package linenux.control;

import java.util.ArrayList;

import linenux.command.AddCommand;
import linenux.command.Command;
import linenux.command.DeleteCommand;
import linenux.command.DoneCommand;
import linenux.command.ExitCommand;
import linenux.command.HelpCommand;
import linenux.command.InvalidCommand;
import linenux.command.ListCommand;
import linenux.command.UndoCommand;
import linenux.command.result.CommandResult;
import linenux.model.Schedule;

/**
 * Assigns commands based on user input.
 */
public class CommandManager {
    private ArrayList<Command> commandList;
    private Schedule schedule;
    private Command catchAllCommand;

    public CommandManager(Schedule schedule) {
        this.schedule = schedule;
        commandList = new ArrayList<>();
        initializeCommands();
    }

    /**
     * Adds all supported commands to the commandList.
     */
    private void initializeCommands() {
        commandList.add(new ExitCommand());
        commandList.add(new AddCommand(this.schedule));
        commandList.add(new ListCommand(this.schedule));
        commandList.add(new DeleteCommand(this.schedule));
        commandList.add(new DoneCommand(this.schedule));
        commandList.add(new ExitCommand());
        commandList.add(new UndoCommand(this.schedule));
        commandList.add(new HelpCommand(this.commandList));

        this.catchAllCommand = new InvalidCommand(this.commandList);
    }

    /**
     * Assigns the appropriate command to the user input.
     * Contract: only 1 command should be awaiting user response at any point in time.
     */
    public CommandResult delegateCommand(String userInput) {
        for (Command command : commandList) {
            if (command.awaitingUserResponse()) {
                return command.userResponse(userInput);
            }
        }

        for (Command command : commandList) {
            if (command.respondTo(userInput)) {
                return command.execute(userInput);
            }
        }

        return this.catchAllCommand.execute(userInput);
    }

    /**
     * Sets the reference for the schedule.
     */
    private void setSchedule(Schedule schedule) {
        this.schedule = schedule;
    }
}

@@ -1188,52 +1188,8 @@
 ));%0A
-        commandList.add(new ExitCommand());%0A
     

Rebase to master