package seedu.taskmanager.storage;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.IOException;

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

import seedu.taskmanager.commons.exceptions.DataConversionException;
import seedu.taskmanager.commons.util.FileUtil;
import seedu.taskmanager.model.TaskManager;
import seedu.taskmanager.model.ReadOnlyTaskManager;
import seedu.taskmanager.model.task.Task;
import seedu.taskmanager.storage.XmlTaskManagerStorage;
import seedu.taskmanager.testutil.TypicalTestTasks;

public class XmlTaskManagerStorageTest {
    private static final String TEST_DATA_FOLDER = FileUtil.getPath("./src/test/data/XmlAddressBookStorageTest/");

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();

    @Test
    public void readAddressBook_nullFilePath_assertionFailure() throws Exception {
        thrown.expect(AssertionError.class);
        readAddressBook(null);
    }

    private java.util.Optional<ReadOnlyTaskManager> readAddressBook(String filePath) throws Exception {
        return new XmlTaskManagerStorage(filePath).readTaskManager(addToTestDataPathIfNotNull(filePath));
    }

    private String addToTestDataPathIfNotNull(String prefsFileInTestDataFolder) {
        return prefsFileInTestDataFolder != null
                ? TEST_DATA_FOLDER + prefsFileInTestDataFolder
                : null;
    }

    @Test
    public void read_missingFile_emptyResult() throws Exception {
        assertFalse(readAddressBook("NonExistentFile.xml").isPresent());
    }

    @Test
    public void read_notXmlFormat_exceptionThrown() throws Exception {

        thrown.expect(DataConversionException.class);
        readAddressBook("NotXmlFormatAddressBook.xml");

        /* IMPORTANT: Any code below an exception-throwing line (like the one above) will be ignored.
         * That means you should not have more than one exception test in one method
         */
    }

    @Test
    public void readAndSaveAddressBook_allInOrder_success() throws Exception {
        String filePath = testFolder.getRoot().getPath() + "TempAddressBook.xml";
        TypicalTestTasks td = new TypicalTestTasks();
        TaskManager original = td.getTypicalTaskManager();
        XmlTaskManagerStorage xmlTaskManagerStorage = new XmlTaskManagerStorage(filePath);

        //Save in new file and read back
        xmlTaskManagerStorage.saveTaskManager(original, filePath);
        ReadOnlyTaskManager readBack = xmlTaskManagerStorage.readTaskManager(filePath).get();
        assertEquals(original, new TaskManager(readBack));

        //Modify data, overwrite exiting file, and read back
        original.addTask(new Task(td.send2));
        original.removeTask(new Task(td.buy1));
        xmlTaskManagerStorage.saveTaskManager(original, filePath);
        readBack = xmlTaskManagerStorage.readTaskManager(filePath).get();
        assertEquals(original, new TaskManager(readBack));

        //Save and read without specifying file path
        original.addTask(new Task(td.call1));
        xmlTaskManagerStorage.saveTaskManager(original); //file path not specified
        readBack = xmlTaskManagerStorage.readTaskManager().get(); //file path not specified
        assertEquals(original, new TaskManager(readBack));

    }

    @Test
    public void saveAddressBook_nullAddressBook_assertionFailure() throws IOException {
        thrown.expect(AssertionError.class);
        saveAddressBook(null, "SomeFile.xml");
    }

    private void saveAddressBook(ReadOnlyTaskManager addressBook, String filePath) throws IOException {
        new XmlTaskManagerStorage(filePath).saveTaskManager(addressBook, addToTestDataPathIfNotNull(filePath));
    }

    @Test
    public void saveAddressBook_nullFilePath_assertionFailure() throws IOException {
        thrown.expect(AssertionError.class);
        saveAddressBook(new TaskManager(), null);
    }


}

@@ -771,27 +771,27 @@
 data/Xml
-AddressBook
+TaskManager
 StorageT
@@ -978,27 +978,27 @@
 oid read
-AddressBook
+TaskManager
 _nullFil
@@ -1098,27 +1098,27 @@
     read
-AddressBook
+TaskManager
 (null);%0A
@@ -1180,27 +1180,27 @@
 er%3E read
-AddressBook
+TaskManager
 (String 
@@ -1662,35 +1662,35 @@
 rtFalse(read
-AddressBook
+TaskManager
 (%22NonExisten
@@ -1870,27 +1870,27 @@
     read
-AddressBook
+TaskManager
 (%22NotXml
@@ -1895,27 +1895,27 @@
 mlFormat
-AddressBook
+TaskManager
 .xml%22);%0A
@@ -2158,27 +2158,27 @@
 dAndSave
-AddressBook
+TaskManager
 _allInOr
@@ -2274,27 +2274,27 @@
  + %22Temp
-AddressBook
+TaskManager
 .xml%22;%0A 
@@ -3490,35 +3490,35 @@
 save
-AddressBook_nullAddressBook
+TaskManager_nullTaskManager
 _ass
@@ -3611,27 +3611,27 @@
     save
-AddressBook
+TaskManager
 (null, %22
@@ -3674,27 +3674,27 @@
 oid save
-AddressBook
+TaskManager
 (ReadOnl
@@ -3706,27 +3706,27 @@
 Manager 
-addressBook
+taskManager
 , String
@@ -3821,19 +3821,19 @@
 ger(
-addressBook
+taskManager
 , ad
@@ -3906,27 +3906,27 @@
 oid save
-AddressBook
+TaskManager
 _nullFil
@@ -4032,19 +4032,19 @@
 save
-AddressBook
+TaskManager
 (new

Refactor XmlTaskManagerStorageTest