package tars.logic.parser;

import static tars.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

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

import tars.commons.util.StringUtil;
import tars.logic.commands.Command;
import tars.logic.commands.EditCommand;
import tars.logic.commands.IncorrectCommand;

/**
 * Edit command parser
 * 
 * @@author A0121533W
 *
 */
public class EditCommandParser extends CommandParser {

    private static final Pattern TASK_INDEX_ARGS_FORMAT = Pattern.compile("(?<targetIndex>.+)");

    /**
     * Parses arguments in the context of the edit task command.
     * 
     * @param args full command args string
     * @return the prepared command
     */
    @Override
    public Command prepareCommand(String args) {
        args = args.trim();
        int targetIndex = 0;
        if (args.indexOf(EMPTY_SPACE_ONE) != -1) {
            targetIndex = args.indexOf(EMPTY_SPACE_ONE);
        }

        Optional<Integer> index = parseIndex(args.substring(0, targetIndex));

        if (!index.isPresent()) {
            return new IncorrectCommand(
                    String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
        }

        ArgumentTokenizer argsTokenizer = new ArgumentTokenizer(namePrefix, priorityPrefix,
                dateTimePrefix, addTagPrefix, removeTagPrefix);
        argsTokenizer.tokenize(args);

        if (argsTokenizer.numPrefixFound() == 0) {
            return new IncorrectCommand(
                    String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
        }

        return new EditCommand(index.get(), argsTokenizer);
    }

    /**
     * Returns the specified index in the {@code command} IF a positive unsigned integer is given as
     * the index. Returns an {@code Optional.empty()} otherwise.
     */
    private Optional<Integer> parseIndex(String command) {
        final Matcher matcher = TASK_INDEX_ARGS_FORMAT.matcher(command.trim());
        if (!matcher.matches()) {
            return Optional.empty();
        }

        String index = matcher.group("targetIndex");
        if (!StringUtil.isUnsignedInteger(index)) {
            return Optional.empty();
        }
        return Optional.of(Integer.parseInt(index));

    }

}

@@ -99,100 +99,8 @@
 T;%0A%0A
-import java.util.Optional;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 impo
@@ -374,106 +374,8 @@
  %7B%0A%0A
-    private static final Pattern TASK_INDEX_ARGS_FORMAT = Pattern.compile(%22(?%3CtargetIndex%3E.+)%22);%0A%0A
     
@@ -790,44 +790,79 @@
     
-Optional%3CInteger%3E index = parseIndex
+String index;%0A        try %7B%0A            index = StringUtil.indexString(
 (arg
@@ -885,26 +885,26 @@
 argetIndex))
+)
 ;
-%0A
 %0A        if 
@@ -904,30 +904,28 @@
     
-if (!index.isPresent()
+%7D catch (Exception e
 ) %7B%0A
@@ -956,32 +956,46 @@
 ncorrectCommand(
+String.format(
 %0A               
@@ -995,38 +995,202 @@
             
-String.format(
+MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));%0A        %7D%0A%0A        if (index.isEmpty()) %7B%0A            return new IncorrectCommand(String.format(%0A                    
 MESSAGE_INVA
@@ -1328,24 +1328,8 @@
 fix,
- priorityPrefix,
 %0A   
@@ -1340,16 +1340,32 @@
         
+ priorityPrefix,
  dateTim
@@ -1535,16 +1535,30 @@
 Command(
+String.format(
 %0A       
@@ -1570,30 +1570,16 @@
         
-String.format(
 MESSAGE_
@@ -1677,626 +1677,8 @@
 and(
-index.get(), argsTokenizer);%0A    %7D%0A%0A    /**%0A     * Returns the specified index in the %7B@code command%7D IF a positive unsigned integer is given as%0A     * the index. Returns an %7B@code Optional.empty()%7D otherwise.%0A     */%0A    private Optional%3CInteger%3E parseIndex(String command) %7B%0A        final Matcher matcher = TASK_INDEX_ARGS_FORMAT.matcher(command.trim());%0A        if (!matcher.matches()) %7B%0A            return Optional.empty();%0A        %7D%0A%0A        String index = matcher.group(%22targetIndex%22);%0A        if (!StringUtil.isUnsignedInteger(index)) %7B%0A            return Optional.empty();%0A        %7D%0A        return Optional.of(
 Inte
@@ -1696,20 +1696,34 @@
 t(index)
+, argsTokenizer
 );%0A
-%0A
     %7D%0A%0A%7D

Remove parseIndex method