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()));
    }
}

@@ -1056,24 +1056,25 @@
 w TestTask%5B%5D
+ 
 %7Btd.elle, td
@@ -1135,32 +1135,32 @@
 lice, td.kappa,%0A
-
                 
@@ -1386,32 +1386,33 @@
 = new TestTask%5B%5D
+ 
 %7Btd.alice, td.fi
@@ -1696,32 +1696,33 @@
 = new TestTask%5B%5D
+ 
 %7Btd.carl, td.ell
@@ -2007,32 +2007,33 @@
 = new TestTask%5B%5D
+ 
 %7Btd.alice, td.be
@@ -2321,24 +2321,25 @@
 w TestTask%5B%5D
+ 
 %7Btd.alice, t
@@ -3095,24 +3095,24 @@
 nd(false));%0A
-
         expe
@@ -3136,16 +3136,17 @@
 stTask%5B%5D
+ 
 %7Btd.carl

Format array declaration