package guitests;

import guitests.guihandles.TaskCardHandle;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import seedu.address.logic.commands.UpdateCommand;
import static seedu.address.logic.commands.UpdateCommand.MESSAGE_UPDATE_TASK_SUCCESS;
import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.AddCommand;
import seedu.address.testutil.TestTask;
import seedu.address.testutil.TestUtil;

import seedu.address.model.task.*;

public class UpdateCommandTest extends AddressBookGuiTest {

	@Test
    public void update() {

      
        TestTask[] currentList = td.getTypicalTasks();
        int targetIndex = 1;
        TestTask taskToUpdate=td.hoon;
        assertUpdateSuccess(targetIndex,taskToUpdate,currentList);
        currentList[targetIndex-1]=taskToUpdate;
        currentList=TestUtil.addTasksToList(currentList);
        
        /*taskToUpdate=td.benson;
        targetIndex=3;
        assertUpdateSuccess(targetIndex,taskToUpdate,currentList);
        currentList[targetIndex-1]=taskToUpdate;
        currentList=TestUtil.addTasksToList(currentList);*/
 
       // commandBox.runCommand(td.hoon.getArgs());
        //assertResultMessage(UpdateCommand.MESSAGE_DUPLICATE_TASK);
        //assertTrue(taskListPanel.isListMatching(currentList));
/*
        //delete from the middle of the list
        currentList = TestUtil.removeTaskFromList(currentList, targetIndex);
        targetIndex = currentList.length/2;
        assertUpdateSuccess(targetIndex, currentList);

        //invalid index
        commandBox.runCommand("update " + currentList.length + 1);
        assertResultMessage("The task index provided is invalid");*/

    }
	
	private void assertUpdateSuccess(int targetIndex, TestTask taskToUpdate, TestTask... currentList) {
		commandBox.runCommand("update " + targetIndex + taskToUpdate.getArgs() );
		
		//confirm the new card contains the right data
        TaskCardHandle updatedCard = taskListPanel.navigateToTask(targetIndex-1);
        assertMatching(taskToUpdate, updatedCard);

        //confirm the list now contains all previous tasks plus the new task
        TestTask[] expectedList = TestUtil.addTasksToList(currentList);
        expectedList[targetIndex-1]=taskToUpdate;
        assertTrue(taskListPanel.isListMatching(expectedList));
        
        //confirm the result message is correct
        assertResultMessage(String.format(MESSAGE_UPDATE_TASK_SUCCESS, taskToUpdate));
    }
	
	
}

@@ -573,16 +573,18 @@
 Test %7B%0A%0A
+%09%0A
 %09@Test%0A 
@@ -609,17 +609,16 @@
 ate() %7B%0A
-%0A
       %0A 
@@ -700,32 +700,34 @@
 ex = 1;%0A        
+/*
 TestTask taskToU
@@ -910,26 +910,27 @@
 urrentList);
+*/
 %0A
- 
        %0A    
@@ -938,32 +938,33 @@
    /
-*taskToUpdate=td.benson;
+/edit with duplicate task
 %0A   
@@ -995,187 +995,247 @@
     
-assertUpdateSuccess(targetIndex,taskToUpdate,currentList);%0A        currentList%5BtargetIndex-1%5D=taskToUpdate;%0A        currentList=TestUtil.addTasksToList(currentList);*/%0A %0A       //
+commandBox.runCommand(%22update %22+targetIndex+td.alice.getArgs());%0A        assertResultMessage(UpdateCommand.MESSAGE_DUPLICATE_TASK);%0A        assertTrue(taskListPanel.isListMatching(currentList));   %0A        %0A       //invalid command%0A       
  com
@@ -1253,23 +1253,33 @@
 Command(
-td.hoon
+%22updatee %22+td.ida
 .getArgs
@@ -1287,26 +1287,24 @@
 ));%0A        
-//
 assertResult
@@ -1315,123 +1315,169 @@
 age(
-UpdateCommand.MESSAGE_DUPLICATE_TASK);%0A        //assertTrue(taskListPanel.isListMatching(currentList
+Messages.MESSAGE_UNKNOWN_COMMAND);%0A        %0A       //invalid index%0A        commandBox.runCommand(%22update %22 + (currentList.length+1) + td.ida.getArgs(
 ));%0A
-/*%0A
         
 //de
@@ -1476,221 +1476,304 @@
     
-//delete from the middle of the list%0A        currentList = TestUtil.removeTaskFromList(currentList, targetIndex);%0A        targetIndex = currentList.length/2;%0A        assertUpdateSuccess(targetIndex, currentList);%0A
+assertResultMessage(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);%0A%0A        //edit in an empty list%0A        commandBox.runCommand(%22clear%22);%0A        commandBox.runCommand(%22update %22+targetIndex+td.ida.getArgs());%0A        assertResultMessage(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);%0A        
 %0A   
@@ -1779,37 +1779,39 @@
       //invalid 
-index
+command
 %0A        command
@@ -1836,35 +1836,28 @@
 date
+e
  %22
- + currentList.length + 1
++td.ida.getArgs()
 );%0A 
@@ -1887,49 +1887,54 @@
 age(
-%22The task index provided is invalid%22);*/%0A
+Messages.MESSAGE_UNKNOWN_COMMAND);%0A        %0A  
 %0A   
@@ -1988,17 +1988,16 @@
 etIndex,
- 
 TestTask

Add update command test