package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.ezdo.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.ezdo.logic.commands.DoneCommand.MESSAGE_DONE_TASK_SUCCESS;

import org.junit.Test;

import guitests.guihandles.TaskCardHandle;
import seedu.ezdo.logic.commands.DoneCommand;
import seedu.ezdo.testutil.TestTask;
import seedu.ezdo.testutil.TestUtil;

public class DoneCommandTest extends EzDoGuiTest {

    @Test
    public void done_success() {

        //marks first task in the list as done
        TestTask[] currentList = td.getTypicalTasks();
        TestTask[] doneList = td.getTypicalDoneTasks();
        int targetIndex = 1;
        TestTask toDone = currentList[targetIndex - 1];
        assertDoneSuccess(targetIndex, currentList, doneList);
        
        //marks the middle task in the list as done
        targetIndex = currentList.length / 2;
        assertDoneSuccess(targetIndex, currentList, doneList);

        //marks last task in the list as done
        currentList = TestUtil.removeTaskFromList(currentList, targetIndex);
        doneList = TestUtil.addTasksToList(doneList, toDone);
        targetIndex = currentList.length;
        assertDoneSuccess(targetIndex, currentList, doneList);

        //invalid index
        currentList = TestUtil.removeTaskFromList(currentList, targetIndex);
        doneList = TestUtil.addTasksToList(doneList, toDone);
        commandBox.runCommand("done " + currentList.length + 1);
        assertResultMessage("The task index provided is invalid");

        //invalid command
        currentList = TestUtil.removeTaskFromList(currentList, targetIndex);
        doneList = TestUtil.addTasksToList(doneList, toDone);
        commandBox.runCommand("dones 1");
        assertResultMessage(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DoneCommand.MESSAGE_USAGE));

    }

    private void assertDoneSuccess(int targetIndexOneIndexed, final TestTask[] currentList, final TestTask[] doneList) {

        TestTask taskToDone = currentList[targetIndexOneIndexed - 1]; // -1 as array uses zero indexing
        TestTask[] expectedRemainder = TestUtil.removeTaskFromList(currentList, targetIndexOneIndexed);
        TestTask[] expectedDone = TestUtil.addTasksToList(doneList, taskToDone);

        commandBox.runCommand("done " + targetIndexOneIndexed);

        //confirm the list now contains all done tasks including the one just marked as done
        assertTrue(taskListPanel.isListMatching(expectedDone));

        //confirm the result message is correct
        assertResultMessage(String.format(MESSAGE_DONE_TASK_SUCCESS, taskToDone));

        //confirm the new card contains the right data
        TaskCardHandle addedCard = taskListPanel.navigateToTask(taskToDone.getName().fullName);
        assertMatching(taskToDone, addedCard);

        //confirm the undone list does not contain the task just marked as done
        commandBox.runCommand("list");
        assertTrue(taskListPanel.isListMatching(expectedRemainder));
    }

}

@@ -281,16 +281,57 @@
 Handle;%0A
+import seedu.ezdo.commons.core.Messages;%0A
 import s
@@ -918,44 +918,230 @@
     
-targetIndex = currentList.length / 2
+currentList = TestUtil.removeTaskFromList(currentList, targetIndex);%0A        doneList = TestUtil.addTasksToList(doneList, toDone);%0A        targetIndex = currentList.length / 2;%0A        toDone = currentList%5BtargetIndex - 1%5D
 ;%0A  
@@ -1819,147 +1819,8 @@
 and%0A
-        currentList = TestUtil.removeTaskFromList(currentList, targetIndex);%0A        doneList = TestUtil.addTasksToList(doneList, toDone);%0A
     
@@ -1854,11 +1854,10 @@
 done
-s 1
+ a
 %22);%0A
@@ -1958,16 +1958,156 @@
 USAGE));
+%0A        %0A        //invalid command%0A        commandBox.runCommand(%22dones 1%22);%0A        assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
 %0A%0A    %7D%0A
@@ -2579,24 +2579,116 @@
 neIndexed);%0A
+    //    for (int i =0; i%3C expectedDone.length; i++) %7BSystem.out.println(expectedDone%5Bi%5D);%7D
 %0A        //c
@@ -3164,16 +3164,24 @@
 dCard);%0A
+        
 %0A       
@@ -3357,21 +3357,39 @@
 emainder));%0A
+        %0A        %0A
     %7D%0A%0A%7D%0A

Update invalid command test cases