package seedu.ezdo.logic.parser;

import static seedu.ezdo.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.ezdo.logic.parser.CliSyntax.KEYWORDS_ARGS_FORMAT;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;

import seedu.ezdo.logic.commands.Command;
import seedu.ezdo.logic.commands.FindCommand;
import seedu.ezdo.logic.commands.IncorrectCommand;

/**
 * Parses input arguments and creates a new FindCommand object
 */
public class FindCommandParser implements CommandParser {

    /**
     * Parses the given {@code String} of arguments in the context of the FindCommand
     * and returns an FindCommand object for execution.
     */
    @Override
    public Command parse(String args) {
        final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(args.trim());
        if (!matcher.matches()) {
            return new IncorrectCommand(
                    String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
        }

        // keywords delimited by whitespace
        final String[] keywords = matcher.group("keywords").split("\\s+");
        final Set<String> keywordSet = new HashSet<>(Arrays.asList(keywords));
        return new FindCommand(keywordSet);
    }

}

@@ -175,16 +175,271 @@
 _FORMAT;
+%0Aimport static seedu.ezdo.logic.parser.CliSyntax.PREFIX_DUEDATE;%0Aimport static seedu.ezdo.logic.parser.CliSyntax.PREFIX_PRIORITY;%0Aimport static seedu.ezdo.logic.parser.CliSyntax.PREFIX_STARTDATE;%0Aimport static seedu.ezdo.logic.parser.CliSyntax.PREFIX_TAG;
 %0A%0Aimport
@@ -479,24 +479,74 @@
 il.HashSet;%0A
+import java.util.List;%0Aimport java.util.Optional;%0A
 import java.
@@ -584,24 +584,84 @@
 x.Matcher;%0A%0A
+import seedu.ezdo.commons.exceptions.IllegalValueException;%0A
 import seedu
@@ -786,16 +786,184 @@
 Command;
+%0Aimport seedu.ezdo.logic.parser.ArgumentTokenizer.Prefix;%0Aimport seedu.ezdo.model.tag.Tag;%0Aimport seedu.ezdo.model.todo.Priority;%0Aimport seedu.ezdo.model.todo.TaskDate;
 %0A%0A/**%0A *
@@ -1167,16 +1167,23 @@
 t of the
+%0A     *
  FindCom
@@ -1186,23 +1186,16 @@
 dCommand
-%0A     *
  and ret
@@ -1451,29 +1451,8 @@
 and(
-%0A                    
 Stri
@@ -1536,141 +1536,1085 @@
   %7D%0A
-%0A        // keywords delimited by whitespace%0A        final String%5B%5D keywords = matcher.group(%22keywords%22).split(%22%5C%5Cs+%22);%0A        final
+        assert args != null;%0A %0A        ArgumentTokenizer argsTokenizer = new ArgumentTokenizer(PREFIX_PRIORITY, PREFIX_STARTDATE, PREFIX_DUEDATE,%0A                PREFIX_TAG);%0A        argsTokenizer.tokenize(args);%0A        String namesToMatch = argsTokenizer.getPreamble().orElse(%22%22); //eat sleep rave relax%0A        String%5B%5D splitNames = namesToMatch.split(%22%5C%5Cs+%22);%0A        Optional%3CPriority%3E findPriority;%0A        Optional%3CTaskDate%3E findStartDate;%0A        Optional%3CTaskDate%3E findDueDate;%0A        Set%3CString%3E findTags;%0A        try %7B%0A            findPriority = ParserUtil.parsePriority(getOptionalValue(argsTokenizer, PREFIX_PRIORITY));%0A            findStartDate = ParserUtil.parseStartDate(getOptionalValue(argsTokenizer, PREFIX_STARTDATE), true);%0A            findDueDate = ParserUtil.parseDueDate(getOptionalValue(argsTokenizer, PREFIX_DUEDATE), true);%0A            findTags = ParserUtil.toSet(argsTokenizer.getAllValues(PREFIX_TAG));%0A            %0A        %7D catch (IllegalValueException ive) %7B%0A            return new IncorrectCommand(ive.getMessage());%0A        %7D%0A        %0A       
  Set
@@ -2629,19 +2629,17 @@
  keyword
-Set
+s
  = new H
@@ -2645,16 +2645,22 @@
 HashSet%3C
+String
 %3E(Arrays
@@ -2667,23 +2667,25 @@
 .asList(
-keyword
+splitName
 s));%0A   
@@ -2723,12 +2723,438 @@
 word
-Set)
+s, findPriority, findStartDate, findDueDate, findTags);%0A    %7D%0A%0A    private Optional%3CString%3E getOptionalValue(ArgumentTokenizer tokenizer, Prefix prefix) %7B%0A        Optional%3CString%3E optionalString;%0A        if (!tokenizer.getValue(prefix).isPresent()) %7B%0A            optionalString = Optional.empty();%0A        %7D else %7B%0A            optionalString = Optional.of(tokenizer.getValue(prefix).get());%0A        %7D%0A        return optionalString
 ;%0A  

Allow parser to parse multiple fields for FindCommand