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 static seedu.ezdo.logic.parser.CliSyntax.PREFIX_DUEDATE;
import static seedu.ezdo.logic.parser.CliSyntax.PREFIX_PRIORITY;
import static seedu.ezdo.logic.parser.CliSyntax.PREFIX_STARTDATE;
import static seedu.ezdo.logic.parser.CliSyntax.PREFIX_TAG;

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

import seedu.ezdo.commons.exceptions.IllegalValueException;
import seedu.ezdo.logic.commands.Command;
import seedu.ezdo.logic.commands.FindCommand;
import seedu.ezdo.logic.commands.IncorrectCommand;
import seedu.ezdo.logic.parser.ArgumentTokenizer.Prefix;
import seedu.ezdo.model.todo.Priority;
import seedu.ezdo.model.todo.TaskDate;

/**
 * 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));
        }
        assert args != null;

        ArgumentTokenizer argsTokenizer = new ArgumentTokenizer(PREFIX_PRIORITY, PREFIX_STARTDATE, PREFIX_DUEDATE,
                PREFIX_TAG);
        argsTokenizer.tokenize(args);
        String namesToMatch = argsTokenizer.getPreamble().orElse(""); //eat sleep rave relax
        String[] splitNames = namesToMatch.split("\\s+");

        Optional<Priority> findPriority;
        Optional<TaskDate> findStartDate;
        Optional<TaskDate> findDueDate;
        Set<String> findTags;

        try {
            findPriority = ParserUtil.parsePriority(getOptionalValue(argsTokenizer, PREFIX_PRIORITY));
            findStartDate = ParserUtil.parseStartDate(getOptionalValue(argsTokenizer, PREFIX_STARTDATE), true);
            findDueDate = ParserUtil.parseDueDate(getOptionalValue(argsTokenizer, PREFIX_DUEDATE), true);
            findTags = ParserUtil.toSet(argsTokenizer.getAllValues(PREFIX_TAG));

        } catch (IllegalValueException ive) {
            return new IncorrectCommand(ive.getMessage());
        }

        Set<String> keywords = new HashSet<String>(Arrays.asList(splitNames));
        return new FindCommand(keywords, findPriority, findStartDate, findDueDate, findTags);
    }

    private Optional<String> getOptionalValue(ArgumentTokenizer tokenizer, Prefix prefix) {
        Optional<String> optionalString;
        if (!tokenizer.getValue(prefix).isPresent()) {
            optionalString = Optional.empty();
        } else {
            optionalString = Optional.of(tokenizer.getValue(prefix).get());
        }
        return optionalString;
    }

}

@@ -1761,31 +1761,8 @@
 %22%22);
- //eat sleep rave relax
 %0A   
@@ -1894,24 +1894,31 @@
 indStartDate
+ = null
 ;%0A        Op
@@ -1945,16 +1945,23 @@
 dDueDate
+ = null
 ;%0A      
@@ -1999,16 +1999,17 @@
   try %7B%0A
+%0A
         
@@ -2016,48 +2016,179 @@
     
-findPriority = ParserUtil.parsePriority(
+boolean isFind = true;%0A            Optional%3CString%3E optionalStartDate = getOptionalValue(argsTokenizer, PREFIX_STARTDATE); %0A            Optional%3CString%3E optionalDueDate = 
 getO
@@ -2226,17 +2226,15 @@
 FIX_
-PRIORITY)
+DUEDATE
 );%0A 
@@ -2290,63 +2290,33 @@
 ate(
-getO
+o
 ptional
-Value(argsTokenizer, PREFIX_STARTDATE), true
+StartDate, isFind
 );%0A 
@@ -2368,61 +2368,31 @@
 ate(
-getO
+o
 ptional
-Value(argsTokenizer, PREFIX_DUEDATE), true
+DueDate, isFind
 );%0A 
@@ -2470,16 +2470,119 @@
 X_TAG));
+%0A            findPriority = ParserUtil.parsePriority(getOptionalValue(argsTokenizer, PREFIX_PRIORITY));
 %0A%0A      
@@ -2867,24 +2867,25 @@
 gs);%0A    %7D%0A%0A
+%0A
     private 
@@ -3248,10 +3248,11 @@
 %0A    %7D%0A%0A
+%0A
 %7D%0A

Modify parser to take in natural language when finding tasks