package seedu.task.logic.parser;

import static seedu.task.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.task.logic.parser.CliSyntax.PREFIX_DUEDATE;
import static seedu.task.logic.parser.CliSyntax.PREFIX_END;
import static seedu.task.logic.parser.CliSyntax.PREFIX_START;
import static seedu.task.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.logic.commands.Command;
import seedu.task.logic.commands.EditCommand;
import seedu.task.logic.commands.EditCommand.EditTaskDescriptor;
import seedu.task.logic.commands.IncorrectCommand;
import seedu.task.model.tag.UniqueTagList;

/**
 * Parses input arguments and creates a new EditCommand object
 */
public class EditCommandParser {

    //@@author A0163673Y
    /**
     * Parses the given {@code String} of arguments in the context of the EditCommand
     * and returns an EditCommand object for execution.
     */
    public Command parse(String args) {
        assert args != null;
        ArgumentTokenizer argsTokenizer =
                new ArgumentTokenizer(PREFIX_TAG, PREFIX_START, PREFIX_END, PREFIX_DUEDATE);
        argsTokenizer.tokenize(args);
        List<Optional<String>> preambleFields = ParserUtil.splitPreamble(argsTokenizer.getPreamble().orElse(""), 2);

        Optional<Integer> index = preambleFields.get(0).flatMap(ParserUtil::parseIndex);
        if (!index.isPresent()) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
        }

        EditTaskDescriptor editTaskDescriptor = new EditTaskDescriptor();
        try {
            editTaskDescriptor.setDescription(ParserUtil.parseDescription(preambleFields.get(1)));
            editTaskDescriptor.setTags(parseTagsForEdit(ParserUtil.toSet(argsTokenizer.getAllValues(PREFIX_TAG))));
            // handle parsing of due date
            if (argsTokenizer.getValue(PREFIX_DUEDATE).isPresent()
                    && argsTokenizer.getValue(PREFIX_DUEDATE).get().equals("")) {
                editTaskDescriptor.setDeleteDueDate(true);
            } else {
                editTaskDescriptor.setDueDate(ParserUtil.parseDueDate(argsTokenizer.getValue(PREFIX_DUEDATE)));
            }
            // handle parsing of duration
            if (argsTokenizer.getValue(PREFIX_START).isPresent()
                    && argsTokenizer.getValue(PREFIX_END).isPresent()
                    && argsTokenizer.getValue(PREFIX_START).get().equals("")
                    && argsTokenizer.getValue(PREFIX_END).get().equals("")) {
                editTaskDescriptor.setDeleteDuration(true);
            } else {
                editTaskDescriptor.setDurationStart(ParserUtil.parseString(argsTokenizer.getValue(PREFIX_START)));
                editTaskDescriptor.setDurationEnd(ParserUtil.parseString(argsTokenizer.getValue(PREFIX_END)));
            }
        } catch (IllegalValueException ive) {
            return new IncorrectCommand(ive.getMessage());
        }

        if (!editTaskDescriptor.isAnyFieldEdited()) {
            return new IncorrectCommand(EditCommand.MESSAGE_NOT_EDITED);
        }

        return new EditCommand(index.get(), editTaskDescriptor);
    }
    //@@author

    /**
     * Parses {@code Collection<String> tags} into an {@code Optional<UniqueTagList>} if {@code tags} is non-empty.
     * If {@code tags} contain only one element which is an empty string, it will be parsed into a
     * {@code Optional<UniqueTagList>} containing zero tags.
     */
    private Optional<UniqueTagList> parseTagsForEdit(Collection<String> tags) throws IllegalValueException {
        assert tags != null;

        if (tags.isEmpty()) {
            return Optional.empty();
        }
        Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
        return Optional.of(ParserUtil.parseTags(tagSet));
    }

}

@@ -901,24 +901,75 @@
 163673Y%0A
+    public static final String EMPTY_STRING = %22%22;%0A%0A
     /**%0A
      * P
@@ -952,32 +952,32 @@
  = %22%22;%0A%0A    /**%0A
-
      * Parses th
@@ -1425,16 +1425,33 @@
 reamble(
+%0A                
 argsToke
@@ -1477,18 +1477,28 @@
 .orElse(
-%22%22
+EMPTY_STRING
 ), 2);%0A%0A
@@ -2225,34 +2225,44 @@
 E).get().equals(
-%22%22
+EMPTY_STRING
 )) %7B%0A           
@@ -2706,18 +2706,28 @@
 .equals(
-%22%22
+EMPTY_STRING
 )%0A      
@@ -2791,18 +2791,28 @@
 .equals(
-%22%22
+EMPTY_STRING
 )) %7B%0A   
@@ -4048,18 +4048,28 @@
 ontains(
-%22%22
+EMPTY_STRING
 ) ? Coll

use variable instead of hard coded string