package seedu.tasklist.logic.commands;

import java.util.List;

import seedu.tasklist.commons.core.Messages;
import seedu.tasklist.logic.commands.exceptions.CommandException;
import seedu.tasklist.model.task.ReadOnlyTask;
import seedu.tasklist.model.task.Task;
import seedu.tasklist.model.task.UniqueTaskList;

/**
 * Marks a task as done using its last displayed index from FlexiTask
 * @author A0143355J
 */
public class DoneCommand extends Command {

    public static final String COMMAND_WORD = "done";

    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 final int targetIndex;

    public DoneCommand(int targetIndex) {
        this.targetIndex = targetIndex;
    }

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

        if (lastShownList.size() < targetIndex) {
            throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

        ReadOnlyTask taskToDone = lastShownList.get(targetIndex - 1);

        Task doneTask;

        doneTask = createDoneTask(taskToDone);

        model.updateTask(targetIndex, doneTask);

        model.updateFilteredListToShowAll();
        return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, taskToDone));
        return null;
    }

}

@@ -102,16 +102,80 @@
 ssages;%0A
+import seedu.tasklist.commons.exceptions.IllegalValueException;%0A
 import s
@@ -265,28 +265,369 @@
 el.task.
-ReadOnlyTask
+DeadlineTask;%0Aimport seedu.tasklist.model.task.EventTask;%0Aimport seedu.tasklist.model.task.FloatingTask;%0Aimport seedu.tasklist.model.task.ReadOnlyDeadlineTask;%0Aimport seedu.tasklist.model.task.ReadOnlyEventTask;%0Aimport seedu.tasklist.model.task.ReadOnlyFloatingTask;%0Aimport seedu.tasklist.model.task.ReadOnlyTask;%0Aimport seedu.tasklist.model.task.Status
 ;%0Aimport
@@ -706,16 +706,39 @@
 TaskList
+.DuplicateTaskException
 ;%0A%0A/**%0A 
@@ -1287,16 +1287,103 @@
 : %251$s%22;
+%0A    public static final String MESSAGE_DONE_ERROR = %22Task is already marked as done!%22;
 %0A%0A    pu
@@ -1891,16 +1891,34 @@
 eTask;%0A%0A
+        try %7B%0A    
         
@@ -1960,57 +1960,317 @@
 e);%0A
-%0A        model.updateTask(targetIndex, doneTask);
+        %7D catch (IllegalValueException ive) %7B%0A            throw new CommandException(ive.getMessage());%0A        %7D%0A%0A        try %7B%0A            model.updateTask(targetIndex, doneTask);%0A        %7D catch (DuplicateTaskException e) %7B%0A            //Task should never be a duplicate%0A            assert false;%0A        %7D
 %0A%0A  
@@ -2408,24 +2408,1259 @@
     
+%7D%0A%0A
     
-return null;
+/**%0A     * Creates and returns a %7B@code Task%7D with the details of %7B@code taskToDone%7D%0A     * edited with status as COMPLETED.%0A     * @throws IllegalValueException%0A     */%0A    private Task createDoneTask(ReadOnlyTask taskToDone) throws IllegalValueException %7B%0A        assert taskToDone != null;%0A%0A        Status status = taskToDone.getStatus();%0A        if (status.checkStatus() == Status.COMPLETED) %7B%0A            throw new IllegalValueException(MESSAGE_DONE_ERROR);%0A        %7D%0A%0A        String type = taskToDone.getType();%0A        switch (type) %7B%0A        case FloatingTask.TYPE:%0A            FloatingTask doneFloatingTask = new FloatingTask((ReadOnlyFloatingTask) taskToDone);%0A            doneFloatingTask.getStatus().setCompleted();%0A            return doneFloatingTask;%0A%0A        case DeadlineTask.TYPE:%0A            DeadlineTask doneDeadlineTask = new DeadlineTask((ReadOnlyDeadlineTask) taskToDone);%0A            doneDeadlineTask.getStatus().setCompleted();%0A            return doneDeadlineTask;%0A%0A        case EventTask.TYPE:%0A            EventTask doneEventTask = new EventTask((ReadOnlyEventTask) taskToDone);%0A            doneEventTask.getStatus().setCompleted();%0A            return doneEventTask;%0A%0A        default:%0A            return null;%0A        %7D
 %0A   

update execute in DoneCommand