package tars.model.task;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

import tars.commons.exceptions.IllegalValueException;

/**
 * Represents a Task's datetime in tars.
 */
public class DateTime {
    public static final String MESSAGE_DATETIME_CONSTRAINTS = "Task datetime should be spaces or alphanumeric characters";

    private LocalDateTime startDate;
    private LocalDateTime endDate;

    public String startDateString;
    public String endDateString;
    
    /**
     * Default constructor
     */
    public DateTime() {}

    /**
     * Validates given task dateTime.
     *
     * @throws DateTimeException if given dateTime string is invalid.
     * @throws IllegalDateException end date occurring before start date.
     */
    public DateTime(String startDate, String endDate)
            throws DateTimeException, IllegalDateException {
        DateTimeFormatter formatter = DateTimeFormatter
                .ofPattern("d/M/uuuu HHmm")
                .withResolverStyle(ResolverStyle.STRICT);
        DateTimeFormatter stringFormatter = DateTimeFormatter
                .ofPattern("dd/MM/uuuu HHmm");
        this.endDate = LocalDateTime.parse(endDate, formatter);
        this.endDateString = this.endDate.format(stringFormatter);

        if (startDate != null) {
            this.startDate = LocalDateTime.parse(startDate, formatter);
            this.startDateString = this.startDate.format(stringFormatter);
            if (this.endDate.isBefore(this.startDate) || this.endDate.isEqual(this.startDate)) {
                throw new IllegalDateException("End dateTime should be after start dateTime.");
            }
        }
    }

    public void setStartDate(LocalDateTime startDate) {
        this.startDate = startDate;
    }

    public void setEndDate(LocalDateTime endDate) {
        this.endDate = endDate;
    }

    @Override
    public String toString() {
        if (this.startDate == null) {
            return endDateString;
        } else {
            return startDateString + " to " + endDateString;
        }
    }
    
    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof DateTime // instanceof handles nulls
                && this.toString().equals(((DateTime) other).toString())); // state check
    }

    /**
     * Signals an error caused by end date occurring before start date
     */
    public class IllegalDateException extends IllegalValueException {
        public IllegalDateException(String message) {
            super(message);
        }
    }
    
}

@@ -1012,33 +1012,16 @@
 ormatter
-%0A                
 .ofPatte
@@ -1159,33 +1159,16 @@
 ormatter
-%0A                
 .ofPatte
@@ -1186,24 +1186,84 @@
 uuu HHmm%22);%0A
+%0A        if (endDate != null && endDate.length() %3E 0) %7B%0A    
         this
@@ -1306,32 +1306,36 @@
 te, formatter);%0A
+    
         this.end
@@ -1384,16 +1384,26 @@
 matter);
+%0A        %7D
 %0A%0A      
@@ -1421,24 +1421,50 @@
 Date != null
+ && startDate.length() %3E 0
 ) %7B%0A        
@@ -1815,24 +1815,25 @@
 %7D%0A        %7D%0A
+%0A
     %7D%0A%0A    p
@@ -2089,17 +2089,41 @@
 artDate 
-=
+!= null && this.endDate !
 = null) 
@@ -2142,16 +2142,43 @@
   return
+ startDateString + %22 to %22 +
  endDate
@@ -2199,16 +2199,68 @@
   %7D else
+ if (this.startDate == null && this.endDate != null)
  %7B%0A     
@@ -2265,37 +2265,35 @@
          return 
-start
+end
 DateString + %22 t
@@ -2290,33 +2290,48 @@
 ring
- + %22 to %22 + endDateString
+;%0A        %7D else %7B%0A            return %22%22
 ;%0A  

Update DateTime