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);
        }
    }
    
}

@@ -252,25 +252,25 @@
  Task's date
-t
+T
 ime in tars.
@@ -564,24 +564,318 @@
 tring;%0A    %0A
+    private static final DateTimeFormatter formatter = DateTimeFormatter%0A            .ofPattern(%22d/M/uuuu HHmm%22)%0A            .withResolverStyle(ResolverStyle.STRICT);%0A    private static final DateTimeFormatter stringFormatter = DateTimeFormatter%0A            .ofPattern(%22dd/MM/uuuu HHmm%22);%0A    %0A
     /**%0A    
@@ -1255,275 +1255,8 @@
 n %7B%0A
-        DateTimeFormatter formatter = DateTimeFormatter%0A                .ofPattern(%22d/M/uuuu HHmm%22)%0A                .withResolverStyle(ResolverStyle.STRICT);%0A        DateTimeFormatter stringFormatter = DateTimeFormatter%0A                .ofPattern(%22dd/MM/uuuu HHmm%22);%0A
     
@@ -1766,24 +1766,24 @@
           %7D%0A
+
         %7D%0A  
@@ -1791,198 +1791,8 @@
  %7D%0A%0A
-    public void setStartDate(LocalDateTime startDate) %7B%0A        this.startDate = startDate;%0A    %7D%0A%0A    public void setEndDate(LocalDateTime endDate) %7B%0A        this.endDate = endDate;%0A    %7D%0A%0A
     

Move formatter to be class attribute