package seedu.tasklist.logic.parser;

import static seedu.tasklist.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.tasklist.logic.parser.CliSyntax.KEYWORDS_ARGS_FORMAT;
import static seedu.tasklist.logic.parser.CliSyntax.PREFIX_STATUS;
import static seedu.tasklist.logic.parser.CliSyntax.PREFIX_TAG;

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

import seedu.tasklist.logic.commands.ClearCommand;
import seedu.tasklist.logic.commands.Command;
import seedu.tasklist.logic.commands.FindCommand;
import seedu.tasklist.logic.commands.IncorrectCommand;

/**
 * Parses input arguments and creates ClearCommand object.
 *
 */
public class ClearCommandParser {
    private boolean isByTags = false;
    private boolean isByStatus = false;

    public Command parse(String args) {
        args = ParserUtil.parseFlexiblePrefix(args);
        ArgumentTokenizer argsTokenizer = new ArgumentTokenizer(PREFIX_TAG, PREFIX_STATUS);
        argsTokenizer.tokenize(args);
        Optional<String> keyword;
        keyword = argsTokenizer.getValue(PREFIX_TAG);
        isByTags = true;
        if (!keyword.isPresent()) {
            keyword = argsTokenizer.getValue(PREFIX_STATUS);
            isByStatus = true;
            isByTags = false;
        }
        if (!keyword.isPresent()) {
            keyword = Optional.of(args.trim());
            isByStatus = false;
            isByTags = false;
        }
        assert keyword.isPresent();
        Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(keyword.get().trim());
        if (!matcher.matches()) {
            return new IncorrectCommand(
                    String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
        }

        if (isByTags) {
            return new ClearCommand(keyword.get()).isByTags();
        } else if (isByStatus) {
            if (!keyword.get().equals("completed") || !keyword.get().equals("uncompleted")) {
                return new IncorrectCommand(
                        String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
            } else {
                return new ClearCommand(keyword.get()).isByStatus();
            }
        } else {    //clear all
            return new ClearCommand();
        }




        //return new ClearCommand(parameter, arg) overload, if nothing then ClearCommand().

    }
}

@@ -769,48 +769,168 @@
 se;%0A
-%0A    public Command parse(String args) %7B
+    private boolean isOnlyClear = false;%0A%0A    public Command parse(String args) %7B%0A        if (args.trim().isEmpty()) %7B%0A            isOnlyClear = true;%0A        %7D
 %0A   
@@ -1581,16 +1581,93 @@
 esent();
+%0A%0A        if (isOnlyClear) %7B%0A            return new ClearCommand();%0A        %7D
 %0A       
@@ -1811,37 +1811,16 @@
 Command(
-%0A                    
 String.f
@@ -1849,36 +1849,37 @@
 COMMAND_FORMAT, 
-Find
+Clear
 Command.MESSAGE_
@@ -2160,33 +2160,8 @@
 and(
-%0A                        
 Stri
@@ -2348,82 +2348,8 @@
    %7D
- else %7B    //clear all%0A            return new ClearCommand();%0A        %7D%0A%0A%0A
 %0A%0A  
@@ -2358,10 +2358,8 @@
     
-//
 retu
@@ -2382,65 +2382,10 @@
 and(
-parameter, arg) overload, if nothing then ClearCommand().
+);
 %0A%0A  

Fix bug that disallow the simple "clear" command to be parsed correctly