package seedu.ezdo.logic.commands;

import java.util.ArrayList;

import seedu.ezdo.commons.core.Messages;
import seedu.ezdo.commons.core.UnmodifiableObservableList;
import seedu.ezdo.logic.commands.exceptions.CommandException;
import seedu.ezdo.model.todo.ReadOnlyTask;
import seedu.ezdo.model.todo.Task;
//@@author A0141010L
/**
 * Marks a task as identified using its last displayed index from ezDo as done
 */
public class DoneCommand extends Command implements MultipleIndexCommand {

    public static final String COMMAND_WORD = "done";
    public static final String SHORT_COMMAND_WORD = "d";

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

    public static final String MESSAGE_DONE_TASK_SUCCESS = "Done task: %1$s";
    public static final String MESSAGE_UNDONE_TASK_SUCCESS = "Undone task: %1$s";
    public static final String MESSAGE_DONE_LISTED = "Done tasks listed";

    private final ArrayList<Integer> targetIndexes;
    private final ArrayList<Task> tasksToToggle;
    private final boolean requestToViewDoneOnly;

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

    public DoneCommand() {
        this.targetIndexes = null;
        this.requestToViewDoneOnly = true;
        this.tasksToToggle = null;
    }


    @Override
    public CommandResult execute() throws CommandException {

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

        if (requestToViewDoneOnly) {
            model.updateFilteredDoneList();
            return new CommandResult(MESSAGE_DONE_LISTED);
        }

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

        for (int i = 0; i < targetIndexes.size(); i++) {
            Task taskToToggle = (Task) lastShownList.get(targetIndexes.get(i) - 1);
            tasksToToggle.add(taskToToggle);
        }

        boolean isDone = model.toggleTasksDone(tasksToToggle);

        if (isDone) {
            return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, tasksToToggle));
        } else {
            return new CommandResult(String.format(MESSAGE_UNDONE_TASK_SUCCESS, tasksToToggle));
        }
    }

  //@@author A0139248X
    @Override
    public boolean isIndexValid(UnmodifiableObservableList<ReadOnlyTask> lastShownList) {
        return targetIndexes.stream().allMatch(index -> index <= lastShownList.size() && index != 0);
    }
}

@@ -158,16 +158,73 @@
 leList;%0A
+import seedu.ezdo.commons.util.MultipleIndexCommandUtil;%0A
 import s
@@ -277,16 +277,16 @@
 eption;%0A
-
 import s
@@ -508,40 +508,8 @@
 and 
-implements MultipleIndexCommand 
 %7B%0A%0A 
@@ -1649,17 +1649,37 @@
 %0A    %7D%0A%0A
+//@@author A0139248X
 %0A
-
     @Ove
@@ -2005,16 +2005,41 @@
    if (!
+MultipleIndexCommandUtil.
 isIndexV
@@ -2056,16 +2056,31 @@
 hownList
+, targetIndexes
 )) %7B%0A   
@@ -2658,32 +2658,32 @@
 asksToToggle));%0A
+
         %7D%0A    %7D%0A
@@ -2685,243 +2685,7 @@
    %7D
-%0A%0A  //@@author A0139248X%0A    @Override%0A    public boolean isIndexValid(UnmodifiableObservableList%3CReadOnlyTask%3E lastShownList) %7B%0A        return targetIndexes.stream().allMatch(index -%3E index %3C= lastShownList.size() && index != 0);%0A    %7D
 %0A%7D%0A

Change DoneCommand to use util method