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.AddressBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
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 AddressBookStorage addressBookStorage;
    private UserPrefsStorage userPrefsStorage;


    public StorageManager(AddressBookStorage addressBookStorage, UserPrefsStorage userPrefsStorage) {
        super();
        this.addressBookStorage = addressBookStorage;
        this.userPrefsStorage = userPrefsStorage;
    }

    public StorageManager(String addressBookFilePath, String userPrefsFilePath) {
        this(new XmlAddressBookStorage(addressBookFilePath), 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 getAddressBookFilePath() {
        return addressBookStorage.getAddressBookFilePath();
    }

    @Override
    public Optional<ReadOnlyTaskList> readAddressBook() throws DataConversionException, IOException {
        return readAddressBook(addressBookStorage.getAddressBookFilePath());
    }

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

    @Override
    public void saveAddressBook(ReadOnlyTaskList addressBook) throws IOException {
        saveAddressBook(addressBook, addressBookStorage.getAddressBookFilePath());
    }

    @Override
    public void saveAddressBook(ReadOnlyTaskList addressBook, String filePath) throws IOException {
        logger.fine("Attempting to write to data file: " + filePath);
        addressBookStorage.saveAddressBook(addressBook, filePath);
    }


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

}

@@ -300,27 +300,24 @@
 s.model.
-AddressBook
+TaskList
 ChangedE
@@ -779,38 +779,32 @@
 ate 
-AddressBookStorage addressBook
+TaskListStorage taskList
 Stor
@@ -887,38 +887,32 @@
 ger(
-AddressBookStorage addressBook
+TaskListStorage taskList
 Stor
@@ -987,40 +987,34 @@
 his.
-addressBookStorage = addressBook
+taskListStorage = taskList
 Stor
@@ -1108,27 +1108,24 @@
 (String 
-addressBook
+taskList
 FilePath
@@ -1178,38 +1178,32 @@
  Xml
-AddressBookStorage(addressBook
+TaskListStorage(taskList
 File
@@ -1752,27 +1752,24 @@
 ring get
-AddressBook
+TaskList
 FilePath
@@ -1792,41 +1792,35 @@
 urn 
-addressBookStorage.getAddressBook
+taskListStorage.getTaskList
 File
@@ -1890,27 +1890,24 @@
 st%3E read
-AddressBook
+TaskList
 () throw
@@ -1970,53 +1970,44 @@
 read
-AddressBook(addressBookStorage.getAddressBook
+TaskList(taskListStorage.getTaskList
 File
@@ -2078,27 +2078,24 @@
 st%3E read
-AddressBook
+TaskList
 (String 
@@ -2236,27 +2236,24 @@
  return 
-addressBook
+taskList
 Storage.
@@ -2256,27 +2256,24 @@
 age.read
-AddressBook
+TaskList
 (filePat
@@ -2313,35 +2313,32 @@
 ic void save
-AddressBook
+TaskList
 (ReadOnlyTas
@@ -2343,27 +2343,24 @@
 askList 
-addressBook
+taskList
 ) throws
@@ -2390,66 +2390,54 @@
 save
-AddressBook(addressBook, addressBookStorage.getAddressBook
+TaskList(taskList, taskListStorage.getTaskList
 File
@@ -2486,27 +2486,24 @@
 oid save
-AddressBook
+TaskList
 (ReadOnl
@@ -2512,27 +2512,24 @@
 askList 
-addressBook
+taskList
 , String
@@ -2638,27 +2638,24 @@
         
-addressBook
+taskList
 Storage.
@@ -2662,31 +2662,25 @@
 save
-AddressBook(addressBook
+TaskList(taskList
 , fi
@@ -2747,27 +2747,24 @@
 d handle
-AddressBook
+TaskList
 ChangedE
@@ -2768,27 +2768,24 @@
 edEvent(
-AddressBook
+TaskList
 ChangedE
@@ -2937,19 +2937,16 @@
 save
-AddressBook
+TaskList
 (eve

replace address book with task list in StorageManager.java