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.AddressBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.ReadOnlyEmeraldo;
import seedu.address.model.UserPrefs;

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

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

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


    public StorageManager(EmeraldoStorage 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);
    }


    // ================ AddressBook methods ==============================

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

    @Override
    public Optional<ReadOnlyEmeraldo> readEmeraldo() throws DataConversionException, IOException {
        return readEmeraldo(addressBookStorage.getEmeraldoFilePath());
    }

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

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

    @Override
    public void saveAddressBook(ReadOnlyEmeraldo 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));
        }
    }

}

@@ -830,35 +830,32 @@
 aldoStorage 
-addressBook
+emeraldo
 Storage;%0A   
@@ -1029,27 +1029,24 @@
    this.
-addressBook
+emeraldo
 Storage 
@@ -1845,35 +1845,32 @@
      return 
-addressBook
+emeraldo
 Storage.getE
@@ -2036,27 +2036,24 @@
 meraldo(
-addressBook
+emeraldo
 Storage.
@@ -2297,27 +2297,24 @@
  return 
-addressBook
+emeraldo
 Storage.
@@ -2478,27 +2478,24 @@
 ssBook, 
-addressBook
+emeraldo
 Storage.
@@ -2717,27 +2717,24 @@
         
-addressBook
+emeraldo
 Storage.

Change variable addressBookStorage to emeraldoStorage