package seedu.emeraldo.model.task;


import seedu.emeraldo.commons.exceptions.IllegalValueException;
import java.time.*;

/**
 * Represents a Task's date and time in Emeraldo.
 * Guarantees: immutable; is valid as declared in {@link #isValidDateTime(String)}
 */
public class DateTime {
    
    public static final String MESSAGE_DATETIME_CONSTRAINTS = "Task date and time must follow this format DD/MM/YYYY HH:MM in 24 hours format";
    //public static final String DATETIME_VALIDATION_REGEX = "(.+)?";
    public static final String DATETIME_VALIDATION_REGEX = "((0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|[1][0-2])/(([0-9][0-9])?[0-9][0-9]))"
                                                            + "( ([01][0-9]|[2][0-3]):([0-5][0-9]))?";

    public final LocalDateTime value;
    public final LocalDate valueDate;
    public final LocalTime valueTime;

    /**
     * Validates given date and time.
     *
     * @throws IllegalValueException if given address string is invalid.
     */
    public DateTime(String dateTime) throws IllegalValueException {
        assert dateTime != null;
        if (!isValidDateTime(dateTime)) {
            throw new IllegalValueException(MESSAGE_DATETIME_CONSTRAINTS);
        }
        
        String [] dateTimeArr = dateTime.split(" ");
        String [] dateArr = dateTimeArr[0].split("/");
        int year = Integer.parseInt(dateArr[2]);
        int month = Integer.parseInt(dateArr[1]);
        int day = Integer.parseInt(dateArr[0]);
        this.valueDate = LocalDate.of(year, month, day);
        
        String [] timeArr = dateTimeArr[1].split(":");
        int hour = Integer.parseInt(timeArr[0]);
        int minute = Integer.parseInt(timeArr[1]);
        this.valueTime = LocalTime.of(hour, minute);
        
        this.value = LocalDateTime.of(this.valueDate, this.valueTime);
        
    }

    private static boolean isValidDateTime(String test) {
        if(test.equals(""))
            return true;
        else
            return test.matches(DATETIME_VALIDATION_REGEX);
    }

    @Override
    public String toString() {
    	
    	return value.toString();
    }

    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof DateTime // instanceof handles nulls
                && this.value.equals(((DateTime) other).value)); // state check
    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }

}

@@ -433,78 +433,8 @@
 t%22;%0A
-    //public static final String DATETIME_VALIDATION_REGEX = %22(.+)?%22;%0A
     
@@ -666,16 +666,46 @@
 9%5D))?%22;%0A
+    public final String value;
 %0A    pub
@@ -729,24 +729,32 @@
 teTime value
+DateTime
 ;%0A    public
@@ -1183,32 +1183,371 @@
      %7D%0A        %0A
+        this.value = dateTime;%0A        %0A        if(dateTime.equals(%22%22))%7B            //Constructing an empty DateTime object %0A            this.valueDateTime = null;%0A            this.valueDate = null;%0A            this.valueTime = null;%0A        %7Delse if(!dateTime.contains(%22:%22))%7B           //Constructing a DateTime object with Date only%0A    
         String %5B
@@ -1583,32 +1583,36 @@
 t(%22 %22);%0A        
+ 
+   
 String %5B%5D dateAr
@@ -1646,24 +1646,28 @@
 %22);%0A        
+    
 int year = I
@@ -1699,24 +1699,28 @@
 %5D);%0A        
+ 
+   
 int month = 
@@ -1741,32 +1741,36 @@
 nt(dateArr%5B1%5D);%0A
+    
         int day 
@@ -1801,32 +1801,36 @@
 rr%5B0%5D);%0A        
+    
 this.valueDate =
@@ -1862,33 +1862,564 @@
 , day);%0A        
+    
 %0A
+            this.valueDateTime = null;%0A            this.valueTime = null;%0A        %7D else %7B                                    //Constructing a DateTime object with Date and time%0A            String %5B%5D dateTimeArr = dateTime.split(%22 %22);%0A            String %5B%5D dateArr = dateTimeArr%5B0%5D.split(%22/%22);%0A            int year = Integer.parseInt(dateArr%5B2%5D);%0A            int month = Integer.parseInt(dateArr%5B1%5D);%0A            int day = Integer.parseInt(dateArr%5B0%5D);%0A            this.valueDate = LocalDate.of(year, month, day);%0A        %0A    
         String %5B
@@ -2461,24 +2461,28 @@
 %22);%0A        
+   
+ 
 int hour = I
@@ -2502,32 +2502,36 @@
 nt(timeArr%5B0%5D);%0A
+    
         int minu
@@ -2565,32 +2565,36 @@
 rr%5B1%5D);%0A        
+   
+ 
 this.valueTime =
@@ -2631,32 +2631,36 @@
        %0A        
+    
 this.value = Loc
@@ -2653,16 +2653,24 @@
 is.value
+DateTime
  = Local
@@ -2718,24 +2718,25 @@
 e);%0A        
+%7D
 %0A    %7D%0A%0A    
@@ -2793,78 +2793,8 @@
 ) %7B%0A
-        if(test.equals(%22%22))%0A            return true;%0A        else%0A    
     
@@ -2905,15 +2905,12 @@
     
-%09%0A
     
-%09
 retu
@@ -2921,19 +2921,8 @@
 alue
-.toString()
 ;%0A  

Fix ArrayIndexOutOfBounds error in DateTime class