package guitests;

import org.junit.Test;
import seedu.address.commons.core.Messages;
import seedu.address.testutil.*;
import static org.junit.Assert.assertTrue;

//@@author A0125097A
public class UndoCommandTest extends AddressBookGuiTest {

    @Test
    public void undo_addCommand() {
        TestActivity[] currentList = td.getTypicalActivities();
        TestActivity activityToAdd = td.findHoon;
        assertUndoAddResult(activityToAdd,currentList);
        
    }
    
    @Test
    public void undo_deleteCommand() {
        TestActivity[] currentList = td.getTypicalActivities();
        int index = 2;
        assertUndoDeleteResult(index,currentList);
    }
    
    @Test
    public void undo_doneCommand() {
        TestActivity[] currentList = td.getTypicalActivities();
        int index = 4;
        assertUndoDoneResult(index,currentList);
    }

    @Test
    public void undo_editActivity() {
        TestActivity[] currentList = td.getTypicalActivities();
        int index = 1;
       
        
        assertUndoEditActivityResult(index,currentList);
    }
    
    
    
    
    
    
    private void assertUndoEditActivityResult(int index, TestActivity[] currentList) {
    	 String newName = "new name";
         String newReminder = "29-12-2021 2200";
         TestActivity activityBeforeEdit = new TestActivity(currentList[index-1]);
         TestActivity activityAfterEdit = new TestActivity(currentList[index-1]);
         activityAfterEdit.setName(newName);
         activityAfterEdit.setReminder(newReminder);
         String editCommand = "edit " + index + " n/" + newName + " r/" + newReminder;
    	
    	assertUndoEditResult(editCommand,activityBeforeEdit,activityAfterEdit,currentList);
		
	}

	private void assertUndoEditResult(String command, TestActivity activityAfterUndo, TestActivity activityBeforeUndo, TestActivity[] currentList) {
        commandBox.runCommand(command);
        commandBox.runCommand("undo"); 
        assertResultMessage(String.format("Undo: Editting task from: %1$s\nto: %2$s",
                activityAfterUndo.getAsText(),activityBeforeUndo.getAsText()));
        assertTrue(activityListPanel.isListMatching(currentList));
  
        
    }

    private void assertUndoAddResult(TestActivity activity,TestActivity... currentList){
        commandBox.runCommand(activity.getAddCommand());
        commandBox.runCommand("undo");
        assertResultMessage(String.format("Undo: Adding of new task: %1$s",activity.getAsText()));
        assertTrue(activityListPanel.isListMatching(currentList));
        
    }
    
    private void assertUndoDeleteResult(int index,TestActivity... currentList){
        TestActivity activityToDelete = currentList[index-1];
        
        commandBox.runCommand("delete " + index);
        commandBox.runCommand("undo");
        assertResultMessage(String.format("Undo: Deleting task: %1$s",activityToDelete.getAsText()));
        assertTrue(activityListPanel.isListMatching(currentList));
    }
    
    private void assertUndoDoneResult(int index,TestActivity... currentList){
        TestActivity activityToDone = currentList[index-1];
        
        commandBox.runCommand("done " + index);
        commandBox.runCommand("undo");
        assertResultMessage(String.format("Undo: Marked task as Completed: %1$s",activityToDone.getAsText()));
        assertTrue(activityListPanel.isListMatching(currentList));
        
    }
    
}

@@ -674,196 +674,8 @@
    %0A
-    @Test%0A    public void undo_doneCommand() %7B%0A        TestActivity%5B%5D currentList = td.getTypicalActivities();%0A        int index = 4;%0A        assertUndoDoneResult(index,currentList);%0A    %7D
 %0A%0A  
@@ -815,25 +815,16 @@
        %0A
-        %0A
         
@@ -891,33 +891,306 @@
     
-%0A    %0A    %0A    
+@Test%0A    public void undo_doneCommand() %7B%0A        TestActivity%5B%5D currentList = td.getTypicalActivities();%0A        int index = 4;%0A        assertUndoDoneResult(index,currentList);%0A    %7D%0A    %0A    @Test%0A    public void undo_noPreviousCommand() %7B  %0A        assertUndoNoPreviousCommand();%0A    %7D
 %0A    %0A
-    
+%0A%0A%09
 priv
@@ -1801,11 +1801,8 @@
 t);%0A
-%09%09%0A
 %09%7D%0A%0A
@@ -3502,10 +3502,173 @@
  %7D%0A    %0A
+    private void assertUndoNoPreviousCommand() %7B%0A        commandBox.runCommand(%22undo%22); %0A        assertResultMessage(%22There is no more commands to undo%22);%0A%09%7D%0A    %0A
 %7D%0A

Add undo no previous command