package seedu.taskboss.logic.parser;

import java.util.List;
import java.util.Locale;
import java.util.Optional;

import com.joestelmach.natty.DateGroup;

import seedu.taskboss.commons.exceptions.IllegalValueException;
import seedu.taskboss.model.task.DateTime;

/*
 * Parses date
 */
public class DateParser {

    private static final String START_DATE = "START DATE: ";
    private static final String END_DATE = "END DATE: ";
    private static final String ERROR_INVALID_DATE = "Failed to understand given date.";
    private static final String ERROR_MULTIPLE_DATES = "Please only enter a single date.";

    private com.joestelmach.natty.Parser nattyParser;

    public DateParser() {
        this.nattyParser = new com.joestelmach.natty.Parser();
    }

    public DateTime parseStartDate(String rawStartDate) throws IllegalValueException {

        if (Locale.getDefault().equals(Locale.US)) {
            rawStartDate = rawStartDate.replaceAll("(\\d{1,2})-(\\d{1,2})-((?:\\d\\d){1,2})", "$2-$1-$3");
        }

        List<DateGroup> dateGroupList = this.nattyParser.parse(rawStartDate);
        int numDates = countDates(dateGroupList);

        if (numDates == 0) {
            throw new IllegalValueException(START_DATE + ERROR_INVALID_DATE);
        }

        if (numDates > 1) {
            throw new IllegalValueException(START_DATE + ERROR_MULTIPLE_DATES);
        }

        DateGroup dateGroup = dateGroupList.get(0);

        return new DateTime(dateGroup.getDates().get(0), dateGroup.isDateInferred(), dateGroup.isTimeInferred());

    }

    public DateTime parseEndDate(String rawEndDate) throws IllegalValueException {

        if (Locale.getDefault().equals(Locale.US)) {
            rawEndDate = rawEndDate.replaceAll("(\\d{1,2})-(\\d{1,2})-((?:\\d\\d){1,2})", "$2-$1-$3");
        }

        List<DateGroup> dateGroupList = this.nattyParser.parse(rawEndDate);
        int numDates = countDates(dateGroupList);

        if (numDates == 0) {
            throw new IllegalValueException(END_DATE + ERROR_INVALID_DATE);
        }

        if (numDates > 1) {
            throw new IllegalValueException(END_DATE + ERROR_MULTIPLE_DATES);
        }

        DateGroup dateGroup = dateGroupList.get(0);

        return new DateTime(dateGroup.getDates().get(0), dateGroup.isDateInferred(), dateGroup.isTimeInferred());

    }

    public Optional<String> convert(Optional<DateTime> dateTime) {
        return dateTime.isPresent() ? Optional.of(dateTime.toString()) : Optional.empty();
    }

    private int countDates(List<DateGroup> dateGroups) {
        int numTotalDates = 0;
        for (DateGroup dateGroup : dateGroups) {
            numTotalDates += dateGroup.getDates().size();
        }
        return numTotalDates;
    }

    public static String getStartDateInvalidDateError() {
        return START_DATE + ERROR_INVALID_DATE;
    }

    public static String getStartDateMultipleDatesError() {
        return START_DATE + ERROR_MULTIPLE_DATES;
    }

    public static String getEndDateInvalidDateError() {
        return END_DATE + ERROR_INVALID_DATE;
    }

    public static String getEndDateMultipleDatesError() {
        return END_DATE + ERROR_MULTIPLE_DATES;
    }
}

@@ -835,32 +835,80 @@
 lueException %7B%0A%0A
+        String currentStartDate = rawStartDate;%0A
         if (Loca
@@ -956,19 +956,23 @@
         
-raw
+current
 StartDat
@@ -1129,19 +1129,23 @@
 r.parse(
-raw
+current
 StartDat
@@ -1687,32 +1687,76 @@
 lueException %7B%0A%0A
+        String currentEndDate = rawEndDate;%0A
         if (Loca
@@ -1804,19 +1804,23 @@
         
-raw
+current
 EndDate 
@@ -1973,19 +1973,23 @@
 r.parse(
-raw
+current
 EndDate)

Fix codacy issue::DateParser