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.DuplicateTaskException;
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 done command";
	public final String MESSAGE_DUPLICATE = "The task is a duplicate of an existing task.";
	public final String MESSAGE_NOT_FOUND = "The task was not found.";
    
    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 : MESSAGE_NOT_FOUND;
        }
        

        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();
		int numberOfTasks = lastShownList.size();
		ReadOnlyTask task = lastShownList.get(numberOfTasks - 1);
		Task taskToAdd = new Task(task.getTitle(), task.getDescription(), task.getStartDate(), task.getDueDate(),
				task.getInterval(), task.getTimeInterval(), task.getStatus(), task.getTags());
		taskToAdd.setStatus(new Status("ONGOING"));
		try {
			model.deleteTask(task);
			model.addTaskWithSpecifiedIndex(taskToAdd, targetIndex - 1);
		} catch (DuplicateTaskException e) {
			return new CommandResult(MESSAGE_DUPLICATE);
		} catch (TaskNotFoundException e) {
			return new CommandResult(MESSAGE_NOT_FOUND);
		}
		return new CommandResult(String.format(MESSAGE_SUCCESS_UNDO));
	}

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

}

@@ -1052,16 +1052,101 @@
 mmand%22;%0A
+%09public static final String MESSAGE_ALREADY_COMPLETED = %22The task is already done.%22;%0A
 %09public 
@@ -1222,24 +1222,24 @@
 ing task.%22;%0A
-
 %09public fina
@@ -1323,24 +1323,56 @@
 argetIndex;%0A
+    public String targetStatus;%0A
     %0A    pub
@@ -1877,24 +1877,197 @@
 ndex - 1);%0A%0A
+        targetStatus= taskToComplete.getStatus().status;%0A        if(targetStatus.equals(%22COMPLETED%22))%0A        %09return new CommandResult(MESSAGE_ALREADY_COMPLETED);%0A        %0A
         try 
@@ -2587,16 +2587,121 @@
 ndo() %7B%0A
+%09%09if(targetStatus.equals(%22COMPLETED%22))%0A%09%09%09return new CommandResult(String.format(MESSAGE_SUCCESS_UNDO));%0A
 %09%09Unmodi
@@ -3112,17 +3112,20 @@
 tus(
-%22ONGOING%22
+targetStatus
 ));%0A
@@ -3469,16 +3469,89 @@
 ));%0A%09%7D%0A%0A
+%09/**%0A%09 * If the task is already COMPLETED, method is not reversible%0A%09 */%0A
 %09@Overri

Fix undo command for done. If the done command is executed and task is already copleted - proper massage for user is shown.