package guitests;

import static org.junit.Assert.assertTrue;

import java.io.File;

import org.junit.Test;

import seedu.address.commons.core.Messages;
import seedu.address.commons.util.FileUtil;
import seedu.address.testutil.TestTask;

//files are automatically written to the saved location
public class OpenAndSaveCommandTest extends AddressBookGuiTest {
    public final String FILE_DIR = "data/";
    public final String FILE_NAME = "data/taskmanager";
    public final String FILE_ALTERNATE_NAME = "data/myPotato";
    public final String FILE_XML_EXTENSION = ".xml";

    //Open and save files functionality
    @Test
    public void save_file() {
        assertSaveResult("save " + FILE_DIR, FILE_NAME);                     //Default filename: taskmanager.xml
        assertOpenResult("open " + FILE_DIR, td.getTypicalTasks());                       // File Directory
        assertSaveResult("save " + FILE_NAME, FILE_NAME);                                 // without .xml
        assertOpenResult("open " + FILE_NAME, td.getTypicalTasks());                      // without .xml
        assertSaveResult("save " + FILE_NAME + FILE_XML_EXTENSION, FILE_NAME);            // with .xml
        assertOpenResult("open " + FILE_NAME + FILE_XML_EXTENSION, td.getTypicalTasks()); // with .xml
        assertSaveResult("save " + FILE_ALTERNATE_NAME, FILE_ALTERNATE_NAME); //Ensure successful save, to be used for test cases
    }

    //Save file, Delete and Open file to retrieve the saved data
    //td.getTypicalTasks() = TestTask[]{alice, benson, carl, daniel, elle, fiona}
    @Test
    public void save_checkTaskList() {
        assertTrue(taskListPanel.isListMatching(td.getTypicalTasks()));
        assertSaveResult("save " + FILE_NAME, FILE_NAME);
        assertSaveResult("save " + FILE_ALTERNATE_NAME, FILE_ALTERNATE_NAME);
        commandBox.runCommand("delete 1");
        assertTrue(taskListPanel.isListMatching(new TestTask[]{td.benson, td.carl, td.daniel, td.elle, td.fiona}));
        assertOpenResult("open " + FILE_NAME + FILE_XML_EXTENSION, td.getTypicalTasks());
        assertTrue(taskListPanel.isListMatching(td.getTypicalTasks()));
    }

    //Test invalid Command confirmation
    @Test
    public void save_invalidCommand_fail() {
        commandBox.runCommand("save" + FILE_DIR);
        assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
        commandBox.runCommand("open" + FILE_NAME);
        assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
    }

    //Ensure save functions properly
    private void assertSaveResult(String command, String filename) {
        File testfile = new File(filename + FILE_XML_EXTENSION);
        if (FileUtil.isFileExists(testfile)) {
            FileUtil.deleteFile(testfile);
        }
        commandBox.runCommand(command);
        assertTrue(FileUtil.isFileExists(testfile));
    }

    //Ensure open functions properly
    private void assertOpenResult(String command, TestTask... expectedhits) {
        commandBox.runCommand(command);
        assertTrue(taskListPanel.isListMatching(expectedhits));
    }
}


@@ -575,16 +575,17 @@
 %0A%0A    //
+ 
 Open and
@@ -698,32 +698,90 @@
 DIR, FILE_NAME);
+ // Default filename:%0A                                    
                 
@@ -787,33 +787,16 @@
       //
-Default filename:
  taskman
@@ -875,38 +875,16 @@
 ());
-                      
  // File
  Dir
@@ -879,17 +879,16 @@
  // File
- 
 Director
@@ -950,40 +950,8 @@
 ME);
-                                
  // 
@@ -1031,37 +1031,16 @@
 asks());
-                     
  // with
@@ -1126,27 +1126,16 @@
 E_NAME);
-           
  // with
@@ -1320,19 +1320,28 @@
 E_NAME);
+%0A       
  //
+ 
 Ensure s
@@ -1395,16 +1395,17 @@
 %0A%0A    //
+ 
 Save fil
@@ -1461,16 +1461,17 @@
 a%0A    //
+ 
 td.getTy
@@ -1530,16 +1530,23 @@
 l, elle,
+%0A    //
  fiona%7D%0A
@@ -1907,17 +1907,19 @@
 stTask%5B%5D
-%7B
+ %7B 
 td.benso
@@ -1958,16 +1958,17 @@
 td.fiona
+ 
 %7D));%0A   
@@ -2139,16 +2139,17 @@
 %0A%0A    //
+ 
 Test inv
@@ -2465,24 +2465,25 @@
    %7D%0A%0A    //
+ 
 Ensure save 
@@ -2841,16 +2841,17 @@
 %0A%0A    //
+ 
 Ensure o
@@ -3063,9 +3063,8 @@
     %7D%0A%7D%0A
-%0A

fix 1.0