package seedu.taskell.logic.commands;

import java.util.ConcurrentModificationException;

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

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

    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 DeleteCommand(int targetIndex) {
        this.targetIndex = targetIndex;
    }


    @Override
    public CommandResult execute() {

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

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

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

        try {
            model.deleteTask(taskToDelete);
            HistoryManager.getInstance().addTask((Task) taskToDelete);
            
            try {
                HistoryManager.getInstance().updateList();
            } catch (ConcurrentModificationException e) {
                HistoryManager.getInstance().updateList();
            }
            
        } catch (TaskNotFoundException tnfe) {
            assert false : "The target task cannot be missing";
        }

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

}

@@ -82,16 +82,95 @@
 eption;%0A
+import java.util.logging.Logger;%0A%0Aimport seedu.taskell.commons.core.LogsCenter;
 %0Aimport 
@@ -600,24 +600,123 @@
  Command %7B%0A%0A
+    private static final Logger logger = LogsCenter.getLogger(DeleteCommand.class.getName());%0A    %0A
     public s
@@ -1863,46 +1863,164 @@
 te);
-%0A            %0A            try %7B%0A      
+           %0A        %7D catch (TaskNotFoundException tnfe) %7B%0A            assert false : %22The target task cannot be missing%22;%0A        %7D%0A        %0A        try %7B%0A
     
@@ -2017,32 +2017,34 @@
 try %7B%0A          
+  
 HistoryManager.g
@@ -2062,36 +2062,32 @@
 ).updateList();%0A
-    
         %7D catch 
@@ -2140,191 +2140,136 @@
     
-    HistoryManager.getInstance().updateList();%0A            %7D%0A            %0A        %7D catch (TaskNotFoundException tnfe) %7B%0A            assert false : %22The target task cannot be missing%22
+logger.severe(%22Concurrent modification exception while updating history%22);%0A            HistoryManager.getInstance().updateList()
 ;%0A  

use try catch