package seedu.todo.controllers;

import java.util.HashMap;
import java.util.Map;

import seedu.todo.commons.exceptions.UnmatchedQuotesException;
import seedu.todo.models.TodoListDB;
import seedu.todo.ui.UiManager;
import seedu.todo.ui.views.IndexView;

public class UndoController implements Controller {
    
    private static final String MESSAGE_SUCCESS = "Successfully undid last command!\nTo redo, type \"redo\".";
    private static final String MESSAGE_FAILURE = "There is no command to undo!";

    @Override
    public float inputConfidence(String input) {
        return input.startsWith("undo") ? 1 : 0;
    }
    
    private static Map<String, String[]> getTokenDefinitions() {
        Map<String, String[]> tokenDefinitions = new HashMap<String, String[]>();
        tokenDefinitions.put("default", new String[] {"undo"});
        return tokenDefinitions;
    }

    @Override
    public void process(String input) {
        
        Map<String, String[]> parsedResult;
        try {
            parsedResult = Tokenizer.tokenize(getTokenDefinitions(), input);            
        } catch (UnmatchedQuotesException e) {
            System.out.println("Unmatched quote!");
            return;
        }
        
        int numUndo = 1;
        if (parsedResult.get("default") != null) {
            numUndo = Integer.parseInt(parsedResult.get("default")[1]);
        }   
        
        // We don't really have a nice way to support SQL transactions, so yeah >_<
        TodoListDB db = TodoListDB.getInstance();
        for (int i = 0; i < numUndo; i++) {
            if (!db.undo()) {
                UiManager.updateConsoleMessage(MESSAGE_FAILURE);
                return;
            }
        }
        db = TodoListDB.getInstance();
        
        // Render
        IndexView view = UiManager.loadView(IndexView.class);
        view.tasks = db.getAllTasks();
        view.events = db.getAllEvents();
        UiManager.renderView(view);
        
        // Update console message
        UiManager.updateConsoleMessage(MESSAGE_SUCCESS);
    }
}

@@ -138,16 +138,59 @@
 eption;%0A
+import seedu.todo.commons.util.StringUtil;%0A
 import s
@@ -420,20 +420,13 @@
 did 
-last command
+%25s %25s
 !%5CnT
@@ -2064,32 +2064,46 @@
 eConsoleMessage(
+String.format(
 MESSAGE_SUCCESS)
@@ -2101,16 +2101,97 @@
 _SUCCESS
+, numUndo,%0A                StringUtil.pluralizer(numUndo, %22command%22, %22commands%22))
 );%0A    %7D

Update success message.