package guitests;

import static org.junit.Assert.assertTrue;

import org.apache.commons.lang.ArrayUtils;
import org.junit.Test;

import seedu.ezdo.commons.core.Messages;
import seedu.ezdo.logic.commands.SortCommand;
import seedu.ezdo.model.EzDo;
import seedu.ezdo.model.todo.Task;
import seedu.ezdo.model.todo.UniqueTaskList;
import seedu.ezdo.testutil.TestTask;
import seedu.ezdo.testutil.TypicalTestTasks;

public class SortCommandTest extends EzDoGuiTest {

    @Override
    protected EzDo getInitialData() {
        EzDo ez = new EzDo();
        TypicalTestTasks.loadEzDoWithSampleData(ez);
        try {
            ez.addTask(new Task(td.kappa));
            ez.addTask(new Task(td.leroy));
            ez.addTask(new Task(td.megan));
        } catch (UniqueTaskList.DuplicateTaskException e) {
            assert false : "not possible";
        }
        return ez;
    }

    @Test
    public void sortByFields() {
        TestTask[] expectedList;

        // sort by start date
        commandBox.runCommand("sort s");
        expectedList = new TestTask[] {td.elle, td.george, td.daniel, td.carl, td.benson, td.fiona, td.alice, td.kappa,
                                      td.leroy, td.megan};
        assertTrue(taskListPanel.isListMatching(expectedList));

        // sort by start date in descending order
        commandBox.runCommand("s s d");
        expectedList = new TestTask[] {td.alice, td.fiona, td.benson, td.carl, td.daniel, td.george, td.elle, td.kappa,
                                      td.leroy, td.megan};
        assertTrue(taskListPanel.isListMatching(expectedList));

        // sort by due date
        commandBox.runCommand("s d");
        expectedList = new TestTask[] {td.carl, td.elle, td.george, td.daniel, td.fiona, td.benson, td.alice, td.kappa,
                                      td.leroy, td.megan};
        assertTrue(taskListPanel.isListMatching(expectedList));

        // sort by name
        commandBox.runCommand("sort n a");
        expectedList = new TestTask[] {td.alice, td.benson, td.carl, td.daniel, td.elle, td.fiona, td.george, td.kappa,
                                      td.leroy, td.megan};
        assertTrue(taskListPanel.isListMatching(expectedList));

        // sort by priority
        commandBox.runCommand("s p");
        expectedList = new TestTask[] {td.alice, td.benson, td.carl, td.daniel, td.fiona, td.elle, td.george, td.kappa,
                                      td.leroy, td.megan};
        assertTrue(taskListPanel.isListMatching(expectedList));

        //invalid command
        commandBox.runCommand("sort z");
        assertResultMessage(SortCommand.MESSAGE_INVALID_FIELD);

        //invalid command
        commandBox.runCommand("sort");
        assertResultMessage(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
    }

    @Test
    public void maintainSortOrder() {
        TestTask[] expectedList;

        commandBox.runCommand("sort d");

        // check that Jack is in the right place after adding him
        commandBox.runCommand(td.jack.getAddCommand(false));
        expectedList = new TestTask[] {td.carl, td.elle, td.george, td.jack, td.daniel, td.fiona, td.benson, td.alice,
                                      td.kappa, td.leroy, td.megan};
        assertTrue(taskListPanel.isListMatching(expectedList));

        // check that Carl is in the right place after editing his due date
        commandBox.runCommand("edit 1 d/12/12/2099");
        String expectedName = taskListPanel.getTask(7).getName().toString();
        assertTrue(expectedName.equals(td.carl.getName().toString()));
    }
}

@@ -1173,32 +1173,33 @@
                 
+ 
 td.leroy, td.meg
@@ -1195,32 +1195,32 @@
 roy, td.megan%7D;%0A
-
         assertTr
@@ -1470,32 +1470,33 @@
 elle, td.kappa,%0A
+ 
                 
@@ -1530,32 +1530,32 @@
 roy, td.megan%7D;%0A
-
         assertTr
@@ -1781,32 +1781,33 @@
 lice, td.kappa,%0A
+ 
                 
@@ -1841,32 +1841,32 @@
 roy, td.megan%7D;%0A
-
         assertTr
@@ -2093,32 +2093,33 @@
 orge, td.kappa,%0A
+ 
                 
@@ -2153,32 +2153,32 @@
 roy, td.megan%7D;%0A
-
         assertTr
@@ -2404,32 +2404,33 @@
 orge, td.kappa,%0A
+ 
                 
@@ -2464,32 +2464,32 @@
 roy, td.megan%7D;%0A
-
         assertTr
@@ -3223,16 +3223,17 @@
 .alice,%0A
+ 
         

Fix alignment of line wrap