package seedu.address.storage;

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

import com.google.common.eventbus.Subscribe;

import seedu.address.commons.core.ComponentManager;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.model.TaskListChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.commons.events.storage.FileLocationChangedEvent;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.ReadOnlyTaskList;
import seedu.address.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);
        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);
    }

    public void setFilePath(String filePath) {
        taskListStorage.setFilePath(filePath);
    }


    @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));
        }
    }

    public void handleFileStorageLocationChangedEvent(FileLocationChangedEvent event) {
        logger.info(LogsCenter.getEventHandlingLogMessage(event, "File Location is changed."));
        setFilePath(event.getFilePath());
    }

}

@@ -208,24 +208,66 @@
 entManager;%0A
+import seedu.address.commons.core.Config;%0A
 import seedu
@@ -967,16 +967,56 @@
 torage;%0A
+    private ConfigStorage configStorage;
 %0A%0A    pu
@@ -1101,16 +1101,71 @@
 sStorage
+,%0A                          ConfigStorage configStorage
 ) %7B%0A    
@@ -1271,24 +1271,68 @@
 efsStorage;%0A
+        this.configStorage = configStorage;%0A
     %7D%0A%0A    p
@@ -1397,24 +1397,47 @@
 refsFilePath
+, String configFilePath
 ) %7B%0A        
@@ -1526,16 +1526,669 @@
 ilePath)
+,%0A             new JsonConfigStorage(configFilePath));%0A    %7D%0A%0A    // ================ Config methods ==============================%0A%0A    @Override%0A    public Config readConfig() throws DataConversionException, IOException %7B%0A        return configStorage.readConfig();%0A    %7D%0A%0A    @Override%0A    public void saveConfig(Config config) throws IOException %7B%0A        configStorage.saveConfig(config);%0A    %7D%0A%0A    private void saveFilePathInConfig(String filePath) throws DataConversionException, IOException %7B%0A        Config thisConfig = configStorage.readConfig();%0A        thisConfig.setAddressBookFilePath(filePath);%0A        configStorage.saveConfig(thisConfig
 );%0A    %7D
@@ -4112,23 +4112,16 @@
 ndleFile
-Storage
 Location
@@ -4156,32 +4156,71 @@
 ngedEvent event)
+ throws%0A        DataConversionException
  %7B%0A        logge
@@ -4340,21 +4340,229 @@
 ilePath());%0A
+        try %7B%0A            saveTaskList(event.getData());%0A            saveFilePathInConfig(event.getFilePath());%0A        %7D catch (IOException e) %7B%0A            raise(new DataSavingExceptionEvent(e));%0A        %7D%0A
     %7D%0A%0A%7D%0A

add configStorage and other methods