//@@author A0139961U
package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.tache.commons.core.Messages.MESSAGE_INVALID_DIRECTORY;
import static seedu.tache.commons.core.Messages.MESSAGE_INVALID_FILE;

import org.junit.Test;

import seedu.tache.logic.commands.LoadCommand;
import seedu.tache.logic.commands.SaveCommand;
import seedu.tache.testutil.TestTask;
import seedu.tache.testutil.TestUtil;

public class SaveAndLoadCommandTest extends TaskManagerGuiTest {

    public final String saveFolder1 = TestUtil.SANDBOX_FOLDER + "saveTest1";
    public final String saveFolder2 = TestUtil.SANDBOX_FOLDER + "saveTest2";
    public final String fileName = "\\taskmanager.xml";

    @Test
    public void saveAndLoadDataFile() {
        TestTask[] tasks = td.getTypicalTasks();

        commandBox.runCommand(td.getFit.getAddCommand());
        tasks = TestUtil.addTasksToList(tasks, td.getFit);

        commandBox.runCommand(SaveCommand.COMMAND_WORD + " " + saveFolder1);
        commandBox.runCommand(SaveCommand.COMMAND_WORD + " " + saveFolder2);

        //Delete the newly added task and add findGirlfriend
        commandBox.runCommand("delete 3"); //delete getFit
        tasks = TestUtil.removeTasksFromList(tasks, td.getFit);
        commandBox.runCommand(td.findGirlfriend.getAddCommand());
        tasks = TestUtil.addTasksToList(tasks, td.findGirlfriend);
        assertTrue(taskListPanel.isListMatching(tasks));

        //Load saveTest1 (saveTest1 : 1-5, getFit | saveTest2 : 1-5, findGirlfriend)
        commandBox.runCommand(LoadCommand.COMMAND_WORD + " " + saveFolder1 + fileName);
        tasks = TestUtil.removeTasksFromList(tasks, td.findGirlfriend);

        //Check if saveTest1 still has getFit
        tasks = TestUtil.addTasksToList(td.getTypicalTasks(), td.getFit);
        assertTrue(taskListPanel.isListMatching(tasks));

        //Now add something to saveTest1 (saveTest1 : 1-5, getFit | saveTest2 : 1-5, findGirlfriend)
        tasks = TestUtil.removeTasksFromList(tasks, td.getFit);
        tasks = TestUtil.removeTasksFromList(tasks, td.visitSarah);
        commandBox.runCommand("delete 3"); //getFit
        commandBox.runCommand("delete 2"); //visitSarah
        commandBox.runCommand(td.findGirlfriend.getAddCommand());
        tasks = TestUtil.addTasksToList(tasks, td.findGirlfriend);
        assertTrue(taskListPanel.isListMatching(tasks));
        //(saveTest1 : 1-4, findGirlfriend | saveTest2 : 1-5, findGirlfriend)

        //Load back the new file and check if getFit is deleted
        tasks = TestUtil.addTasksToList(td.getTypicalTasks(), td.findGirlfriend);
        commandBox.runCommand(LoadCommand.COMMAND_WORD + " " + saveFolder2 + fileName);
        assertTrue(taskListPanel.isListMatching(tasks));
    }

    @Test
    public void saveInvalidDirectoryFailure() {
        commandBox.runCommand(SaveCommand.COMMAND_WORD + " \\");
        assertResultMessage(MESSAGE_INVALID_DIRECTORY);
        commandBox.runCommand(SaveCommand.COMMAND_WORD + " /");
        assertResultMessage(MESSAGE_INVALID_DIRECTORY);
    }

    @Test
    public void loadInvalidFilePathFailure() {
        commandBox.runCommand(LoadCommand.COMMAND_WORD + " " + saveFolder1);
        assertResultMessage(MESSAGE_INVALID_FILE);
        commandBox.runCommand(LoadCommand.COMMAND_WORD + " " + saveFolder1 + "\\someInvalidFolder" + fileName);
        assertResultMessage(MESSAGE_INVALID_FILE);
    }

}

@@ -1458,34 +1458,43 @@
 //Load saveTest1
- (
+%0A        //
 saveTest1 : 1-5,
@@ -1493,37 +1493,155 @@
 1 : 
-1-5, getFit %7C saveTest2 : 1-5
+payDavid, visitSarah, eggsAndBread, visitGrandma, readBook, getFit%0A        //saveTest2 : payDavid, visitSarah, eggsAndBread, visitGrandma, readBook
 , fi
@@ -1644,33 +1644,32 @@
 , findGirlfriend
-)
 %0A        command
@@ -1996,109 +1996,8 @@
 );%0A%0A
-        //Now add something to saveTest1 (saveTest1 : 1-5, getFit %7C saveTest2 : 1-5, findGirlfriend)%0A
     
@@ -2432,17 +2432,16 @@
       //
-(
 saveTest
@@ -2448,11 +2448,54 @@
 1 : 
-1-4
+payDavid, eggsAndBread, visitGrandma, readBook
 , fi
@@ -2510,11 +2510,19 @@
 iend
- %7C 
+%0A        //
 save
@@ -2533,11 +2533,66 @@
 2 : 
-1-5
+payDavid, visitSarah, eggsAndBread, visitGrandma, readBook
 , fi
@@ -3143,24 +3143,24 @@
 RD + %22 /%22);%0A
-
         asse
@@ -3202,32 +3202,529 @@
 ECTORY);%0A    %7D%0A%0A
+    @Test%0A    public void saveDirectoryNotExistSuccess() %7B%0A        commandBox.runCommand(SaveCommand.COMMAND_WORD + %22 %22 + saveFolder1 + %22%5C%5CNotExistFolder%22);%0A        assertResultMessage(String.format(SaveCommand.MESSAGE_SUCCESS, saveFolder1 + %22%5C%5CNotExistFolder%22));%0A    %7D%0A%0A    @Test%0A    public void saveDirectoryExistSuccess() %7B%0A        commandBox.runCommand(SaveCommand.COMMAND_WORD + %22 %22 + saveFolder1);%0A        assertResultMessage(String.format(SaveCommand.MESSAGE_SUCCESS, saveFolder1));%0A    %7D%0A%0A
     @Test%0A    pu

Update SaveAndLoadCommandTest