package seedu.taskboss.logic.parser;

import static seedu.taskboss.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.taskboss.logic.parser.CliSyntax.KEYWORDS_ARGS_FORMAT;
import static seedu.taskboss.logic.parser.CliSyntax.PREFIX_END_DATE;
import static seedu.taskboss.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.taskboss.logic.parser.CliSyntax.PREFIX_START_DATE;

import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.regex.Matcher;

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

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

    /**
     * Parses the given {@code String} of arguments in the context of the FindCommand
     * and returns an FindCommand object for execution.
     */
    public Command parse(String args) {
        ArgumentTokenizer argsTokenizer =
                new ArgumentTokenizer(PREFIX_NAME, PREFIX_START_DATE, PREFIX_END_DATE);
        argsTokenizer.tokenize(args);
        String nameValue = checkEmpty(argsTokenizer.getValue(PREFIX_NAME));
        String startDatetimeValue = checkEmpty(argsTokenizer.getValue(PREFIX_START_DATE));
        String endDatetimeValue = checkEmpty(argsTokenizer.getValue(PREFIX_END_DATE));

        if (notValid(nameValue, startDatetimeValue, endDatetimeValue)) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
                    FindCommand.MESSAGE_USAGE));
        } else {
            String keywords = getValue(nameValue, startDatetimeValue, endDatetimeValue);
            String prefix = getPrefixString(nameValue, startDatetimeValue, endDatetimeValue);

            final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(keywords.trim());
            if (!matcher.matches()) {
                return new IncorrectCommand(
                        String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
            }
            // keywords delimited by whitespace
            return new FindCommand(prefix, keywords);
        }

    }

    private String checkEmpty(Optional<String> test) {
        try {
            return test.get();
        } catch (NoSuchElementException nsee) {
            return "";
        }
    }

    private boolean notValid(String test1, String test2, String test3) {
        String sum = test1 + test2 + test3;
        return (!sum.equals(test1) && !sum.equals(test2) && !sum.equals(test3));
    }

    private String getValue(String test1, String test2, String test3) {
        return test1 + test2 + test3;
    }

    private String getPrefixString(String test1, String test2, String test3) {
        String sum = test1 + test2 + test3;
        if (sum.equals(test1)) {
            return PREFIX_NAME.toString();
        } else if (sum.equals(test2)) {
            return PREFIX_START_DATE.toString();
        } else {
            return PREFIX_END_DATE.toString();
        }
    }

}

@@ -257,16 +257,88 @@
 D_DATE;%0A
+import static seedu.taskboss.logic.parser.CliSyntax.PREFIX_INFORMATION;%0A
 import s
@@ -824,16 +824,93 @@
 rser %7B%0A%0A
+    private static final String EMPTY_STRING = %22%22;%0A%0A    //@@author A0147990R%0A
     /**%0A
@@ -1210,32 +1210,76 @@
 RT_DATE,
- PREFIX_END_DATE
+%0A                        PREFIX_END_DATE, PREFIX_INFORMATION
 );%0A     
@@ -1564,16 +1564,106 @@
 _DATE));
+%0A        String informationValue = checkEmpty(argsTokenizer.getValue(PREFIX_INFORMATION));
 %0A%0A      
@@ -1724,16 +1724,34 @@
 imeValue
+, informationValue
 )) %7B%0A   
@@ -1928,24 +1928,32 @@
 ywords = get
+NoneNull
 Value(nameVa
@@ -1972,36 +1972,74 @@
 tetimeValue,
- endDatetime
+%0A                    endDatetimeValue, information
 Value);%0A    
@@ -2104,36 +2104,74 @@
 tetimeValue,
- endDatetime
+%0A                    endDatetimeValue, information
 Value);%0A%0A   
@@ -2558,32 +2558,57 @@
       %7D%0A%0A    %7D%0A%0A
+    //@@author A0147990R%0A
     private Stri
@@ -2758,18 +2758,28 @@
  return 
-%22%22
+EMPTY_STRING
 ;%0A      
@@ -2781,32 +2781,57 @@
        %7D%0A    %7D%0A%0A
+    //@@author A0147990R%0A
     private bool
@@ -2875,32 +2875,46 @@
 t2, String test3
+, String test4
 ) %7B%0A        Stri
@@ -2935,32 +2935,40 @@
  + test2 + test3
+ + test4
 ;%0A        return
@@ -3035,17 +3035,64 @@
 st3)
-);%0A    %7D%0A
+ && !sum.equals(test4));%0A    %7D%0A%0A    //@@author A0147990R
 %0A   
@@ -3110,16 +3110,24 @@
 ring get
+NoneNull
 Value(St
@@ -3156,32 +3156,46 @@
 t2, String test3
+, String test4
 ) %7B%0A        retu
@@ -3214,24 +3214,32 @@
 est2 + test3
+ + test4
 ;%0A    %7D%0A%0A   
@@ -3231,24 +3231,49 @@
 st4;%0A    %7D%0A%0A
+    //@@author A0147990R%0A
     private 
@@ -3335,16 +3335,30 @@
 ng test3
+, String test4
 ) %7B%0A    
@@ -3395,16 +3395,24 @@
  + test3
+ + test4
 ;%0A      
@@ -3576,32 +3576,55 @@
 ;%0A        %7D else
+ if (sum.equals(test3))
  %7B%0A            r
@@ -3649,32 +3649,99 @@
 ATE.toString();%0A
+        %7D else %7B%0A            return PREFIX_INFORMATION.toString();%0A
         %7D%0A    %7D%0A

add prefix_information in findCommandParser