package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.jobs.logic.commands.RedoCommand.MESSAGE_FAILURE;
import static seedu.jobs.logic.commands.RedoCommand.MESSAGE_SUCCESS;

import java.util.EmptyStackException;
import java.util.Stack;

import org.junit.Test;

import seedu.jobs.model.task.UniqueTaskList.IllegalTimeException;
import seedu.jobs.testutil.TestTask;
import seedu.jobs.testutil.TestUtil;

//@@author A0140055W

public class RedoCommandTest extends TaskBookGuiTest {
    private final Stack<TestTask> testStack = new Stack<TestTask>();

    @Test
    public void redo() throws IllegalArgumentException, IllegalTimeException, EmptyStackException {
        commandBox.runCommand("redo");
        assertResultMessage(MESSAGE_FAILURE);
        TestTask[] currentList = td.getTypicalTasks();
        // add a task for undoing to enable redoing
        TestTask taskToAdd = td.CS4101;
        commandBox.runCommand(td.CS4101.getAddCommand());
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        undoForRedo(currentList);
        // TestTask taskUndone1 = currentList[currentList.length - 1];
        currentList = TestUtil.removeTasksFromList(currentList, taskToAdd);
        assertRedoSuccess(currentList);
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        // add another task
        taskToAdd = td.CS4102;
        commandBox.runCommand(td.CS4102.getAddCommand());
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        // undo two times for upcoming double undo test
        TestTask taskUndone1 = currentList[currentList.length - 1];
        undoForRedo(currentList);
        currentList = TestUtil.removeTasksFromList(currentList, taskUndone1);
        TestTask taskUndone2 = currentList[currentList.length - 1];
        undoForRedo(currentList);
        currentList = TestUtil.removeTasksFromList(currentList, taskUndone2);
        // double redo test
        assertRedoSuccess(currentList);
        currentList = TestUtil.addTasksToList(currentList, taskUndone2);
        assertRedoSuccess(currentList);
        currentList = TestUtil.addTasksToList(currentList, taskUndone1);
        commandBox.runCommand("redo");
        assertResultMessage(MESSAGE_FAILURE);
    }

    private void undoForRedo(TestTask[] currentList)
            throws IllegalArgumentException, IllegalTimeException, EmptyStackException {
        TestTask taskUndone = currentList[currentList.length - 1];
        testStack.push(taskUndone);
        commandBox.runCommand("undo");
    }

    private void assertRedoSuccess(TestTask[] currentList)
            throws IllegalArgumentException, IllegalTimeException, EmptyStackException {
        TestTask taskToBeRedone = testStack.pop();
        TestTask[] expectedResult = TestUtil.addTasksToList(currentList, taskToBeRedone);
        commandBox.runCommand("redo");
        // confirm the resultant list after redoing matches the original
        assertTrue(taskListPanel.isListMatching(expectedResult));
        // confirm that the result message is correct
        assertResultMessage(String.format(MESSAGE_SUCCESS, taskToBeRedone));
    }
}

@@ -198,71 +198,8 @@
 S;%0A%0A
-import java.util.EmptyStackException;%0Aimport java.util.Stack;%0A%0A
 impo
@@ -363,30 +363,8 @@
 l;%0A%0A
-//@@author A0140055W%0A%0A
 publ
@@ -418,77 +418,33 @@
 t %7B%0A
+%0A
     
-private final Stack%3CTestTask%3E testStack = new Stack%3CTestTask%3E();%0A
+//@@author A0164440M
 %0A   
@@ -470,16 +470,20 @@
 oid redo
+Test
 () throw
@@ -534,32 +534,12 @@
 tion
-, EmptyStackException
  %7B%0A
+%0A
     
@@ -614,24 +614,70 @@
 GE_FAILURE);
+%0A%0A        // to add a random task to be undone
 %0A        Tes
@@ -732,909 +732,205 @@
     
-// add a task for undoing to enable redoing%0A        TestTask taskToAdd = td.CS4101;%0A        commandBox.runCommand(td.CS4101.getAddCommand());%0A        currentList = TestUtil.addTasksToList(currentList, taskToAdd);%0A        undoForRedo(currentList);%0A        // TestTask taskUndone1 = currentList%5BcurrentList.length - 1%5D;%0A        currentList = TestUtil.removeTasksFromList(currentList, taskToAdd);%0A        assertRedoSuccess(currentList);%0A        currentList = TestUtil.addTasksToList(currentList, taskToAdd);%0A        // add another task%0A        taskToAdd = td.CS4102;%0A        commandBox.runCommand(td.CS4102.getAddCommand());%0A        currentList = TestUtil.addTasksToList(currentList, taskToAdd);%0A        // undo two times for upcoming double undo test%0A        TestTask taskUndone1 = currentList%5BcurrentList.length - 1%5D;%0A        undoForRedo(currentList);%0A        currentList = TestUtil.removeTasksFromList
+TestTask taskToDelete = currentList%5BcurrentList.length - 1%5D;%0A        commandBox.runCommand(%22delete %22 + td.getTypicalTasks().length);%0A        commandBox.runCommand(%22undo%22);%0A        assertRedoSuccess
 (cur
@@ -947,1031 +947,257 @@
 task
-Undone1
+ToDelete
 );%0A    
+%7D%0A
     
-TestTask taskUndone2 = currentList%5BcurrentList.length - 1%5D;%0A        undoForRedo(currentList);%0A        currentList = TestUtil.removeTasksFromList(currentList, taskUndone2);%0A        // double redo test%0A        assertRedoSuccess(currentList);%0A        currentList = TestUtil.addTasksToList(currentList, taskUndone2);%0A        assertRedoSuccess(currentList);%0A        currentList = TestUtil.addTasksToList(currentList, taskUndone1);%0A        commandBox.runCommand(%22redo%22);%0A        assertResultMessage(MESSAGE_FAILURE);%0A    %7D%0A%0A    private void undoForRedo(TestTask%5B%5D currentList)%0A            throws IllegalArgumentException, IllegalTimeException, EmptyStackException %7B%0A        TestTask taskUndone = currentList%5BcurrentList.length - 1%5D;%0A        testStack.push(taskUndone);%0A        commandBox.runCommand(%22undo%22);%0A    %7D%0A%0A    private void assertRedoSuccess(TestTask%5B%5D currentList)%0A            throws IllegalArgumentException, IllegalTimeException, EmptyStackException %7B%0A        TestTask taskToBeRedone = testStack.pop(
+//@@author%0A%0A%0A  //@@author A0140055W%0A    private void assertRedoSuccess(final TestTask%5B%5D currentList, TestTask taskToBeAdd)%0A            throws IllegalArgumentException, IllegalTimeException %7B%0A%0A        commandBox.runCommand(%22redo%22
 );%0A 
@@ -1244,18 +1244,23 @@
 til.
-add
+remove
 Tasks
-To
+From
 List
@@ -1285,128 +1285,14 @@
 ToBe
-Redone);%0A        commandBox.runCommand(%22redo%22);%0A        // confirm the resultant list after redoing matches the original
+Add);%0A
 %0A   
@@ -1358,61 +1358,8 @@
 ));%0A
-        // confirm that the result message is correct
 %0A   
@@ -1426,18 +1426,16 @@
 ToBe
-Redone
+Add
 ));%0A
+%0A
     

Fix redo test