package seedu.task.storage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger;

import com.google.common.eventbus.Subscribe;

import seedu.task.commons.core.ComponentManager;
import seedu.task.commons.core.LogsCenter;
import seedu.task.commons.events.model.TaskListChangedEvent;
import seedu.task.commons.events.storage.DataSavingExceptionEvent;
import seedu.task.commons.exceptions.DataConversionException;
import seedu.task.model.ReadOnlyTaskList;
import seedu.task.model.UserPrefs;

/**
 * Manages storage of TaskList data in local storage.
 */
public class StorageManager extends ComponentManager implements Storage {

    private static final Logger logger = LogsCenter.getLogger(StorageManager.class);
    private TaskListStorage taskListStorage;
    private UserPrefsStorage userPrefsStorage;


    public StorageManager(TaskListStorage taskListStorage, UserPrefsStorage userPrefsStorage) {
        super();
        this.taskListStorage = taskListStorage;
        this.userPrefsStorage = userPrefsStorage;
    }

    public StorageManager(String taskListFilePath, String userPrefsFilePath) {
        this(new XmlTaskListStorage(taskListFilePath), new JsonUserPrefsStorage(userPrefsFilePath));
    }

    // ================ UserPrefs methods ==============================

    @Override
    public Optional<UserPrefs> readUserPrefs() throws DataConversionException, IOException {
        return userPrefsStorage.readUserPrefs();
    }

    @Override
    public void saveUserPrefs(UserPrefs userPrefs) throws IOException {
        userPrefsStorage.saveUserPrefs(userPrefs);
    }


    // ================ TaskList methods ==============================

    @Override
    public String getTaskListFilePath() {
        return taskListStorage.getTaskListFilePath();
    }

    @Override
    public Optional<ReadOnlyTaskList> readTaskList() throws DataConversionException, IOException {
        return readTaskList(taskListStorage.getTaskListFilePath());
    }

    @Override
    public Optional<ReadOnlyTaskList> readTaskList(String filePath) throws DataConversionException, IOException {
        logger.fine("Attempting to read data from file: " + filePath);
        System.out.println("Attempting to read data from file: " + filePath);
        return taskListStorage.readTaskList(filePath);
    }

    @Override
    public void saveTaskList(ReadOnlyTaskList taskList) throws IOException {
        saveTaskList(taskList, taskListStorage.getTaskListFilePath());
    }

    @Override
    public void saveTaskList(ReadOnlyTaskList taskList, String filePath) throws IOException {
        logger.fine("Attempting to write to data file: " + filePath);
        taskListStorage.saveTaskList(taskList, filePath);

    }

    //@@author A0163559U
    @Override
    public void saveTaskListInNewLocation(ReadOnlyTaskList taskList, File newFile) throws IOException {
        logger.fine("Attempting to copy task manager data to file: " + newFile.toString());
        taskListStorage.saveTaskListInNewLocation(taskList, newFile);
    }

    @Override
    public Optional<ReadOnlyTaskList> loadTaskListFromNewLocation(ReadOnlyTaskList taskList, File loadFile)
            throws FileNotFoundException, DataConversionException {
        logger.fine("Attempting to load task manager data from file: " + loadFile.toString());
        return taskListStorage.loadTaskListFromNewLocation(taskList, loadFile);
    }

    public void updateXmlTaskListStorage(File file) {
        XmlTaskListStorage xmlTaskListStorage = (XmlTaskListStorage) taskListStorage;
        xmlTaskListStorage.updateState(file);;
    }
    //@@author


    @Override
    @Subscribe
    public void handleTaskListChangedEvent(TaskListChangedEvent event) {
        logger.info(LogsCenter.getEventHandlingLogMessage(event, "Local data changed, saving to file"));
        try {
            saveTaskList(event.data);
        } catch (IOException e) {
            raise(new DataSavingExceptionEvent(e));
        }
    }


}

@@ -2840,32 +2840,105 @@
 U%0A    @Override%0A
+    /**%0A     * Saves current state of task list in new location.%0A     */%0A
     public void 
@@ -3017,32 +3017,32 @@
 s IOException %7B%0A
-
         logger.f
@@ -3200,32 +3200,114 @@
 %0A%0A    @Override%0A
+    /**%0A     * Attempts to load a stored task list into the task manager.%0A     */%0A
     public Optio
@@ -3640,32 +3640,143 @@
 adFile);%0A    %7D%0A%0A
+    /**%0A     * Updates internal state data as storage support.%0A     * @param file File to update state%0A     */%0A
     public void 

add storage manager comments