package seedu.ezdo.logic.commands;

import java.util.ArrayList;

import seedu.ezdo.commons.core.EventsCenter;
import seedu.ezdo.commons.core.Messages;
import seedu.ezdo.commons.core.UnmodifiableObservableList;
import seedu.ezdo.commons.events.ui.JumpToListRequestEvent;
import seedu.ezdo.commons.util.MultipleIndexCommandUtil;
import seedu.ezdo.logic.commands.exceptions.CommandException;
import seedu.ezdo.model.todo.ReadOnlyTask;
import seedu.ezdo.model.todo.Task;

//@@author A0139177W
/**
 * Selects a task identified using its last displayed index from ezDo.
 */
public class SelectCommand extends Command {

    public static final String COMMAND_WORD = "select";

    public static final String MESSAGE_USAGE = COMMAND_WORD
            + ": Selects the task identified by the index number used in the last task listing.\n"
            + "Parameters: INDEX (must be a positive integer)\n" + "Example: " + COMMAND_WORD + " 1";

    public static final String MESSAGE_SELECT_TASK_SUCCESS = "Selected Task: %1$s";
    private final ArrayList<Integer> targetIndexes;
    private final ArrayList<Task> tasksToToggle;

    public SelectCommand(ArrayList<Integer> indexes) {
        this.targetIndexes = new ArrayList<Integer>(indexes);
        this.tasksToToggle = new ArrayList<Task>();
    }

    @Override
    public CommandResult execute() throws CommandException {

        UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

        if (!MultipleIndexCommandUtil.isIndexValid(lastShownList, targetIndexes)) {
            throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

        if (!MultipleIndexCommandUtil.isDone(lastShownList, targetIndexes)) {
            throw new CommandException(Messages.MESSAGE_TASK_DONE);
        }

        MultipleIndexCommandUtil.addTasksToList(tasksToToggle, lastShownList, targetIndexes);
        model.toggleTasksSelect(tasksToToggle);

        int offset = 1;
        int lastIndex = targetIndexes.size() - offset;
        int lastElementInTargetIndexes = targetIndexes.get(lastIndex);

        JumpToListRequestEvent scrollToTask = new JumpToListRequestEvent(lastElementInTargetIndexes - offset);
        EventsCenter.getInstance().post(scrollToTask);

        return new CommandResult(String.format(MESSAGE_SELECT_TASK_SUCCESS, targetIndexes));

    }

}

@@ -1109,18 +1109,163 @@
 ToToggle
-;%0A
+ = new ArrayList%3CTask%3E();%0A%0A%0A    /**%0A     * Creates an arraylist of targetIndexes based on the total indexes quantity.%0A     * @param indexes%0A     */
 %0A    pub
@@ -1382,62 +1382,62 @@
     
+%7D%0A%0A
     
-this.tasksToToggle = new ArrayList%3CTask%3E();%0A
+/**%0A     * Executes the Select command.%0A 
     
-%7D%0A
+*/
 %0A   
@@ -1616,180 +1616,80 @@
     
-if (!MultipleIndexCommandUtil.isIndexValid(lastShownList, targetIndexes)) %7B%0A            throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);%0A        %7D
+checkValidIndexes(lastShownList);%0A        checkTasksDone(lastShownList);
 %0A%0A  
@@ -1698,13 +1698,8 @@
     
-if (!
 Mult
@@ -1723,15 +1723,38 @@
 til.
-isDone(
+addTasksToList(tasksToToggle, 
 last
@@ -1778,19 +1778,17 @@
 Indexes)
-) %7B
+;
 %0A       
@@ -1792,76 +1792,57 @@
     
-    throw new CommandException(Messages.MESSAGE_TASK_DONE
+model.toggleTasksSelect(tasksToToggle
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -1841,87 +1841,104 @@
     
-%7D%0A%0A        MultipleIndexCommandUtil.addTasksToList(tasksToToggle, lastShownList
+scrollToFinalTask();%0A%0A        return new CommandResult(String.format(MESSAGE_SELECT_TASK_SUCCESS
 , ta
@@ -1953,58 +1953,112 @@
 xes)
-;
+);%0A
 %0A    
+%7D%0A%0A
     
-model.toggleTasksSelect(tasksToToggle);%0A
+/** Scroll to the last task which was updated. **/%0A    private void scrollToFinalTask() %7B
 %0A   
@@ -2375,108 +2375,789 @@
 k);%0A
-%0A        return new CommandResult(String.format(MESSAGE_SELECT_TASK_SUCCESS, targetIndexes));%0A%0A    %7D%0A%0A%7D
+    %7D%0A%0A    /** checks if the tasks with the indexes specified are marked as done */%0A    private void checkTasksDone(UnmodifiableObservableList%3CReadOnlyTask%3E lastShownList) throws CommandException %7B%0A        if (!MultipleIndexCommandUtil.isDone(lastShownList, targetIndexes)) %7B%0A            throw new CommandException(Messages.MESSAGE_TASK_DONE);%0A        %7D%0A    %7D%0A%0A    /** checks if the indexes specified are all smaller than the size of the list and not 0 i.e. valid */%0A    private void checkValidIndexes(UnmodifiableObservableList%3CReadOnlyTask%3E lastShownList) throws CommandException %7B%0A        if (!MultipleIndexCommandUtil.isIndexValid(lastShownList, targetIndexes)) %7B%0A            throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);%0A        %7D%0A    %7D%0A%0A%7D%0A//@@author
 %0A

Add comments for SelectCommand methods