package Parser;

import commandFactory.CommandType;
import commonClasses.Constants;
import commonClasses.SummaryReport;

public class Parser {

	private ParsedResult result;
	private MainCommandInterpreter mainHandler;
	private OptionalCommandInterpreter optionHandler;

	public Parser() {
		mainHandler = new MainCommandInterpreter();
		optionHandler = new OptionalCommandInterpreter();
	}

	public ParsedResult parseString(String input) {
		result = new ParsedResult();

		// Processing first command and getting command param
		try {
			String[] seperatedInput = input.split("-");
			
			String commandWord = getCommandWord(seperatedInput[0]);
			mainHandler.identifyAndSetCommand(commandWord.toLowerCase());
			if (commandDoesNotRequireParam(mainHandler.getCommand())) {
				result.setCommandType(mainHandler.getCommand());
				return result;
			}
			String remainingInput = mainHandler.removeCommandWord(seperatedInput[0]);
			//String commandParam = getParam(remainingInput);

			//remainingInput = removeParam(remainingInput);
			result = mainHandler.updateResults(result, remainingInput.trim());

			// Processing optional commands
			processOptionalCommand(seperatedInput);

			if (result.getTaskDetails().getDueDate() == null) {
				result.getTaskDetails().setDueDate(Constants.SOMEDAY);

			}
			return result;
		} catch (Exception e) {
			result.setValidationResult(false);
			return result;
		}

		/*
		 * System.out.println(commandWord);
		 * 
		 * System.out.println(commandWord); System.out.println(commandParam);
		 * System.out.println(ParsedResult.getTaskDetails().getDueDate()
		 * .toLocalDate().toString("dd/MM/yyyy"));
		 * System.out.println(ParsedResult.getTaskDetails().getDueDate()
		 * .toLocalTime().toString("HH:mm"));
		 */
	}

	private void processOptionalCommand(String[] remainingInput) {
		String commandWord;

		for(int i=1; i<remainingInput.length;i++) {
			commandWord = getCommandWord(remainingInput[i]);

			optionHandler.identifyAndSetCommand(commandWord.toLowerCase());

			remainingInput[i] = optionHandler.removeCommandWord(remainingInput[i]);
			//commandParam = getParam(remainingInput[i]);


			result = optionHandler.updateResults(result, remainingInput[i].trim());

			//remainingInput = removeParam(remainingInput);
		}
	}

	private static boolean commandDoesNotRequireParam(CommandType command) {

		if (command == CommandType.UNDO)
			return true;

		return false;

	}

	private static String removeParam(String remainingInput) {
		int indexEndOfParam = remainingInput.indexOf(']');

		// Adjust index to take substring after ']'
		indexEndOfParam++;
		remainingInput = remainingInput.substring(indexEndOfParam);

		return remainingInput.trim();
	}

	private static String getCommandWord(String input) {
		String[] splittedCommand = input.split(" ");

		return splittedCommand[0];
	}

/*	private static String getParam(String remainingInput)
			throws InvalidParameterException {
		int indexStartOfParam = remainingInput.indexOf('[');
		int indexEndOfParam = remainingInput.indexOf(']');

		if (indexStartOfParam == -1 || indexEndOfParam == -1) {
			SummaryReport
					.setFeedBackMsg(Constants.MESSAGE_INVALID_PARAM_FORMATTING);
			throw new InvalidParameterException();
			// return remainingInput;
		}
		indexStartOfParam++; // adjust to get first letter of param
		remainingInput = remainingInput.substring(indexStartOfParam,
				indexEndOfParam);

		return remainingInput.trim();
	} */

}

@@ -926,112 +926,8 @@
 %5D);%0A
-%09%09%09//String commandParam = getParam(remainingInput);%0A%0A%09%09%09//remainingInput = removeParam(remainingInput);
 %0A%09%09%09
@@ -1193,34 +1193,16 @@
 ;%0A%0A%09%09%09%7D%0A
-%09%09%09return result;%0A
 %09%09%7D catc
@@ -1258,16 +1258,19 @@
 alse);%0A%09
+%09%7D%0A
 %09%09return
@@ -1282,359 +1282,8 @@
 lt;%0A
-%09%09%7D%0A%0A%09%09/*%0A%09%09 * System.out.println(commandWord);%0A%09%09 * %0A%09%09 * System.out.println(commandWord); System.out.println(commandParam);%0A%09%09 * System.out.println(ParsedResult.getTaskDetails().getDueDate()%0A%09%09 * .toLocalDate().toString(%22dd/MM/yyyy%22));%0A%09%09 * System.out.println(ParsedResult.getTaskDetails().getDueDate()%0A%09%09 * .toLocalTime().toString(%22HH:mm%22));%0A%09%09 */%0A
 %09%7D%0A%0A
@@ -1615,58 +1615,8 @@
 %5D);%0A
-%09%09%09//commandParam = getParam(remainingInput%5Bi%5D);%0A%0A
 %0A%09%09%09
@@ -1692,59 +1692,8 @@
 );%0A%0A
-%09%09%09//remainingInput = removeParam(remainingInput);%0A
 %09%09%7D%0A
@@ -1871,1005 +1871,118 @@
 ing 
-removeParam(String remainingInput) %7B%0A%09%09int indexEndOfParam = remainingInput.indexOf('%5D');%0A%0A%09%09// Adjust index to take substring after '%5D'%0A%09%09indexEndOfParam++;%0A%09%09remainingInput = remainingInput.substring(indexEndOfParam);%0A%0A%09%09return remainingInput.trim();%0A%09%7D%0A%0A%09private static String getCommandWord(String input) %7B%0A%09%09String%5B%5D splittedCommand = input.split(%22 %22);%0A%0A%09%09return splittedCommand%5B0%5D;%0A%09%7D%0A%0A/*%09private static String getParam(String remainingInput)%0A%09%09%09throws InvalidParameterException %7B%0A%09%09int indexStartOfParam = remainingInput.indexOf('%5B');%0A%09%09int indexEndOfParam = remainingInput.indexOf('%5D');%0A%0A%09%09if (indexStartOfParam == -1 %7C%7C indexEndOfParam == -1) %7B%0A%09%09%09SummaryReport%0A%09%09%09%09%09.setFeedBackMsg(Constants.MESSAGE_INVALID_PARAM_FORMATTING);%0A%09%09%09throw new InvalidParameterException();%0A%09%09%09// return remainingInput;%0A%09%09%7D%0A%09%09indexStartOfParam++; // adjust to get first letter of param%0A%09%09remainingInput = remainingInput.substring(indexStartOfParam,%0A%09%09%09%09indexEndOfParam);%0A%0A%09%09return remainingInput.trim();%0A%09%7D */
+getCommandWord(String input) %7B%0A%09%09String%5B%5D splittedCommand = input.split(%22 %22);%0A%0A%09%09return splittedCommand%5B0%5D;%0A%09%7D
 %0A%0A%7D%0A

Clean up code for Parser