package seedu.address.storage;

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

import java.io.IOException;

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

import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.commons.util.FileUtil;
import seedu.address.model.ReadOnlyTaskList;
import seedu.address.model.TaskList;
import seedu.address.model.task.Task;
import seedu.address.testutil.TypicalTestTasks;

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

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

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();

    @Test
    public void readTaskListNullFilePathAssertionFailure() throws Exception {
        thrown.expect(AssertionError.class);
        java.util.Optional<ReadOnlyTaskList> tl = readTaskList(null);
        assertNull(tl);
    }

    private java.util.Optional<ReadOnlyTaskList> readTaskList(String filePath) throws Exception {
        return new XmlTaskListStorage(filePath).readTaskList(addToTestDataPathIfNotNull(filePath));
    }

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

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

    @Test
    public void readNotXmlFormatExceptionThrown() throws Exception {

        thrown.expect(DataConversionException.class);
        readTaskList("NotXmlFormatTaskList.xml");
        fail();
        /* 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 readAndSaveTaskListAllInOrderSuccess() throws Exception {
        String filePath = testFolder.getRoot().getPath() + "TempTaskList.xml";
        TypicalTestTasks td = new TypicalTestTasks();
        TaskList original = td.getTypicalTaskList();
        XmlTaskListStorage xmlTaskListStorage = new XmlTaskListStorage(filePath);

        //Save in new file and read back
        xmlTaskListStorage.saveTaskList(original, filePath);
        ReadOnlyTaskList readBack = xmlTaskListStorage.readTaskList(filePath).get();
        assertEquals(original, new TaskList(readBack));

        //Modify data, overwrite exiting file, and read back
        original.addTask(new Task(td.hoon));
        original.removeTask(new Task(td.alice));
        xmlTaskListStorage.saveTaskList(original, filePath);
        readBack = xmlTaskListStorage.readTaskList(filePath).get();
        assertEquals(original, new TaskList(readBack));

        //Save and read without specifying file path
        original.addTask(new Task(td.ida));
        xmlTaskListStorage.saveTaskList(original); //file path not specified
        readBack = xmlTaskListStorage.readTaskList().get(); //file path not specified
        assertEquals(original, new TaskList(readBack));

    }

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

    private void saveTaskList(ReadOnlyTaskList taskList, String filePath) throws IOException {
        new XmlTaskListStorage(filePath).saveTaskList(taskList, addToTestDataPathIfNotNull(filePath));
    }

    @Test
    public void saveTaskListNullFilePathAssertionFailure() throws IOException {
        thrown.expect(AssertionError.class);
        saveTaskList(new TaskList(), null);
        fail();
    }


}

@@ -1500,32 +1500,16 @@
  != null
-%0A               
  ? TEST_
@@ -1551,24 +1551,8 @@
 lder
-%0A               
  : n
@@ -1924,16 +1924,27 @@
       /*
+%0A         *
  IMPORTA
@@ -2002,16 +2002,27 @@
  the one
+%0A         *
  above) 
@@ -2037,27 +2037,16 @@
 ignored.
-%0A         *
  That me
@@ -2082,16 +2082,27 @@
 than one
+%0A         *
  excepti
@@ -2501,24 +2501,25 @@
 %0A%0A        //
+ 
 Save in new 
@@ -2691,38 +2691,29 @@
         
+T
 as
-sertEquals(original,
+kList tl =
  new Tas
@@ -2719,32 +2719,67 @@
 skList(readBack)
+;%0A        assertEquals(original, tl
 );%0A%0A        //Mo
@@ -2776,16 +2776,17 @@
       //
+ 
 Modify d
@@ -3118,16 +3118,17 @@
       //
+ 
 Save and
@@ -3255,24 +3255,25 @@
 riginal); //
+ 
 file path no
@@ -3346,16 +3346,17 @@
 et(); //
+ 
 file pat
@@ -3352,32 +3352,95 @@
 // file path not
+%0A                                                            //
  specified%0A     
@@ -4107,11 +4107,10 @@
 %0A    %7D%0A%0A
-%0A
 %7D%0A

fix test 'readAndSaveTaskListAllInOrderSuccess' by using a temporary variable..?