package Parser;

import java.security.InvalidParameterException;


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 commandWord = getCommandWord(input);
			mainHandler.identifyAndSetCommand(commandWord.toLowerCase());
			if (commandDoesNotRequireParam(mainHandler.getCommand())) {
				result.setCommandType(mainHandler.getCommand());
				return result;
			}
			String remainingInput = mainHandler.removeCommandWord(input);
			String commandParam = getParam(remainingInput);

			remainingInput = removeParam(remainingInput);
			result = mainHandler.updateResults(result, commandParam);

			// Processing optional commands


			processOptionalCommand(remainingInput);

			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;
		String commandParam;

		while (remainingInput.isEmpty() == false) {
			commandWord = getCommandWord(remainingInput);

			optionHandler.identifyAndSetCommand(commandWord.toLowerCase());

			remainingInput = optionHandler.removeCommandWord(remainingInput);
			commandParam = getParam(remainingInput);


			result = optionHandler.updateResults(result, commandParam);

			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();
	}

}

@@ -580,16 +580,63 @@
 %09%09try %7B%0A
+%09%09%09String%5B%5D seperatedInput = input.split(%22-%22);%0A
 %09%09%09Strin
@@ -662,29 +662,41 @@
 CommandWord(
-i
+seperatedI
 nput
+%5B1%5D
 );%0A%09%09%09mainHa
@@ -952,19 +952,33 @@
 ord(
-i
+seperatedI
 nput
+%5B1%5D
 );%0A%09%09%09
+//
 Stri
@@ -1021,24 +1021,26 @@
 Input);%0A%0A%09%09%09
+//
 remainingInp
@@ -1115,36 +1115,45 @@
 lts(result, 
-commandParam
+remainingInput.trim()
 );%0A%0A%09%09%09// Pr
@@ -1207,25 +1207,25 @@
 Command(
-remaining
+seperated
 Input);%0A
@@ -1839,24 +1839,26 @@
 mmand(String
+%5B%5D
  remainingIn
@@ -1916,48 +1916,49 @@
 %0A%0A%09%09
-while (remainingInput.isEmpty() == false
+for(int i=4; i%3CremainingInput.length;i+=2
 ) %7B%0A
@@ -1999,24 +1999,27 @@
 mainingInput
+%5Bi%5D
 );%0A%0A%09%09%09optio
@@ -2095,16 +2095,19 @@
 ingInput
+%5Bi%5D
  = optio
@@ -2147,22 +2147,27 @@
 ingInput
+%5Bi%5D
 );%0A%09%09%09
+//
 commandP
@@ -2192,24 +2192,27 @@
 mainingInput
+%5Bi%5D
 );%0A%0A%0A%09%09%09resu
@@ -2256,27 +2256,41 @@
 lt, 
-commandParam
+remainingInput%5Bi%5D.trim()
 );%0A%0A%09%09%09
+//
 rema

New approach to Parsing the string. "-add" to add task