package seedu.taskell.logic.commands;

import java.util.ArrayList;
import java.util.logging.Logger;

import seedu.taskell.commons.core.LogsCenter;
import seedu.taskell.model.task.ReadOnlyTask;
import seedu.taskell.model.task.Task;
import seedu.taskell.model.task.UniqueTaskList.DuplicateTaskException;
import seedu.taskell.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * Undo most recent executed command
 */
public class UndoCommand extends Command {
    private static final Logger logger = LogsCenter.getLogger(UndoCommand.class.getName());
    
    public static final String COMMAND_WORD = "undo";
    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Undo most recent command.\n"
            + "Example: " + COMMAND_WORD;
    
    public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted Task: %1$s";
    public static final String MESSAGE_ADD_TASK_SUCCESS = "Task added back: %1$s";
    public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task manager";
    public static final String MESSAGE_NO_TASK_TO_UNDO = "No add or delete commands available to undo.";

    private static ArrayList<String> commandHistory;
    
    private static String mostRecentCommand;
    private static Task mostRecentAddedTask;
    private static Task mostRecentDeletedTask;
    
    public UndoCommand() {
        
    }
    
    @Override
    public CommandResult execute() {
        if (mostRecentCommand==null) {
            return new CommandResult(String.format(MESSAGE_NO_TASK_TO_UNDO));
        }
        switch (mostRecentCommand) {
        case AddCommand.COMMAND_WORD:
            try {
                model.deleteTask(mostRecentAddedTask);
            } catch (TaskNotFoundException e) {
                assert false : "The target task cannot be missing";
            }
            return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, mostRecentAddedTask));
        case DeleteCommand.COMMAND_WORD:
            try {
                model.addTask(mostRecentDeletedTask);
                return new CommandResult(String.format(MESSAGE_ADD_TASK_SUCCESS, mostRecentDeletedTask));
            } catch (DuplicateTaskException e) {
                return new CommandResult(MESSAGE_DUPLICATE_TASK);
            }
        default:
            return new CommandResult(String.format(MESSAGE_NO_TASK_TO_UNDO));
        }
    }
    
    public static void updateMostRecentCommand(String commandWord) {
        mostRecentCommand = commandWord;
    }
    
    public static void updateMostRecentAddedTask(Task task) {
        mostRecentAddedTask = task;
    }
    
    public static void updateMostRecentDeletedTask(ReadOnlyTask task) {
        mostRecentDeletedTask = (Task) task;
    }
    
    public static void initializeCommandHistory() {
        if (commandHistory==null) {
            commandHistory = new ArrayList<>();
        }
    }
    
    public static void addCommandToHistory(String command) {
        commandHistory.add(command.trim());
    }

}

@@ -1638,183 +1638,98 @@
     
-try %7B%0A                model.deleteTask(mostRecentAddedTask);%0A            %7D catch (TaskNotFoundException e) %7B%0A                assert false : %22The target task cannot be missing%22
+return undoAdd();%0A        case DeleteCommand.COMMAND_WORD:%0A            return undoDelete()
 ;%0A  
@@ -1726,37 +1726,40 @@
 lete();%0A        
-    %7D
+default:
 %0A            ret
@@ -1806,97 +1806,85 @@
 AGE_
-DELETE_TASK_SUCCESS, mostRecentAddedTask));%0A        case DeleteCommand.COMMAND_WORD:%0A    
+NO_TASK_TO_UNDO));%0A        %7D%0A    %7D%0A%0A    private CommandResult undoDelete() %7B%0A
     
@@ -1893,20 +1893,16 @@
   try %7B%0A
-    
         
@@ -1947,36 +1947,32 @@
 k);%0A            
-    
 return new Comma
@@ -2053,20 +2053,16 @@
         
-    
 %7D catch 
@@ -2094,36 +2094,32 @@
 ) %7B%0A            
- 
-   
 return new Comma
@@ -2164,35 +2164,238 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-    default:%0A    
+private CommandResult undoAdd() %7B%0A        try %7B%0A            model.deleteTask(mostRecentAddedTask);%0A        %7D catch (TaskNotFoundException e) %7B%0A            assert false : %22The target task cannot be missing%22;%0A        %7D%0A
     
@@ -2449,36 +2449,51 @@
 AGE_
-NO_TASK_TO_UNDO));%0A        %7D
+DELETE_TASK_SUCCESS, mostRecentAddedTask));
 %0A   

refactor code