package seedu.taskboss.logic.commands;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

import seedu.taskboss.commons.core.EventsCenter;
import seedu.taskboss.commons.core.Messages;
import seedu.taskboss.commons.core.UnmodifiableObservableList;
import seedu.taskboss.commons.events.ui.JumpToListRequestEvent;
import seedu.taskboss.commons.exceptions.IllegalValueException;
import seedu.taskboss.logic.commands.exceptions.CommandException;
import seedu.taskboss.model.task.ReadOnlyTask;

//@@author A0144904H
public class TerminateCommand extends Command {

    private static final int INDEX_ZERO = 0;
    public static final String COMMAND_WORD = "terminate";
    public static final String COMMAND_WORD_SHORT = "t";

    public static final String MESSAGE_USAGE = COMMAND_WORD + "/" + COMMAND_WORD_SHORT
            + ": Marks the recurring tasks identified by the index"
            + " numbers provided that are used in the last listing as done. "
            + "Parameters: LIST OF INDEXES (must be a positive integers)\n"
            + "Example: " + COMMAND_WORD
            + " 1 2 3" +  " || " + COMMAND_WORD_SHORT + " 1";

    public static final String MESSAGE_MARK_RECURRING_TASK_DONE_SUCCESS = "recurring tasks marked done: %1$s";
    public static final String DONE = "Done";
    public static final String ERROR_TASK_NOT_RECURRING = "All tasks indicated should be recurring";

    public final ArrayList<Integer> filteredTaskListIndices;
    public final ArrayList<ReadOnlyTask> recurringTasksToMarkDone;

    public TerminateCommand(Set<Integer> targetIndex) {
        filteredTaskListIndices = new ArrayList<Integer>(targetIndex);
        Collections.sort(filteredTaskListIndices);
        recurringTasksToMarkDone = new ArrayList<ReadOnlyTask>();
    }

    @Override
    public CommandResult execute() throws CommandException, IllegalValueException {
        UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

        if (filteredTaskListIndices.get(filteredTaskListIndices.size() - 1) > lastShownList.size()
                || filteredTaskListIndices.get(INDEX_ZERO) < 1) {
            throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

        for (int index : filteredTaskListIndices) {
            ReadOnlyTask recurringTaskToMarkDone = lastShownList.get(index - 1);
            recurringTasksToMarkDone.add(recurringTaskToMarkDone);
        }

        model.end(filteredTaskListIndices, recurringTasksToMarkDone);

        scrollToTask(recurringTasksToMarkDone);
        return new CommandResult(String.format(MESSAGE_MARK_RECURRING_TASK_DONE_SUCCESS, recurringTasksToMarkDone));
    }

    /**
     * Scrolls to the position of the task with the lowest index
     * in the mark done list
     */
    private void scrollToTask(ArrayList<ReadOnlyTask> recurringTasksToMarkDone) {
        ReadOnlyTask recurringTaskToMarkDone = recurringTasksToMarkDone.get(INDEX_ZERO);
        UnmodifiableObservableList<ReadOnlyTask> latestShownList = model.getFilteredTaskList();
        int targetIndex = latestShownList.indexOf(recurringTaskToMarkDone);
        EventsCenter.getInstance().post(new JumpToListRequestEvent(targetIndex));
    }

}

@@ -457,32 +457,79 @@
 mmandException;%0A
+import seedu.taskboss.model.category.Category;%0A
 import seedu.tas
@@ -559,16 +559,16 @@
 lyTask;%0A
-
 %0A//@@aut
@@ -1462,16 +1462,109 @@
 urring%22;
+%0A    public static final String ERROR_TERMINATED_TASK = %22The task was terminated previously%22;
 %0A%0A    pu
@@ -2441,24 +2441,24 @@
 tIndices) %7B%0A
-
             
@@ -2526,16 +2526,180 @@
 x - 1);%0A
+            if (recurringTaskToMarkDone.getCategories().contains(Category.done)) %7B%0A                throw new CommandException(ERROR_TERMINATED_TASK);%0A            %7D%0A
         

fix terminating a terminated task bug