package guitests;

import static org.junit.Assert.*;
import static seedu.jimi.logic.commands.EditCommand.MESSAGE_EDIT_SUCCESS;

import org.junit.Test;

import guitests.guihandles.FloatingTaskCardHandle;
import seedu.jimi.commons.exceptions.IllegalValueException;
import seedu.jimi.model.task.Name;
import seedu.jimi.testutil.TestFloatingTask;
import seedu.jimi.testutil.TestUtil;
//@@author A0138915X
public class EditCommandTest extends AddressBookGuiTest{

    //@Test
    public void edit() throws IllegalValueException {
        TestFloatingTask[] currentList = td.getTypicalTasks();
        
        //edit the first task in list with all details
        int targetIndex = 1;
        
        //task to be changed to
        String newName = "Get rich or die coding";

        //edit the first task in list with only name changes
        assertEditTaskSuccess(currentList, targetIndex, newName);
        
        // edit last task in list with only name changes
        newName = "Don't die poor";
        targetIndex = currentList.length;
        assertEditTaskSuccess(currentList, targetIndex, newName);

        //invalid index
        commandBox.runCommand("edit t" + currentList.length + 1 + " \"Change invalid stuff\"");
        assertResultMessage("The task index provided is invalid");
        
        //TODO: edit the first event in the list with all details
    }
    
    /**
     * Runs the edit command on the targetTask and checks if the edited task matches with changedTask.
     * Expected new task is to be at the top of the list. 
     * 
     * @param targetIndex Index of target task to be edited.
     * @param currentList List of tasks to be edited.
     * @throws IllegalValueException 
     */
    private void assertEditTaskSuccess(TestFloatingTask[] currentList, int targetIndex, String newName) throws IllegalValueException{

        //task to be replaced with
        TestFloatingTask expectedTask = new TestFloatingTask();
        expectedTask.setName(new Name(newName));
        
        if (targetIndex <= currentList.length && targetIndex > 0) {
            expectedTask.setTags(currentList[targetIndex - 1].getTags());
        }
        
        expectedTask.setPriority(currentList[targetIndex - 1].getPriority());
        
        //edit the name of the target task with the newName
        commandBox.runCommand("edit " + "t" + targetIndex + " \"" + newName + "\" ");
        
        //confirm the edited card contains the right data
        FloatingTaskCardHandle addedCard = taskListPanel.navigateToTask(expectedTask.getName().fullName);
        assertMatching(expectedTask, addedCard);

        //confirm the list now contains all previous tasks plus the edited task
        TestFloatingTask[] expectedList = TestUtil.replaceTaskFromList(currentList, expectedTask, targetIndex - 1);
        assertTrue(taskListPanel.isListMatching(expectedList));
        
        //confirm the result message is correct
        assertResultMessage(String.format(MESSAGE_EDIT_SUCCESS, expectedTask));
    }

}

@@ -132,24 +132,105 @@
 ort 
-org.junit.Test;%0A
+java.util.Comparator;%0A%0Aimport org.junit.Test;%0A%0Aimport edu.emory.mathcs.backport.java.util.Arrays;
 %0Aimp
@@ -277,16 +277,57 @@
 Handle;%0A
+import seedu.jimi.commons.core.Messages;%0A
 import s
@@ -413,16 +413,59 @@
 k.Name;%0A
+import seedu.jimi.model.task.ReadOnlyTask;%0A
 import s
@@ -621,18 +621,16 @@
 t%7B%0A%0A    
-//
 @Test%0A  
@@ -744,32 +744,97 @@
 asks();%0A        
+Arrays.sort(currentList, 0, currentList.length, NameComparator);%0A
 %0A        //edit 
@@ -1485,44 +1485,53 @@
 age(
-%22The task index provided is invalid%22
+Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX
 );%0A 
@@ -3059,16 +3059,91 @@
 x - 1);%0A
+        Arrays.sort(expectedList, 0, expectedList.length, NameComparator);%0A
         
@@ -3341,11 +3341,466 @@
 ;%0A    %7D%0A
+    %0A    //@@author A0143471L%0A    public static Comparator%3CReadOnlyTask%3E NameComparator = new Comparator%3CReadOnlyTask%3E() %7B%0A%0A        public int compare(ReadOnlyTask task1, ReadOnlyTask task2) %7B%0A%0A            String taskName1 = task1.getName().toString().toUpperCase();%0A            String taskName2 = task2.getName().toString().toUpperCase();%0A%0A            // ascending order%0A            return taskName1.compareTo(taskName2);%0A        %7D%0A    %7D;%0A    // @@author
 %0A%7D%0A

Fix EditCommandTest to work