package guitests;

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

import org.junit.Test;
import java.util.EmptyStackException;
import java.util.Stack;

import guitests.guihandles.TaskCardHandle;
import javafx.collections.ObservableList;
import seedu.jobs.commons.core.Messages;
import seedu.jobs.logic.commands.RedoCommand;
import seedu.jobs.model.task.UniqueTaskList;
import seedu.jobs.model.task.Task;
import seedu.jobs.model.task.UniqueTaskList.IllegalTimeException;
import seedu.jobs.model.FixedStack;
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));
  }
}

@@ -786,17 +786,18 @@
 iTest %7B%0A
-%09
+  
 private 
@@ -857,16 +857,21 @@
 ();%0A
-%09
+  %0A  
 @Test%0A
-%09
+  
 publ
@@ -962,18 +962,20 @@
 ption %7B%0A
-%09%09
+    
 commandB
@@ -997,18 +997,20 @@
 redo%22);%0A
-%09%09
+    
 assertRe
@@ -1039,18 +1039,20 @@
 ILURE);%0A
-%09%09
+    
 TestTask
@@ -1090,18 +1090,20 @@
 asks();%0A
-%09%09
+    
 // add a
@@ -1138,18 +1138,20 @@
 redoing%0A
-%09%09
+    
 TestTask
@@ -1174,18 +1174,20 @@
 CS4101;%0A
-%09%09
+    
 commandB
@@ -1228,18 +1228,20 @@
 and());%0A
-%09%09
+    
 currentL
@@ -1295,18 +1295,20 @@
 ToAdd);%0A
-%09%09
+    
 undoForR
@@ -1325,18 +1325,20 @@
 tList);%0A
-%09%09
+    
 //TestTa
@@ -1391,18 +1391,20 @@
 h - 1%5D;%0A
-%09%09
+    
 currentL
@@ -1463,18 +1463,20 @@
 ToAdd);%0A
-%09%09
+    
 assertRe
@@ -1846,34 +1846,36 @@
 st.length - 1%5D;%0A
-%09%09
+    
 undoForRedo(curr
@@ -1876,34 +1876,36 @@
 o(currentList);%0A
-%09%09
+    
 currentList = Te
@@ -1958,18 +1958,20 @@
 done1);%0A
-%09%09
+    
 TestTask
@@ -2022,18 +2022,20 @@
 h - 1%5D;%0A
-%09%09
+    
 undoForR
@@ -2126,18 +2126,20 @@
 done2);%0A
-%09%09
+    
 // doubl
@@ -2150,18 +2150,20 @@
 do test%0A
-%09%09
+    
 assertRe
@@ -2178,34 +2178,36 @@
 s(currentList);%0A
-%09%09
+    
 currentList = Te
@@ -2255,18 +2255,20 @@
 done2);%0A
-%09%09
+    
 assertRe
@@ -2291,18 +2291,20 @@
 tList);%0A
-%09%09
+    
 currentL
@@ -2441,12 +2441,17 @@
 E);%0A
-%09%7D%0A%09
+  %7D%0A  %0A  
 priv
@@ -2487,35 +2487,38 @@
 %5B%5D currentList)%0A
-%09%09%09
+      
 throws IllegalAr
@@ -2578,18 +2578,20 @@
 ption %7B%0A
-%09%09
+    
 TestTask
@@ -2641,18 +2641,20 @@
 h - 1%5D;%0A
-%09%09
+    
 testStac
@@ -2673,18 +2673,20 @@
 ndone);%0A
-%09%09
+    
 commandB
@@ -2712,10 +2712,14 @@
 %22);%0A
-%09%7D
+  %7D%0A  
 %0A  p
@@ -2776,11 +2776,14 @@
 st)%0A
-%09%09%09
+      
 thro

fix check style 2