package guitests;

import guitests.guihandles.TaskCardHandle;
import static org.junit.Assert.assertTrue;
import static seedu.task.logic.commands.UpdateCommand.MESSAGE_UPDATE_TASK_SUCCESS;

import org.junit.Test;

import seedu.task.commons.core.Messages;
import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.logic.commands.UpdateCommand;
import seedu.task.model.tag.Tag;
import seedu.task.model.task.*;
import seedu.task.testutil.TestTask;
import seedu.task.testutil.TestUtil;

public class UpdateCommandTest extends TaskManagerGuiTest {
	@Test
    public void update() throws IllegalValueException {
	    TestTask[] currentList = td.getTypicalTasks();
        int targetIndex = 1;
        
        // update first task
        assertUpdateSuccess(targetIndex, td.hoon, currentList);

        currentList = TestUtil.replaceTaskFromList(currentList, td.hoon, targetIndex - 1);
        targetIndex = currentList.length;
        
        // update last task
        assertUpdateSuccess(targetIndex, td.ida, currentList);
        
        currentList = TestUtil.replaceTaskFromList(currentList, td.ida, targetIndex - 1);
        targetIndex = 1;

        // add new tags
        Tag tagToAdd = new Tag("urgent");
        commandBox.runCommand("update " + targetIndex + " t/urgent");
        ReadOnlyTask newTask = taskListPanel.getTask(targetIndex - 1);
        assertTrue(newTask.getTags().contains(tagToAdd));
        
        // remove existing tags
        commandBox.runCommand("update " + targetIndex + " rt/urgent");
        newTask = taskListPanel.getTask(targetIndex - 1);
        assertTrue(!newTask.getTags().contains(tagToAdd));
        
        // update with no changes
        commandBox.runCommand("update " + targetIndex);
        TaskCardHandle updatedCard = taskListPanel.navigateToTask(targetIndex - 1);
        assertMatching(td.hoon, updatedCard);
        
        // update own task without changing name
        targetIndex = 3;
        commandBox.runCommand("update " + targetIndex + td.carl.getArgs());
        assertTrue(taskListPanel.isListMatching(currentList));
        
        // update task to a name that is duplicated
        targetIndex = 2;
        commandBox.runCommand("update " + targetIndex + td.carl.getArgs());
        assertResultMessage(UpdateCommand.MESSAGE_DUPLICATE_TASK);
        assertTrue(taskListPanel.isListMatching(currentList));
        
        // invalid index
        commandBox.runCommand("update " + (currentList.length+1) + td.ida.getArgs());
        assertResultMessage(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        assertTrue(taskListPanel.isListMatching(currentList));
        
        // no index provided
        commandBox.runCommand("update");
        assertTrue(taskListPanel.isListMatching(currentList));

        //invalid command
        commandBox.runCommand("updates " + currentList.length);
        assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
        assertTrue(taskListPanel.isListMatching(currentList));
    }
	
	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);

        TestTask[] expectedList = TestUtil.addTasksToList(currentList);
        
        // merge tags
        taskToUpdate.getTags().mergeFrom(expectedList[targetIndex - 1].getTags());
        
        expectedList[targetIndex - 1] = taskToUpdate;
        assertTrue(taskListPanel.isListMatching(expectedList));
        
        //confirm the result message is correct
        assertResultMessage(String.format(MESSAGE_UPDATE_TASK_SUCCESS, taskToUpdate));
    }
	
	
}

@@ -1678,30 +1678,780 @@
  // 
-update with no changes
+modify open time%0A        targetIndex = 4;%0A        commandBox.runCommand(%22update %22 + targetIndex + %22 s/today%22);%0A        TaskCardHandle updatedCard = taskListPanel.navigateToTask(targetIndex-1);%0A        TestTask expectedTask = currentList%5BtargetIndex - 1%5D;%0A        expectedTask.setOpenTime(new DateTime(%22today%22));%0A        assertMatching(expectedTask, updatedCard);%0A        %0A        // modify close time%0A        commandBox.runCommand(%22update %22 + targetIndex + %22 c/tomorrow%22);%0A        updatedCard = taskListPanel.navigateToTask(targetIndex-1);%0A        expectedTask = currentList%5BtargetIndex - 1%5D;%0A        expectedTask.setCloseTime(new DateTime(%22tomorrow%22));%0A        assertMatching(expectedTask, updatedCard);%0A        %0A        // update with no changes%0A        targetIndex = 1;
 %0A   
@@ -2503,24 +2503,24 @@
 Index);%0A
+
         
 TaskCard
@@ -2507,39 +2507,24 @@
 x);%0A        
-TaskCardHandle 
 updatedCard 

Add new test cases for Update command