package taskDo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import parser.ParsedResult;

import commandFactory.CommandAction;
import commandFactory.CommandFactory;
import commandFactory.CommandType;
import commonClasses.Constants;
import commonClasses.StorageList;
import commonClasses.SummaryReport;

public class Executor {
	
	private static final Logger log = LogManager.getLogger(Executor.class);

	public Executor() {
		StorageList.getInstance().loadFile();
	}

	public void execute(ParsedResult parsedResult) {		
		CommandFactory commandFactory = new CommandFactory();
		CommandType commandType = parsedResult.getCommandType();
		log.info("Command [" + commandType.toString() + "].");
		if (commandType.equals(CommandType.UNDO)) {
			executeUndo(parsedResult);
		}else	if(commandType.equals(CommandType.REDO)){
			executeRedo(parsedResult);
		}else{
			executeCommand(parsedResult, commandFactory, commandType);
		}
		
		CategoryList.updateCategoryList(StorageList.getInstance().getTaskList());
	}

	private void executeUndo(ParsedResult parsedResult) {
		History history = History.getInstance();
		if(!history.getUndoActionHistory().empty()){
			CommandAction commandAction = history.getUndoActionHistory().pop();
			Task lastTask = history.getUndoTaskHistory().pop();

			history.getRedoActionHistory().push(commandAction);

			parsedResult.setTask(lastTask);
			commandAction.undo(parsedResult);
		}else{
			history.getUndoTaskHistory().clear();
			SummaryReport.setFeedBackMsg(Constants.MESSAGE_FAIL_UNDO);
		}
	}

	private void executeRedo(ParsedResult parsedResult) {
		History history = History.getInstance();
		if(!history.getRedoActionHistory().empty()){
			CommandAction commandAction = history.getRedoActionHistory().pop();
			Task lastTask = history.getRedoTaskHistory().pop();
			history.getUndoActionHistory().push(commandAction);
			parsedResult.setTask(lastTask);
			commandAction.execute(parsedResult);
		}else{
			history.getRedoTaskHistory().clear();
			SummaryReport.setFeedBackMsg(Constants.MESSAGE_FAIL_REDO);
		}
	}

	private void executeCommand(ParsedResult parsedResult,
			CommandFactory commandFactory, CommandType commandType) {
		CommandAction commandAction = commandFactory.getCommandAction(commandType);			
		commandAction.execute(parsedResult);
		if(!commandType.equals(CommandType.DISPLAY)&&!commandType.equals(CommandType.SEARCH)){
			History.getInstance().getUndoActionHistory().push(commandAction);
		}
	}
}
@@ -361,18 +361,16 @@
 cutor %7B%0A
-%09%0A
 %09private
@@ -667,16 +667,19 @@
 Type();%0A
+%09%09%0A
 %09%09log.in
@@ -727,16 +727,19 @@
  %22%5D.%22);%0A
+%09%09%0A
 %09%09if (co
@@ -1148,70 +1148,57 @@
 ;%0A%09%09
-if(!history.getU
+Comma
 nd
-o
 Action
-History().empty())%7B%0A%09%09%09CommandAction 
+ commandAction = null;%0A%09%09%0A%09%09try%7B%0A%09%09%09
 comm
@@ -1426,37 +1426,105 @@
 rsedResult);%0A%09%09%7D
-else%7B
+catch(Exception e)%7B%0A%09%09%09log.info(%22no more undo command %5B%22 + e + %22%5D.%22);%0A%09%09%09
 %0A%09%09%09history.getU
@@ -1723,70 +1723,57 @@
 ;%0A%09%09
-if(!history.getRedoActionHistory().empty())%7B%0A%09%09%09CommandAction 
+CommandAction commandAction = null;%0A%09%09%0A%09%09try%7B%0A%09%09%09
 comm
@@ -1877,16 +1877,20 @@
 .pop();%0A
+%09%09%09%0A
 %09%09%09histo
@@ -1936,16 +1936,20 @@
 ction);%0A
+%09%09%09%0A
 %09%09%09parse
@@ -2014,29 +2014,93 @@
 Result);%0A%09%09%7D
-else%7B
+catch(Exception e)%7B%0A%09%09%09log.info(%22no more redo command %5B%22 + e + %22%5D.%22);
 %0A%09%09%09history.
@@ -2316,16 +2316,62 @@
 Type) %7B%0A
+%09%09%0A%09%09History history = History.getInstance();%0A
 %09%09Comman
@@ -2393,16 +2393,43 @@
 Action =
+ null;%0A%09%09%0A%09%09commandAction =
  command
@@ -2466,19 +2466,16 @@
 ndType);
-%09%09%09
 %0A%09%09comma
@@ -2508,19 +2508,213 @@
 ult);%0A%09%09
-if(
+%0A%09%09if(isNotDisplayAndSearch(commandType))%7B%0A%09%09%09history.getUndoActionHistory().push(commandAction);%0A%09%09%7D%0A%09%7D%0A%0A%09private boolean isNotDisplayAndSearch(CommandType commandType) %7B%0A%09%09boolean isNotDisplay = 
 !command
@@ -2749,10 +2749,34 @@
 LAY)
-&&
+;%0A%09%09boolean isNotSearch = 
 !com
@@ -2814,83 +2814,47 @@
 RCH)
-)%7B%0A%09%09%09History.getInstance().getUndoActionHistory().push(commandAction);%0A%09%09%7D
+;%0A%09%09return isNotDisplay && isNotSearch;
 %0A%09%7D%0A

refactor executor