package seedu.address.logic.commands;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.UnmodifiableObservableList;
import seedu.address.model.task.*;
import seedu.address.model.task.UniqueTaskList.TaskNotFoundException;
import seedu.address.ui.PersonListPanel;

/**
 * Sets as completed a task identified using it's last displayed index from the address book.
 */
public class DoneCommand extends Command {

    public final int targetIndex;

    public static final String COMMAND_WORD = "done";

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

    public static final String MESSAGE_DONE_TASK_SUCCESS = "Completed Task: %1$s";

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

    @Override
    public CommandResult execute() {

        UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredDatedTaskList();
        UnmodifiableObservableList<ReadOnlyTask> lastUndatedTaskList = model.getFilteredUndatedTaskList();
 
        if ((targetIndex <= PersonListPanel.DATED_DISPLAY_INDEX_OFFSET 
                && lastUndatedTaskList.size() < targetIndex)  // index <= 10 && index > size of list
           || (targetIndex > PersonListPanel.DATED_DISPLAY_INDEX_OFFSET  // index > 10 && index - 10 > size of list 
                   && lastShownList.size() < targetIndex - PersonListPanel.DATED_DISPLAY_INDEX_OFFSET)) {
            indicateAttemptToExecuteIncorrectCommand();
            return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
        }

        ReadOnlyTask readTaskToComplete;
        if (targetIndex > PersonListPanel.DATED_DISPLAY_INDEX_OFFSET) {
            readTaskToComplete = lastShownList.get(targetIndex - 1 - PersonListPanel.DATED_DISPLAY_INDEX_OFFSET);
        }
        else {
            readTaskToComplete = lastUndatedTaskList.get(targetIndex - 1);
        }
        
        if (!readTaskToComplete.getStatus().equals(new Status(Status.State.DONE))){
            try {
                model.completeTask(readTaskToComplete);
            } catch (TaskNotFoundException pnfe) {
                assert false : "The target task cannot be found";
            }
        }
        
        //TODO look at posting a set as completed event.
        //EventsCenter.getInstance().post(new JumpToListRequestEvent(targetIndex - 1));
        return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, readTaskToComplete));

    }

}

@@ -889,16 +889,105 @@
 : %251$s%22;
+%0A    public static final String MESSAGE_TASK_ALREADY_DONE = %22Task is already completed.%22;
 %0A%0A    pu
@@ -2518,34 +2518,230 @@
   %7D%0A        
-%7D%0A        
+    return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, readTaskToComplete));%0A        %7D%0A        else %7B%0A            return new CommandResult(String.format(MESSAGE_TASK_ALREADY_DONE));%0A        %7D
 %0A        //T
@@ -2874,24 +2874,24 @@
  - 1));%0A
+
         
 return n
@@ -2886,95 +2886,8 @@
     
-return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, readTaskToComplete));
 %0A%0A  

Add message in DoneCommand to inform user that target task to complete was already completed