package seedu.taskmanager.model;

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

import com.google.common.eventbus.Subscribe;

import seedu.taskmanager.commons.core.ComponentManager;
import seedu.taskmanager.commons.core.LogsCenter;
import seedu.taskmanager.commons.events.model.TaskManagerChangedEvent;
import seedu.taskmanager.storage.StorageManager;

// @@author A0140032E
/**
 * Represents the History of user commands in this session
 */
public class HistoryManager extends ComponentManager {

    private final Model model;
    private static HistoryManager instance = null;
    private static final Logger logger = LogsCenter.getLogger(StorageManager.class);
    private ArrayList<ReadOnlyTaskManager> historyList;
    private ArrayList<ReadOnlyTaskManager> futureList;

    public static final String INITIALIZATION_ERROR = "HistoryManager has not been initialized with a Model yet";


    protected HistoryManager(Model model) {
        super();
        this.model = model;

        historyList = new ArrayList<ReadOnlyTaskManager>();
        futureList = new ArrayList<ReadOnlyTaskManager>();

        TaskManager taskManager = new TaskManager(model.getTaskManager());
        historyList.add(taskManager);
    }

    public static HistoryManager createInstance(Model model) {
        instance = new HistoryManager(model);
        return instance;
    }
    public static HistoryManager getInstance() {
        if (instance == null) {
            throw new NullPointerException(INITIALIZATION_ERROR);
        }
        return instance;
    }

    @Subscribe
    public void handleTaskManagerChangedEvent(TaskManagerChangedEvent event) {
        logger.info(LogsCenter.getEventHandlingLogMessage(event, "Local data changed, updating history manager"));
        TaskManager taskManager = new TaskManager(event.data);
        historyList.add(taskManager);
    }

    private ReadOnlyTaskManager getMostRecentHistory() {
        if (historyList.size() < 2) {
            throw new NullPointerException();
        }
        ReadOnlyTaskManager t = historyList.remove(historyList.size() - 1);
        futureList.add(new TaskManager(t));
        return historyList.remove(historyList.size() - 1);
    }

    private ReadOnlyTaskManager getMostRecentFuture() {
        if (futureList.size() < 1) {
            throw new NullPointerException();
        }
        ReadOnlyTaskManager t = futureList.remove(futureList.size() - 1);
        return t;
    }

    public void undo() {
        ReadOnlyTaskManager t = getMostRecentHistory();
        model.resetData(t);
    }

    public void redo() {
        ReadOnlyTaskManager t = getMostRecentFuture();
        model.resetData(t);
    }
}

@@ -312,16 +312,105 @@
 dEvent;%0A
+import seedu.taskmanager.commons.events.storage.TaskManagerStorageDirectoryChangedEvent;%0A
 import s
@@ -990,17 +990,16 @@
  yet%22;%0A%0A
-%0A
     prot
@@ -1456,24 +1456,25 @@
 ance;%0A    %7D%0A
+%0A
     public s
@@ -1740,24 +1740,125 @@
 nt event) %7B%0A
+        TaskManager taskManager = new TaskManager(event.data);%0A        historyList.add(taskManager);%0A
         logg
@@ -1909,17 +1909,34 @@
 e(event,
- 
+%0A                (
 %22Local d
@@ -1976,17 +1976,113 @@
 ager
-%22
+. Histories = %22 + historyList.size() + %22 Futures = %22%0A                        + futureList.size())
 ));%0A    
     
@@ -2081,102 +2081,485 @@
     
+%7D%0A%0A
     
-TaskManager taskManager = new TaskManager(event.data);%0A        historyList.add(taskManager
+@Subscribe%0A    public void handleTaskManagerStorageDirectoryChangedEvent(TaskManagerStorageDirectoryChangedEvent event) %7B%0A        historyList = new ArrayList%3CReadOnlyTaskManager%3E();%0A        futureList = new ArrayList%3CReadOnlyTaskManager%3E();%0A        logger.info(LogsCenter.getEventHandlingLogMessage(event,%0A                (%22Storage file location changed, resetting history. Histories = %22 + historyList.size() + %22 Futures = %22%0A                        + futureList.size()))
 );%0A 

remove history after load/move command