package seedu.task.logic.commands;

import seedu.task.commons.core.Messages;
import seedu.task.commons.core.UnmodifiableObservableList;
import seedu.task.model.task.ReadOnlyTask;
import seedu.task.model.task.UniqueTaskList.TaskNotFoundException;
import seedu.task.model.task.Status;
import seedu.task.model.task.Task;

/**
 * Mark a task as completed which is identified using it's last displayed index from the task manager.
 */
public class DoneCommand extends Command {
    
    public static final String COMMAND_WORD = "done";

    public static final String MESSAGE_USAGE = COMMAND_WORD
            + ": Mark a task as completed which is 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_COMPLETED_TASK_SUCCESS = "Completed Task: %1$s";
	public static final String MESSAGE_SUCCESS_UNDO = "Undo of delete command";
    
    public int targetIndex;
    
    public DoneCommand(int targetIndex) {
        this.targetIndex = targetIndex;
    }
    
    @Override
    public CommandResult execute() {

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

        if (lastShownList.size() < targetIndex) {
            indicateAttemptToExecuteIncorrectCommand();
            return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

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

        try {
            ReadOnlyTask completedTask = taskToComplete;
            completedTask.setStatus(new Status("COMPLETED"));
            model.completeTask(taskToComplete, completedTask);
            
        } catch (TaskNotFoundException pnfe) {
            assert false : "The target task cannot be missing";
        }
        

        return new CommandResult(String.format(MESSAGE_COMPLETED_TASK_SUCCESS, taskToComplete));
    }

    /**
     * Assume that done task is at the end of list
     */
	@Override
	public CommandResult executeUndo() {
		UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();
		this.targetIndex = lastShownList.size()-1;
		return this.execute();
	}


	@Override
	public boolean isReversible() {
		return true;
	}

}

@@ -172,16 +172,84 @@
 lyTask;%0A
+import seedu.task.model.task.UniqueTaskList.DuplicateTaskException;%0A
 import s
@@ -299,32 +299,32 @@
 FoundException;%0A
-
 import seedu.tas
@@ -1042,21 +1042,176 @@
 of d
-elete comma
+one command%22;%0A%09public final String MESSAGE_DUPLICATE = %22The task is a duplicate of an existing task.%22;%0A%09public final String MESSAGE_NOT_FOUND = %22The task was not fou
 nd
+.
 %22;%0A 
@@ -2051,43 +2051,25 @@
 e : 
-%22The target task cannot be missing%22
+MESSAGE_NOT_FOUND
 ;%0A  
@@ -2194,20 +2194,14 @@
  %7D%0A%0A
-    /**%0A    
+%09/**%0A%09
  * A
@@ -2243,20 +2243,17 @@
 of list%0A
-    
+%09
  */%0A%09@Ov
@@ -2387,32 +2387,33 @@
 st();%0A%09%09
-this.targetIndex
+int numberOfTasks
  = lastS
@@ -2431,40 +2431,650 @@
 ze()
--1;%0A%09%09return this.execute(
+;%0A%09%09ReadOnlyTask task = lastShownList.get(numberOfTasks - 1);%0A%09%09Task taskToAdd = new Task(task.getTitle(), task.getDescription(), task.getStartDate(), task.getDueDate(),%0A%09%09%09%09task.getInterval(), task.getTimeInterval(), task.getStatus(), task.getTags());%0A%09%09taskToAdd.setStatus(new Status(%22ONGOING%22));%0A%09%09try %7B%0A%09%09%09model.deleteTask(task);%0A%09%09%09model.addTaskWithSpecifiedIndex(taskToAdd, targetIndex - 1);%0A%09%09%7D catch (DuplicateTaskException e) %7B%0A%09%09%09return new CommandResult(MESSAGE_DUPLICATE);%0A%09%09%7D catch (TaskNotFoundException e) %7B%0A%09%09%09return new CommandResult(MESSAGE_NOT_FOUND
 );%0A
+%09
 %09%7D%0A
+%09%09return new CommandResult(String.format(MESSAGE_SUCCESS_UNDO));%0A%09%7D
 %0A%0A%09@

Fix undo for done command.