package seedu.address.storage;

import com.google.common.eventbus.Subscribe;
import seedu.address.commons.core.ComponentManager;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.model.TaskBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.commons.events.storage.StorageDataPathChangedEvent;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.ReadOnlyTaskBook;
import seedu.address.model.UserPrefs;

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

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

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


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

    public StorageManager(String addressBookFilePath, String userPrefsFilePath) {
        this(new XmlTaskBookStorage(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);
    }


    // ================ TaskBook methods ==============================

    @Override
    public String getTaskBookFilePath() {
        return addressBookStorage.getTaskBookFilePath();
    }

    @Override
    public Optional<ReadOnlyTaskBook> readTaskBook() throws DataConversionException, IOException {
        return readTaskBook(addressBookStorage.getTaskBookFilePath());
    }

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

    @Override
    public void saveTaskBook(ReadOnlyTaskBook addressBook) throws IOException {
        saveTaskBook(addressBook, addressBookStorage.getTaskBookFilePath());
    }

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


    @Override
    @Subscribe
    public void handleAddressBookChangedEvent(TaskBookChangedEvent event) {
        logger.info(LogsCenter.getEventHandlingLogMessage(event, "Local data changed, saving to file"));
        try {
            saveTaskBook(event.data);
        } catch (IOException e) {
            raise(new DataSavingExceptionEvent(e));
        }
    }
    
    //@@author A0139528W
    @Override
    @Subscribe
    public void handleStorageDataChangedEvent(StorageDataPathChangedEvent event) {
        logger.info(LogsCenter.getEventHandlingLogMessage(event, "Saving task.xml in a new location."));
        
        this.addressBookStorage = new XmlTaskBookStorage(event.newDataPath);
    }
    //@@author

}

@@ -855,39 +855,36 @@
 TaskBookStorage 
-address
+task
 BookStorage;%0A   
@@ -971,23 +971,20 @@
 Storage 
-address
+task
 BookStor
@@ -1047,39 +1047,36 @@
 );%0A        this.
-address
+task
 BookStorage = ad
@@ -1073,23 +1073,20 @@
 orage = 
-address
+task
 BookStor
@@ -1180,23 +1180,20 @@
 (String 
-address
+task
 BookFile
@@ -1262,23 +1262,20 @@
 Storage(
-address
+task
 BookFile
@@ -1852,39 +1852,36 @@
 %0A        return 
-address
+task
 BookStorage.getT
@@ -2043,31 +2043,28 @@
 eadTaskBook(
-address
+task
 BookStorage.
@@ -2308,23 +2308,20 @@
  return 
-address
+task
 BookStor
@@ -2411,31 +2411,28 @@
 nlyTaskBook 
-address
+task
 Book) throws
@@ -2471,28 +2471,22 @@
 ook(
-addressBook, address
+taskBook, task
 Book
@@ -2713,23 +2713,20 @@
         
-address
+task
 BookStor
@@ -2829,15 +2829,12 @@
 ndle
-Address
+Task
 Book
@@ -3404,23 +3404,20 @@
    this.
-address
+task
 BookStor

refactor StorageManager.java