package seedu.ezdo.logic.parser;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;

public class DateParser {

    public String value;

    public static final String USER_DATE_INPUT_FORMAT = "dd/MM/yyyy HH:mm";
    public static final String USER_DATE_OUTPUT_FORMAT = "dd/MM/yyyy HH:mm";
    public static final String NATTY_DATE_FORMAT = "MM/dd/yyyy HH:mm";

    public final SimpleDateFormat userInputDateFormat = new SimpleDateFormat(USER_DATE_INPUT_FORMAT);
    public final SimpleDateFormat userOutputDateFormat = new SimpleDateFormat(USER_DATE_OUTPUT_FORMAT);
    public final SimpleDateFormat nattyDateFormat = new SimpleDateFormat(NATTY_DATE_FORMAT);

    public DateParser(String input) {
        try {
            // Format input string to suit Natty dependency
            String formattedInitialTaskDate = changeToNattyDateFormat(input);
            this.value = nattyManipulation(formattedInitialTaskDate);
        } catch (ParseException pe) {
            this.value = nattyManipulation(input);
        }
    }

    /**
     * Changes the initial date format of a string input for Natty.
     */
    private String changeToNattyDateFormat(String input) throws ParseException {

        Date userDateObject = userInputDateFormat.parse(input);
        String output = nattyDateFormat.format(userDateObject);

        return output;
    }

    /**
     * Uses Natty dependency (natural language date parser) to manipulate date
     * input in String.
     */
    private String nattyManipulation(String taskDate) {

        try {
            // Initialises Natty parser
            Parser parser = new Parser();

            // Parses input String into a list of DateGroups
            List<DateGroup> dateGroupList = parser.parse(taskDate);

            // Retrieves parsed date
            Date parsedDate = dateGroupList.get(0).getDates().get(0);

            // Format parsed date to suit the UI
            String formattedFinalTaskDate = userOutputDateFormat.format(parsedDate);

            return formattedFinalTaskDate;

        } catch (IndexOutOfBoundsException e) {
            return taskDate;
        }

    }

}

@@ -31,41 +31,8 @@
 r;%0A%0A
-import java.text.ParseException;%0A
 impo
@@ -107,16 +107,80 @@
 il.List;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -308,84 +308,8 @@
 e;%0A%0A
-    public static final String USER_DATE_INPUT_FORMAT = %22dd/MM/yyyy HH:mm%22;%0A
     
@@ -381,16 +381,17 @@
 HH:mm%22;%0A
+%0A
     publ
@@ -417,45 +417,78 @@
 ing 
-NATTY_DATE_FORMAT = %22MM/dd/yyyy HH:mm
+DAY_MONTH_YEAR_FORMAT = %22%5E(%5B0-9%5D%7B1,2%7D%5B-/%5D)(%5B0-9%5D%7B1,2%7D%5B-/%5D)(%5B0-9%5D%7B4%7D.*)
 %22;%0A%0A
@@ -521,26 +521,27 @@
 eFormat user
-In
+Out
 putDateForma
@@ -579,10 +579,11 @@
 ATE_
-IN
+OUT
 PUT_
@@ -583,32 +583,34 @@
 OUTPUT_FORMAT);%0A
+%0A%0A
     public final
@@ -608,376 +608,196 @@
 lic 
-final SimpleDateFormat userOutputDateFormat = new SimpleDateFormat(USER_DATE_OUTPUT_FORMAT);%0A    public final SimpleDateFormat nattyDateFormat = new SimpleDateFormat(NATTY_DATE_FORMAT);%0A%0A    public DateParser(String input) %7B%0A        try %7B%0A            // Format input string to suit Natty dependency%0A            String formattedInitialTaskDate = changeToNattyDateFormat
+DateParser(String input) %7B%0A        // swap day and month as natty only accepts the MM/DD/YYYY format%0A        if (input.matches(DAY_MONTH_YEAR_FORMAT)) %7B%0A            input = swapDayAndMonth
 (inp
@@ -801,24 +801,30 @@
 input);%0A    
+    %7D%0A
         this
@@ -854,147 +854,118 @@
 ion(
-formattedInitialTaskDate);%0A        %7D catch (ParseException pe) %7B%0A            this.value = nattyManipulation(input);%0A        %7D%0A    %7D%0A
+input);%0A    %7D%0A%0A    /**%0A     * Swaps the day and month of a date in DD/MM/YYYY format for natty to parse.
 %0A    
-/*
+ 
 *%0A  
@@ -973,158 +973,286 @@
   * 
-Changes the initial date format of a string input for Natty.%0A     */%0A    private String changeToNattyDateFormat(String input) throws ParseException %7B%0A
+@param input A string representing a date in the DD/MM/YYYY format.%0A     * @return A string equal to the input date but in MM/DD/YYYY format.%0A     */%0A    private String swapDayAndMonth(String input) %7B%0A        Pattern dayMonthYearPattern = Pattern.compile(DAY_MONTH_YEAR_FORMAT);
 %0A   
@@ -1260,55 +1260,53 @@
     
-Date userDateObject = userInputDateFormat.parse
+Matcher matcher = dayMonthYearPattern.matcher
 (inp
@@ -1310,24 +1310,25 @@
 input);%0A
+%0A
         
 String o
@@ -1323,65 +1323,99 @@
     
-String output = nattyDateFormat.format(userDateObject
+matcher.matches();%0A        input = matcher.group(2) + matcher.group(1) + matcher.group(3
 );%0A
-%0A
     
@@ -1429,11 +1429,10 @@
 urn 
-out
+in
 put;
@@ -1614,17 +1614,16 @@
 Date) %7B%0A
-%0A
         
@@ -2211,22 +2211,21 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A%7D%0A

Fix handling of dates in DD/MM/YYYY format