//@@author A0138474X
package seedu.address.logic.commands;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Description;
import seedu.address.model.task.Event;
import seedu.address.model.task.Name;
import seedu.address.model.task.Priority;
import seedu.address.model.task.ReadOnlyTask;
import seedu.address.model.task.ReadOnlyTask.FinishProperty;
import seedu.address.model.task.Task;
import seedu.address.model.task.TaskDate;
import seedu.address.model.task.TaskTime;
import seedu.address.model.task.UniqueTaskList.DuplicateTaskException;
import seedu.address.model.task.Venue;


/**
 * Deletes a person identified using it's last displayed index from the address book.
 */
public class FinishCommand extends Command {

    public static final String COMMAND_WORD = "finish";

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

    public static final String MESSAGE_FINISH_TASK_SUCCESS = "Mark finished task: %1$s";
    public static final String MESSAGE_FINISH_TASK_MARKED = "Task had already been finished";
    public static final String MESSAGE_WRONG_TASK_INDEX = "This task already exists in the task manager.";

    public final int targetIndex;
    private boolean isSuccess;
    private Task task;

    public FinishCommand(int targetIndex) {
        this.targetIndex = targetIndex;
        this.isSuccess = false;
    }


    @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 taskToMark = lastShownList.get(targetIndex - 1);

        if (taskToMark.isFinished()) {
            throw new CommandException(MESSAGE_FINISH_TASK_MARKED);
        } else {
            Name updatedName = taskToMark.getName();
            TaskDate updatedDueDate = taskToMark.getDate();
            TaskTime updatedDueTime = taskToMark.getTime();
            Description updatedDescription = taskToMark.getDescription();
            Tag updatedTag = taskToMark.getTag();
            Venue updatedVenue = taskToMark.getVenue();
            Priority updatedPriority = taskToMark.getPriority();
            boolean updatedFavorite = taskToMark.isFavorite();
            FinishProperty updatedFinish = FinishProperty.FINISHED;

            if (taskToMark.isEvent()) {
                TaskDate updatedStartDate = ((Event) taskToMark).getStartDate();
                TaskTime updatedStartTime = ((Event) taskToMark).getStartTime();
                try {
                    taskToMark = new Event(updatedName, updatedStartDate, updatedStartTime,
                            updatedDueDate, updatedDueTime, updatedDescription, updatedTag,
                            updatedVenue, updatedPriority, updatedFavorite, updatedFinish);
                } catch (IllegalValueException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                taskToMark  = new Task(
                        updatedName, updatedDueDate, updatedDueTime, updatedDescription,
                        updatedTag, updatedVenue, updatedPriority, updatedFavorite, updatedFinish);
            }
        }

        try {
            model.updateTask(targetIndex - 1, taskToMark);
            task = (Task) taskToMark;
            isSuccess = true;
        } catch (DuplicateTaskException e) {
            this.isSuccess = false;
            throw new CommandException(MESSAGE_WRONG_TASK_INDEX);
        }

        return new CommandResult(String.format(MESSAGE_FINISH_TASK_SUCCESS, taskToMark));
    }

    @Override
    public boolean isUndoable() {
        return false;
    }
}


@@ -741,32 +741,102 @@
 eTaskException;%0A
+import seedu.address.model.task.UniqueTaskList.TaskNotFoundException;%0A
 import seedu.add
@@ -858,16 +858,16 @@
 .Venue;%0A
-
 %0A%0A/**%0A *
@@ -1697,16 +1697,47 @@
 uccess;%0A
+    private boolean isDeleted;%0A
     priv
@@ -2242,16 +2242,65 @@
 ex - 1);
+%0A        %0A        ReadOnlyTask editedTask = null;
 %0A%0A      
@@ -3211,25 +3211,25 @@
         
-taskToMar
+editedTas
 k = new 
@@ -3670,25 +3670,25 @@
         
-taskToMar
+editedTas
 k  = new
@@ -3968,25 +3968,25 @@
 ex - 1, 
-taskToMar
+editedTas
 k);%0A    
@@ -4007,25 +4007,25 @@
  (Task) 
-taskToMar
+editedTas
 k;%0A     
@@ -4147,60 +4147,191 @@
    t
-hrow new CommandException(MESSAGE_WRONG_TASK_INDEX);
+ry %7B%0A                model.deleteTask(taskToMark);%0A            %7D catch (TaskNotFoundException e1) %7B%0A                assert false : %22The target person cannot be missing%22;%0A            %7D
 %0A   
@@ -4498,20 +4498,19 @@
  return 
-fals
+tru
 e;%0A    %7D
@@ -4510,11 +4510,12 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A%0A

edit Finish command to detect duplicate finish task and delete the task to be mark