package guitests;

import static org.junit.Assert.*;
import static seedu.jimi.logic.commands.DeleteCommand.MESSAGE_DELETE_TASK_SUCCESS;

import org.junit.Test;

import seedu.jimi.commons.exceptions.IllegalValueException;
import seedu.jimi.model.task.Name;
import seedu.jimi.testutil.TestFloatingTask;
import seedu.jimi.testutil.TestUtil;

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";
        TestFloatingTask expectedTask = new TestFloatingTask();
        expectedTask.setName(new Name(newName)); //set up newTask with changed name

        //edit the last task in list with only name changes
        currentList = TestUtil.replaceTaskFromList(currentList, expectedTask, targetIndex);
        targetIndex = currentList.length;
        assertEditTaskSuccess(currentList, expectedTask, targetIndex, newName);

        //edit the middle task of the list with only name changes
        newName = "Don't die poor";
        expectedTask.setName(new Name(newName));
        currentList = TestUtil.replaceTaskFromList(currentList, expectedTask, targetIndex);
        targetIndex = currentList.length/2;
        assertEditTaskSuccess(currentList, expectedTask, targetIndex, newName);

        //invalid index
        commandBox.runCommand("edit " + currentList.length + 1);
        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. //TODO: change to match previous index of edited task
     * 
     * @param targetIndex Index of target task to be edited.
     * @param currentList List of tasks to be edited.
     * @throws IllegalValueException 
     */
    private void assertEditTaskSuccess(TestFloatingTask[] currentList,TestFloatingTask expectedTask, int targetIndex, String newName) throws IllegalValueException{

        //edit the name of the target task with the newName
        commandBox.runCommand("edit " + targetIndex + " " + newName);
        
        //confirm the list now contains all previous persons except the deleted person
        //TODO: change from checking last index to previous index of changed task
        assertTrue(taskListPanel.isListMatching(currentList));

        //confirm the result message is correct
        assertResultMessage(String.format(MESSAGE_DELETE_TASK_SUCCESS, expectedTask));
    }

}

@@ -90,14 +90,12 @@
 nds.
-Delete
+Edit
 Comm
@@ -98,38 +98,36 @@
 Command.MESSAGE_
-DELETE
+EDIT
 _TASK_SUCCESS;%0A%0A
@@ -150,16 +150,67 @@
 .Test;%0A%0A
+import guitests.guihandles.FloatingTaskCardHandle;%0A
 import s
@@ -752,156 +752,8 @@
 ng%22;
-%0A        TestFloatingTask expectedTask = new TestFloatingTask();%0A        expectedTask.setName(new Name(newName)); //set up newTask with changed name
 %0A%0A  
@@ -769,18 +769,19 @@
 dit the 
-la
+fir
 st task 
@@ -815,142 +815,8 @@
 ges%0A
-        currentList = TestUtil.replaceTaskFromList(currentList, expectedTask, targetIndex);%0A        targetIndex = currentList.length;%0A
     
@@ -849,38 +849,24 @@
 currentList,
- expectedTask,
  targetIndex
@@ -890,241 +890,34 @@
     
-//edit the middle task of the list with only name changes%0A        newName = %22Don't die poor%22;%0A        expectedTask.setName(new Name(newName));%0A        currentList = TestUtil.replaceTaskFromList(currentList, expectedTask, targetIndex)
+newName = %22Don't die poor%22
 ;%0A  
@@ -958,10 +958,8 @@
 ngth
-/2
 ;%0A  
@@ -998,30 +998,16 @@
 entList,
- expectedTask,
  targetI
@@ -1438,61 +1438,8 @@
 st. 
-//TODO: change to match previous index of edited task
 %0A   
@@ -1678,38 +1678,8 @@
 ist,
-TestFloatingTask expectedTask,
  int
@@ -1753,305 +1753,703 @@
   //
-edit the name of the target task with the newName%0A        commandBox.runCommand(%22edit %22 + targetIndex + %22 %22 + newName);%0A        %0A        //confirm the list now contains all previous persons except the deleted person%0A        //TODO: change from c
+task to be replaced with%0A        TestFloatingTask expectedTask = new TestFloatingTask();%0A        expectedTask.setName(new Name(newName));%0A        %0A        //edit the name of the target task with the newName%0A        commandBox.runCommand(%22edit %22 + targetIndex + %22 %22 + newName);%0A        %0A        //confirm the edited card contains the right data%0A        FloatingTaskCardHandle addedCard = taskListPanel.navigateToTask(expectedTask.getName().fullName);%0A        assertMatching(expectedTask, addedCard);%0A%0A        //confirm t
 he
-cking
  l
-ast index to previous index of changed task
+ist now contains all previous tasks plus the edited task%0A        TestFloatingTask%5B%5D expectedList = TestUtil.replaceTaskFromList(currentList, expectedTask, targetIndex - 1);
 %0A   
@@ -2497,23 +2497,32 @@
 ing(
-currentList));%0A
+expectedList));%0A        
 %0A   
@@ -2620,14 +2620,12 @@
 AGE_
-DELETE
+EDIT
 _TAS

Update EditCommand tests