package seedu.doist.logic.parser;

import static seedu.doist.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.doist.logic.parser.CliSyntax.*;

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.doist.commons.exceptions.IllegalValueException;
import seedu.doist.logic.commands.AddCommand;
import seedu.doist.logic.commands.Command;
import seedu.doist.logic.commands.HelpCommand;
import seedu.doist.logic.commands.IncorrectCommand;

/**
 * Parses input arguments and creates a new AddCommand object
 */
public class AddCommandParser {

    private static final Pattern Add_Command_Regex = Pattern.compile("(?<preamble>[^\\\\]*)(?<parameters>((\\\\)(\\S+)(\\s+)([^\\\\]*))*)");
    
    ////argsTokenizer.getValue(PREFIX_PHONE).get() is an an example of how to use the tokenizer
    /**
     * Parses the given {@code String} of arguments in the context of the AddCommand
     * and returns an AddCommand object for execution.
     */
    public Command parse(String args) {
        final Matcher matcher = Add_Command_Regex.matcher(args.trim());
        if (!matcher.matches() || args.trim().equals("")) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
        }

        final String argument = matcher.group("preamble");
        final String parameters = matcher.group("parameters").trim();
        ArrayList<String> tokens = new ArrayList<String>();
        for(int i = 0; i < parameters.length(); i ++) {
            if(parameters.charAt(i) == '\\') {
                int start = i;
                while(parameters.charAt(i) != ' '){
                    i ++;
                }
                tokens.add(parameters.substring(start, i));     
            }
        }
        
        ArgumentTokenizer argsTokenizer =
              new ArgumentTokenizer(PREFIX_FROM, PREFIX_TO, PREFIX_REMIND, PREFIX_EVERY, PREFIX_AS, PREFIX_BY, PREFIX_UNDER);
        
        if(!argsTokenizer.validateTokens(tokens)){
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
        }
        argsTokenizer.tokenize(parameters); 

        try {
            return new AddCommand(argument, argsTokenizer.getTokenizedArguments());
        } catch (IllegalValueException ive) {
            return new IncorrectCommand(ive.getMessage());
        }
//       
//        try {
//            
//            return new AddCommand(
//                    argsTokenizer.getPreamble().get(),
//                    argsTokenizer.getValue(PREFIX_FROM).get(),
//                    argsTokenizer.getValue(PREFIX_TO).get(),
//                    argsTokenizer.getValue(PREFIX_REMIND).get(),
//                    argsTokenizer.getValue(PREFIX_EVERY).get(),
//                    argsTokenizer.getValue(PREFIX_AS).get(),
//                    argsTokenizer.getValue(PREFIX_BY).get(),
//                    argsTokenizer.getValue(PREFIX_UNDER).get()
//                    );
//        } catch (NoSuchElementException nsee) {
//            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
//        } catch (IllegalValueException ive) {
//           
//        }
    }

}

@@ -193,49 +193,8 @@
 st;%0A
-import java.util.NoSuchElementException;%0A
 impo
@@ -408,55 +408,8 @@
 nd;%0A
-import seedu.doist.logic.commands.HelpCommand;%0A
 impo
@@ -2387,865 +2387,12 @@
   %7D%0A
-//       %0A//        try %7B%0A//            %0A//            return new AddCommand(%0A//                    argsTokenizer.getPreamble().get(),%0A//                    argsTokenizer.getValue(PREFIX_FROM).get(),%0A//                    argsTokenizer.getValue(PREFIX_TO).get(),%0A//                    argsTokenizer.getValue(PREFIX_REMIND).get(),%0A//                    argsTokenizer.getValue(PREFIX_EVERY).get(),%0A//                    argsTokenizer.getValue(PREFIX_AS).get(),%0A//                    argsTokenizer.getValue(PREFIX_BY).get(),%0A//                    argsTokenizer.getValue(PREFIX_UNDER).get()%0A//                    );%0A//        %7D catch (NoSuchElementException nsee) %7B%0A//            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));%0A//        %7D catch (IllegalValueException ive) %7B%0A//           %0A//        %7D%0A    %7D%0A
+    %7D
 %0A%7D%0A

Remove redundant code