package seedu.tache.logic.commands;

import seedu.tache.commons.core.Messages;
import seedu.tache.commons.core.UnmodifiableObservableList;
import seedu.tache.logic.commands.exceptions.CommandException;
import seedu.tache.model.task.ReadOnlyTask;
import seedu.tache.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * Deletes a task identified using it's last displayed index from the task manager.
 */
public class DeleteCommand extends Command {

    public enum TaskType { TypeTask, TypeDetailedTask }

    public static final String COMMAND_WORD = "delete";

    public static final String MESSAGE_USAGE = COMMAND_WORD
            + ": Deletes 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_DELETE_TASK_SUCCESS = "Deleted Task: %1$s";

    public final int targetIndex;
    public final TaskType type;

    public DeleteCommand(int targetIndex) {
        this.targetIndex = targetIndex;
        this.type = TaskType.TypeTask;
    }

    public DeleteCommand(int targetIndex, TaskType type) {
        this.targetIndex = targetIndex;
        this.type = type;
    }


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

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

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

        try {
            model.deleteTask(taskToDelete);
        } catch (TaskNotFoundException tnfe) {
            assert false : "The target task cannot be missing";

        }

        return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, taskToDelete));

    }

}

@@ -239,16 +239,121 @@
 lyTask;%0A
+import seedu.tache.model.task.Task;%0Aimport seedu.tache.model.task.UniqueTaskList.DuplicateTaskException;%0A
 import s
@@ -548,16 +548,36 @@
 Command 
+implements Undoable 
 %7B%0A%0A    p
@@ -1046,73 +1046,247 @@
 s%22;%0A
-%0A    public final int targetIndex;%0A    public final TaskType type
+    public static final String MESSAGE_DUPLICATE_TASK = %22%251$s already exists in the task manager%22;%0A%0A    public final int targetIndex;%0A    public final TaskType type;%0A    private ReadOnlyTask taskToDelete;%0A    private boolean commandSuccess
 ;%0A%0A 
@@ -1407,16 +1407,48 @@
 peTask;%0A
+        commandSuccess = false;%0A
     %7D%0A%0A 
@@ -1567,24 +1567,56 @@
 ype = type;%0A
+        commandSuccess = false;%0A
     %7D%0A%0A%0A    
@@ -1937,29 +1937,16 @@
         
-ReadOnlyTask 
 taskToDe
@@ -2047,16 +2047,87 @@
 elete);%0A
+            commandSuccess = true;%0A            undoHistory.push(this);%0A
         
@@ -2341,10 +2341,483 @@
 %0A    %7D%0A%0A
+    //@@author A0150120H%0A    @Override%0A    public boolean isUndoable() %7B%0A        return commandSuccess;%0A    %7D%0A%0A    @Override%0A    public String undo() throws CommandException %7B%0A        try %7B%0A            model.addTask(new Task(taskToDelete));%0A            return String.format(MESSAGE_DELETE_TASK_SUCCESS, taskToDelete);%0A        %7D catch (DuplicateTaskException e) %7B%0A            throw new CommandException(String.format(MESSAGE_DUPLICATE_TASK, taskToDelete));%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Update DeleteCommand to support Undo