package guitests;

import guitests.guihandles.FloatingTaskCardHandle;
import org.junit.Test;

import seedu.jimi.commons.core.Messages;
import seedu.jimi.logic.commands.AddCommand;
import seedu.jimi.model.task.ReadOnlyTask;
import seedu.jimi.testutil.TestDeadlineTask;
import seedu.jimi.testutil.TestFloatingTask;
import seedu.jimi.testutil.TestUtil;
import seedu.jimi.testutil.TypicalTestDeadlineTasks;
import seedu.jimi.testutil.TypicalTestFloatingTasks;

import static org.junit.Assert.assertTrue;

public class AddCommandTest extends AddressBookGuiTest {

    //@Test
    public void add() {
        ReadOnlyTask[] currentList = td.getTypicalTasks();
        
        //add one task
        TestFloatingTask taskToAdd = TypicalTestFloatingTasks.dream;
        assertAddSuccess(taskToAdd, currentList);
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);

        //add another task
        taskToAdd = TypicalTestFloatingTasks.night;
        assertAddSuccess(taskToAdd, currentList);
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);

        //add duplicate task
        commandBox.runCommand(TypicalTestFloatingTasks.dream.getAddCommand());
        assertResultMessage(AddCommand.MESSAGE_DUPLICATE_TASK);
        assertTrue(taskListPanel.isListMatching(currentList));
        
        //add deadline task
        TestDeadlineTask deadlineTaskToAdd = TypicalTestDeadlineTasks.homework;
        assertAddSuccess(deadlineTaskToAdd, currentList);
        currentList = TestUtil.addTasksToList(currentList, deadlineTaskToAdd);
        
        //add to empty list
        commandBox.runCommand("clear");
        assertAddSuccess(TypicalTestFloatingTasks.beach);

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

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

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

        //confirm the list now contains all previous tasks plus the new task
        ReadOnlyTask[] expectedList = TestUtil.addTasksToList(currentList, taskToAdd);
        assertTrue(taskListPanel.isListMatching(expectedList));
    }
    
    //@@author A0143471L
    private void assertAddSuccess(TestDeadlineTask taskToAdd, ReadOnlyTask... currentList) {
        commandBox.runCommand(taskToAdd.getAddCommand());

        assertResultMessage(String.format(AddCommand.MESSAGE_SUCCESS, taskToAdd));
    }
    //@@author
}

@@ -87,16 +87,67 @@
 .Test;%0A%0A
+import edu.emory.mathcs.backport.java.util.Arrays;%0A
 import s
@@ -224,16 +224,51 @@
 ommand;%0A
+import seedu.jimi.model.task.Name;%0A
 import s
@@ -580,16 +580,46 @@
 tTrue;%0A%0A
+import java.util.Comparator;%0A%0A
 public c
@@ -672,18 +672,16 @@
  %7B%0A%0A    
-//
 @Test%0A  
@@ -975,32 +975,34 @@
 st, taskToAdd);%0A
+/*
 %0A        //add a
@@ -1182,24 +1182,105 @@
 taskToAdd);%0A
+        Arrays.sort(currentList, 0, currentList.length, NameComparator);%0A        
 %0A        //a
@@ -1499,24 +1499,26 @@
 rentList));%0A
+*/
         %0A   
@@ -1660,37 +1660,24 @@
 ineTaskToAdd
-, currentList
 );%0A        c
@@ -1746,32 +1746,104 @@
 ToAdd);%0A        
+Arrays.sort(currentList, 0, currentList.length, NameComparator);%0A       
 %0A        //add t
@@ -2609,32 +2609,107 @@
 st, taskToAdd);%0A
+        Arrays.sort(expectedList, 0, expectedList.length, NameComparator);%0A
         assertTr
@@ -2847,49 +2847,89 @@
 ask 
-taskToAdd, ReadOnlyTask... currentList) %7B
+deadlineTaskToAdd) %7B%0A        System.out.print(deadlineTaskToAdd.getAddCommand());
 %0A   
@@ -2947,33 +2947,41 @@
 dBox.runCommand(
-t
+deadlineT
 askToAdd.getAddC
@@ -3062,17 +3062,25 @@
 UCCESS, 
-t
+deadlineT
 askToAdd
@@ -3085,24 +3085,456 @@
 dd));%0A    %7D%0A
+    %0A    public static Comparator%3CReadOnlyTask%3E NameComparator%0A                          = new Comparator%3CReadOnlyTask%3E() %7B%0A%0A        public int compare(ReadOnlyTask task1, ReadOnlyTask task2) %7B%0A%0A          String taskName1 = task1.getName().toString().toUpperCase();%0A          String taskName2 = task2.getName().toString().toUpperCase();%0A%0A          //ascending order%0A          return taskName1.compareTo(taskName2);%0A        %7D%0A    %7D;%0A
     //@@auth

Fix AddCommandTest to work for floatingtasks