package seedu.stask.logic.commands;

import seedu.stask.commons.util.CommandUtil;
import seedu.stask.model.task.UniqueTaskList;
import seedu.stask.model.undo.UndoTask;

//@@author A0139145E
/**
 * undo the previous command
 */
public class RedoCommand extends Command implements Undoable{

    public static final String COMMAND_WORD = "redo";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Redoes the last undone action from Task Book in this session\n"
            + "Example: " + COMMAND_WORD;

    public static final String MESSAGE_SUCCESS = "Undo action \"%1$s\" has been reverted";

    public static final String MESSAGE_REDO_NOT_POSSIBLE = "There are no actions available for redo";

    private UndoTask toRedo;
    
    public RedoCommand() {}

    @Override
    public CommandResult execute() {
        assert model != null;
        toRedo = model.redoTask();

        //No undoable action found.
        if (toRedo == null) { 
            return new CommandResult(MESSAGE_REDO_NOT_POSSIBLE);
        }

        int duplicateOrClashTaskResult = 0;

        try {
            switch (toRedo.getCommand()){

            case AddCommand.COMMAND_WORD:
                duplicateOrClashTaskResult = model.addTask(toRedo.getPostData());
                break;

            case DeleteCommand.COMMAND_WORD:
                model.deleteTask(toRedo.getPostData());
                break;

            case EditCommand.COMMAND_WORD:
                model.deleteTask(toRedo.getPreData());  
                duplicateOrClashTaskResult = model.addTask(toRedo.getPostData());               
                break;

            case DoneCommand.COMMAND_WORD:
                model.completeTask(toRedo.getPostData());               
                break;

            }

            populateUndo();
            
            //Determine if duplicate exist
            
            CommandResult temporary = new CommandResult(String.format(MESSAGE_SUCCESS, toRedo.getCommand()));
            return CommandUtil.generateCommandResult(temporary,duplicateOrClashTaskResult);

        }
        catch (UniqueTaskList.TaskNotFoundException tnfe){
            assert false : "Task not found not possible";
        return new CommandResult(MESSAGE_REDO_NOT_POSSIBLE);
        }

    }
    
    @Override
    public void populateUndo(){
        assert COMMAND_WORD != null;
        assert toRedo != null;
        model.addUndo(toRedo);
    }
}
//@@author

@@ -280,16 +280,17 @@
 Undoable
+ 
 %7B%0A%0A    p
@@ -842,32 +842,33 @@
  model != null;%0A
+%0A
         toRedo =
@@ -890,75 +890,78 @@
 ();%0A
-%0A        //No undoable action found.%0A        if (toRedo == null) %7B 
+        if (toRedo == null) %7B%0A            //No undoable action to redo
 %0A   
@@ -1077,17 +1077,16 @@
 lt = 0;%0A
-%0A
         
@@ -1131,18 +1131,18 @@
 mmand())
+ 
 %7B
-%0A
 %0A       
@@ -1166,32 +1166,33 @@
 and.COMMAND_WORD
+ 
 :%0A              
@@ -1274,33 +1274,32 @@
          break;%0A
-%0A
             case
@@ -1317,32 +1317,33 @@
 and.COMMAND_WORD
+ 
 :%0A              
@@ -1399,33 +1399,32 @@
          break;%0A
-%0A
             case
@@ -1440,32 +1440,33 @@
 and.COMMAND_WORD
+ 
 :%0A              
@@ -1620,33 +1620,32 @@
          break;%0A
-%0A
             case
@@ -1669,16 +1669,17 @@
 AND_WORD
+ 
 :%0A      
@@ -1768,16 +1768,115 @@
  break;%0A
+            default :%0A                assert false : %22Command not possible%22;%0A                break;
 %0A       
@@ -1874,33 +1874,32 @@
 ;%0A            %7D%0A
-%0A
             popu
@@ -1914,77 +1914,8 @@
 ();%0A
-            %0A            //Determine if duplicate exist%0A            %0A
     
@@ -1997,16 +1997,65 @@
 UCCESS, 
+%0A                                                
 toRedo.g
@@ -2132,16 +2132,54 @@
 mporary,
+ %0A                                    
 duplicat
@@ -2199,17 +2199,16 @@
 esult);%0A
-%0A
         
@@ -2266,16 +2266,17 @@
 on tnfe)
+ 
 %7B%0A      
@@ -2394,25 +2394,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A    %0A 
@@ -2453,16 +2453,17 @@
 teUndo()
+ 
 %7B%0A      

Refactor code quality