package guitests;

import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;

import guitests.guihandles.TaskCardHandle;
import seedu.task.commons.core.Messages;
import seedu.task.commons.exceptions.DataConversionException;
import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.logic.commands.AddCommand;
import seedu.task.model.task.Name;
import seedu.task.testutil.TestTask;
import seedu.task.testutil.TestUtil;

public class AddCommandTest extends TaskManagerGuiTest {

    @Test
    public void add() throws IllegalValueException {
        //add one task
        TestTask[] currentList = null;
        try {
            currentList = td.getTypicalTasks();
        } catch (DataConversionException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        TestTask taskToAdd = currentList[0];
        taskToAdd.setName(new Name("Another Task Name"));
        assertAddSuccess(taskToAdd, currentList);
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);

        //add another task
        taskToAdd = currentList[1];
        assertAddSuccess(taskToAdd, currentList);
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);

        //add duplicate task
        commandBox.runCommand(currentList[2].getAddCommand());
        assertResultMessage(AddCommand.MESSAGE_DUPLICATE_TASK);
        assertTrue(taskListPanel.isListMatching(currentList));

        //add to empty list
        commandBox.runCommand("clear");
        assertAddSuccess(currentList[3]);

        //invalid command
        commandBox.runCommand("adds task99");
        assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
    }

    private void assertAddSuccess(TestTask taskToAdd, TestTask... currentList) {
        commandBox.runCommand(taskToAdd.getAddCommand());

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

        //confirm the list now contains all previous tasks plus the new task
        TestTask[] expectedList = TestUtil.addTasksToList(currentList, taskToAdd);
        assertTrue(taskListPanel.isListMatching(expectedList));
    }

}

@@ -382,23 +382,25 @@
 ask.
-model.task.Name
+testutil.TestTask
 ;%0Aim
@@ -424,32 +424,39 @@
 estutil.TestTask
+Builder
 ;%0Aimport seedu.t
@@ -597,24 +597,52 @@
 xception %7B%0A 
+       //@@author A0139410N%0A
        //add
@@ -901,33 +901,32 @@
        %7D%0A       
- 
 TestTask taskToA
@@ -922,32 +922,52 @@
 ask taskToAdd = 
+new TestTaskBuilder(
 currentList%5B0%5D;%0A
@@ -964,17 +964,17 @@
 tList%5B0%5D
-;
+)
 %0A       
@@ -978,30 +978,21 @@
     
-taskToAdd.setName(new 
+        .with
 Name
@@ -1012,16 +1012,23 @@
 k Name%22)
+.build(
 );%0A     
@@ -1195,22 +1195,93 @@
 d = 
-currentList%5B1%5D
+new TestTaskBuilder(currentList%5B1%5D)%0A                .withName(%22Second Task%22).build();
 ;%0A  
@@ -1392,32 +1392,51 @@
 t, taskToAdd);%0A%0A
+        //@@author%0A
         //add du

Modify TestTask to add, needs sorting to be implemented for addTaskToList in TestUtil to pass this last test case