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_INFORMATION;
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.regex.Matcher;

import seedu.taskboss.logic.commands.Command;
import seedu.taskboss.logic.commands.FindCommand;
import seedu.taskboss.logic.commands.IncorrectCommand;
import seedu.taskboss.logic.parser.ArgumentTokenizer.Prefix;

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

    private static final String EMPTY_STRING = "";
    private static final String prefixName = PREFIX_NAME.getPrefix();
    private static final String prefixInformation = PREFIX_INFORMATION.getPrefix();
    private static final String prefixStartDate = PREFIX_START_DATE.getPrefix();
    private static final String prefixEndDate = PREFIX_END_DATE.getPrefix();

    //@@author A0147990R
    /**
     * 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) {

        String inputprefix = parsePrefix(args);
        String pre;

        if (inputprefix.equals(EMPTY_STRING)) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
                    FindCommand.MESSAGE_USAGE));
        }

        Prefix inputPrefix;
        if (inputprefix.equals(prefixName)) {
            inputPrefix = PREFIX_NAME;
            pre = PREFIX_NAME.toString();
        } else if (inputprefix.equals(prefixInformation)) {
            inputPrefix = PREFIX_INFORMATION;
            pre = PREFIX_INFORMATION.toString();
        } else if (inputprefix.equals(prefixStartDate)) {
            inputPrefix = PREFIX_START_DATE;
            pre = PREFIX_START_DATE.toString();
        } else if (inputprefix.equals(prefixEndDate)) {
            inputPrefix = PREFIX_END_DATE;
            pre = PREFIX_END_DATE.toString();
        } else {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
                    FindCommand.MESSAGE_USAGE));
        }

        try {
            ArgumentTokenizer argsTokenizer = new ArgumentTokenizer(inputPrefix);
            argsTokenizer.tokenize(args);
            String keywords = argsTokenizer.getValue(inputPrefix).get();

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

        } catch (NoSuchElementException nsee) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
                    FindCommand.MESSAGE_USAGE));
        }

    }

  //@@author A0147990R
    private String parsePrefix(String args) {
        int prefixIndex = args.indexOf("/");
        if (prefixIndex == -1) {
            return EMPTY_STRING;
        } else {
            return args.substring(1, prefixIndex + 1);
        }
    }

}

@@ -320,84 +320,15 @@
 FIX_
-INFORMATION;%0Aimport static seedu.taskboss.logic.parser.CliSyntax.PREFIX_NAME
+KEYWORD
 ;%0Aim
@@ -844,162 +844,8 @@
 %22%22;%0A
-    private static final String prefixName = PREFIX_NAME.getPrefix();%0A    private static final String prefixInformation = PREFIX_INFORMATION.getPrefix();%0A
     
@@ -997,16 +997,92 @@
 refix();
+%0A    private static final String prefixKeyword = PREFIX_KEYWORD.getPrefix();
 %0A%0A    //
@@ -1631,153 +1631,15 @@
 efix
-Name)) %7B%0A            inputPrefix = PREFIX_NAME;%0A            pre = PREFIX_NAME.toString();%0A        %7D else if (inputprefix.equals(prefixInformation
+Keyword
 )) %7B
@@ -1672,27 +1672,23 @@
  PREFIX_
-INFORMATION
+KEYWORD
 ;%0A      
@@ -1710,19 +1710,15 @@
 FIX_
-INFORMATION
+KEYWORD
 .toS

update findCommandParser to support find by keyword, remove name and information