package guitests;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import seedu.taskboss.commons.core.Messages;
import seedu.taskboss.logic.commands.RedoCommand;
import seedu.taskboss.testutil.TestTask;
import seedu.taskboss.testutil.TestUtil;

//@@author A0138961W
public class RedoCommandTest extends TaskBossGuiTest {

    @Test
    public void redo() {
        //without any last command
        commandBox.runCommand("redo");
        assertResultMessage(RedoCommand.MESSAGE_WITHOUT_PREVIOUS_OPERATION);

        //redo one undo command
        TestTask[] currentList = td.getTypicalTasks();
        TestTask taskToAdd = td.taskI;
        commandBox.runCommand(taskToAdd.getAddCommand());
        commandBox.runCommand("undo");
        TestTask[] expectedList = TestUtil.addTasksToList(currentList, taskToAdd);
        assertRedoCommandSuccess(false, expectedList);

        //redo operation fails when undo fails
        commandBox.runCommand("undo");
        commandBox.runCommand("redo");
        assertResultMessage(RedoCommand.MESSAGE_WITHOUT_PREVIOUS_OPERATION);

        //redo operation fails when apply delete operation after undo operation
        TestTask[] currentList = td.getTypicalTasks();
        TestTask taskToAdd = td.taskI;
        commandBox.runCommand(taskToAdd.getAddCommand());
        commandBox.runCommand("undo");
        commandBox.runCommand("delete 1");
        commandBox.runCommand("redo");
        assertResultMessage(RedoCommand.MESSAGE_WITHOUT_PREVIOUS_OPERATION);

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

    @Test
    public void redoShortCommand() {

        //without any last command
        commandBox.runCommand("r");
        assertResultMessage(RedoCommand.MESSAGE_WITHOUT_PREVIOUS_OPERATION);

        //redo one undo command
        TestTask[] currentList = td.getTypicalTasks();
        TestTask taskToAdd = td.taskI;
        commandBox.runCommand(taskToAdd.getAddCommand());
        commandBox.runCommand("undo");
        TestTask[] expectedList = TestUtil.addTasksToList(currentList, taskToAdd);
        assertRedoCommandSuccess(true, expectedList);

        //redo operation fails when undo fails
        commandBox.runCommand("u");
        commandBox.runCommand("r");
        assertResultMessage(RedoCommand.MESSAGE_WITHOUT_PREVIOUS_OPERATION);

        //redo operation fails when apply delete operation after undo operation
        TestTask[] currentList = td.getTypicalTasks();
        TestTask taskToAdd = td.taskI;
        commandBox.runCommand(taskToAdd.getAddCommand());
        commandBox.runCommand("u");
        commandBox.runCommand("d 1");
        commandBox.runCommand("r");
        assertResultMessage(RedoCommand.MESSAGE_WITHOUT_PREVIOUS_OPERATION);

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

    private void assertRedoCommandSuccess(boolean isShortCommand, TestTask[] expectedList) {
        if (isShortCommand) {
            commandBox.runCommand("r");
        } else {
            commandBox.runCommand("redo");
        }
        assertTrue(taskListPanel.isListMatching(expectedList));
        assertResultMessage(RedoCommand.MESSAGE_SUCCESS);
    }
}

@@ -1189,32 +1189,35 @@
 sk%5B%5D currentList
+One
  = td.getTypical
@@ -1243,32 +1243,35 @@
 stTask taskToAdd
+One
  = td.taskI;%0A   
@@ -1298,32 +1298,35 @@
 ommand(taskToAdd
+One
 .getAddCommand()
@@ -1914,32 +1914,35 @@
 sk%5B%5D currentList
+Two
  = td.getTypical
@@ -1968,32 +1968,35 @@
 stTask taskToAdd
+Two
  = td.taskI;%0A   
@@ -2023,32 +2023,35 @@
 ommand(taskToAdd
+Two
 .getAddCommand()
@@ -2153,32 +2153,35 @@
 List(currentList
+Two
 , taskToAdd);%0A  
@@ -2167,32 +2167,35 @@
 stTwo, taskToAdd
+Two
 );%0A        asser
@@ -2535,32 +2535,37 @@
 sk%5B%5D currentList
+Three
  = td.getTypical
@@ -2591,32 +2591,37 @@
 stTask taskToAdd
+Three
  = td.taskI;%0A   
@@ -2648,32 +2648,37 @@
 ommand(taskToAdd
+Three
 .getAddCommand()

Update RedoCommandTest.java