package seedu.taskmanager.storage;


import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import seedu.taskmanager.commons.events.model.TaskManagerChangedEvent;
import seedu.taskmanager.commons.events.storage.DataSavingExceptionEvent;
import seedu.taskmanager.model.TaskManager;
import seedu.taskmanager.model.ReadOnlyTaskManager;
import seedu.taskmanager.model.UserPrefs;
import seedu.taskmanager.storage.JsonUserPrefsStorage;
import seedu.taskmanager.storage.Storage;
import seedu.taskmanager.storage.StorageManager;
import seedu.taskmanager.storage.XmlTaskManagerStorage;
import seedu.taskmanager.testutil.EventsCollector;
import seedu.taskmanager.testutil.TypicalTestTasks;

public class StorageManagerTest {

    private StorageManager storageManager;

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();


    @Before
    public void setUp() {
        storageManager = new StorageManager(getTempFilePath("ab"), getTempFilePath("prefs"));
    }


    private String getTempFilePath(String fileName) {
        return testFolder.getRoot().getPath() + fileName;
    }


    @Test
    public void prefsReadSave() throws Exception {
        /*
         * Note: This is an integration test that verifies the StorageManager is properly wired to the
         * {@link JsonUserPrefsStorage} class.
         * More extensive testing of UserPref saving/reading is done in {@link JsonUserPrefsStorageTest} class.
         */
        UserPrefs original = new UserPrefs();
        original.setGuiSettings(300, 600, 4, 6);
        storageManager.saveUserPrefs(original);
        UserPrefs retrieved = storageManager.readUserPrefs().get();
        assertEquals(original, retrieved);
    }

    @Test
    public void addressBookReadSave() throws Exception {
        /*
         * Note: This is an integration test that verifies the StorageManager is properly wired to the
         * {@link XmlAddressBookStorage} class.
         * More extensive testing of UserPref saving/reading is done in {@link XmlAddressBookStorageTest} class.
         */
        TaskManager original = new TypicalTestTasks().getTypicalTaskManager();
        storageManager.saveTaskManager(original);
        ReadOnlyTaskManager retrieved = storageManager.readTaskManager().get();
        assertEquals(original, new TaskManager(retrieved));
    }

    @Test
    public void getAddressBookFilePath() {
        assertNotNull(storageManager.getTaskManagerFilePath());
    }

    @Test
    public void handleAddressBookChangedEvent_exceptionThrown_eventRaised() throws IOException {
        // Create a StorageManager while injecting a stub that  throws an exception when the save method is called
        Storage storage = new StorageManager(new XmlAddressBookStorageExceptionThrowingStub("dummy"),
                                             new JsonUserPrefsStorage("dummy"));
        EventsCollector eventCollector = new EventsCollector();
        storage.handleTaskManagerChangedEvent(new TaskManagerChangedEvent(new TaskManager()));
        assertTrue(eventCollector.get(0) instanceof DataSavingExceptionEvent);
    }


    /**
     * A Stub class to throw an exception when the save method is called
     */
    class XmlAddressBookStorageExceptionThrowingStub extends XmlTaskManagerStorage {

        public XmlAddressBookStorageExceptionThrowingStub(String filePath) {
            super(filePath);
        }

        @Override
        public void saveTaskManager(ReadOnlyTaskManager addressBook, String filePath) throws IOException {
            throw new IOException("dummy exception");
        }
    }


}

@@ -1950,27 +1950,27 @@
 ic void 
-addressBook
+taskManager
 ReadSave
@@ -2122,35 +2122,35 @@
 * %7B@link Xml
-AddressBook
+TaskManager
 Storage%7D cla
@@ -2231,35 +2231,35 @@
 n %7B@link Xml
-AddressBook
+TaskManager
 StorageTest%7D
@@ -2583,27 +2583,27 @@
 void get
-AddressBook
+TaskManager
 FilePath
@@ -2710,27 +2710,27 @@
 d handle
-AddressBook
+TaskManager
 ChangedE
@@ -2948,35 +2948,35 @@
 ager(new Xml
-AddressBook
+TaskManager
 StorageExcep
@@ -3427,35 +3427,35 @@
    class Xml
-AddressBook
+TaskManager
 StorageExcep
@@ -3526,19 +3526,19 @@
  Xml
-AddressBook
+TaskManager
 Stor
@@ -3699,19 +3699,19 @@
 ger 
-addressBook
+taskManager
 , St

Refactor StorageManagerTest (fully working)