package seedu.task.model.task;

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

import seedu.task.commons.exceptions.IllegalValueException;

/**
 * Represents a Task's date in the address book. Guarantees: immutable; is valid
 * as declared in {@link #isValidTiming(String)}
 */
public class Timing implements Comparable<Timing> {

    public static final String MESSAGE_TIMING_CONSTRAINTS = "Task date should be in the format HH:mm dd/MM/yyyy OR dd/MM/yyyy";
    public static final String[] TIMING_FORMAT = {
            // "dd/MM/yyyy HH:mm",
            "HH:mm dd/MM/yyyy", "dd/MM/yyyy" };
    public static final String NULL_TIMING = "n/a";
    public final String value;
    private Date date;

    /**
     * Validates given date.
     *
     * @throws IllegalValueException
     *             if given date string is invalid.
     */
    public Timing(String timing) throws IllegalValueException {
        if (timing != null) {
            String trimmedTiming = timing.trim();
            if (!isValidTiming(trimmedTiming)) {
                throw new IllegalValueException(MESSAGE_TIMING_CONSTRAINTS);
            }
            this.value = trimmedTiming;
        } else {
            this.value = NULL_TIMING;
        }
    }

    /**
     * Returns if a given string is a valid date.
     */
    private boolean isValidTiming(String test) {
        boolean isValid = false;
        if (test.equals(NULL_TIMING)) {
            isValid = true;
        } else {
            for (int i = 0; i < TIMING_FORMAT.length; i++) {
                SimpleDateFormat sdf = new SimpleDateFormat(TIMING_FORMAT[i]);
                sdf.setLenient(false);
                try {
                    // throws ParseException if date is not valid
                    Date date = sdf.parse(test);
                    isValid = true;
                    this.date = date;
                    break;
                } catch (ParseException e) {
                }
            }
        }
        return isValid;
    }

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

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

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

    /**
     * Results in Timing sorted in ascending order.
     */
    @SuppressWarnings("deprecation")
    @Override
    public int compareTo(Timing compareTiming) {
        if (this.date == null || compareTiming.date == null) {
            System.out.println(this.date + "NULL!!!" + compareTiming.date);

        }
        boolean thisNull = this.date.equals(NULL_TIMING);
        boolean otherNull = compareTiming.date.equals(NULL_TIMING);

        if (thisNull && otherNull) {
            return 0;
        } else if (thisNull) {
            return 1;
        } else if (otherNull) {
            return -1;
        }

        int compareToResult = this.date.getYear() - compareTiming.date.getYear() + 3800;

        if (compareToResult == 0) {
            compareToResult = this.date.getMonth() - compareTiming.date.getMonth();
        }

        if (compareToResult == 0) {
            compareToResult = this.date.getDay() - compareTiming.date.getDay();
        }
        return compareToResult;
    }

}

@@ -2805,139 +2805,269 @@
     
-if (this.date == null %7C%7C compareTiming.date == null) %7B%0A            System.out.println(this.date + %22NULL!!!%22 + compareTiming.date);%0A
+boolean thisNull = this.date == null;%0A        boolean otherNull = compareTiming.date == null;%0A%0A        if (thisNull && otherNull) %7B%0A            return 0;%0A        %7D else if (thisNull) %7B%0A            return 1;%0A        %7D else if (otherNull) %7B%0A            return -1;
 %0A   
@@ -3065,32 +3065,33 @@
 n -1;%0A        %7D%0A
+%0A
         boolean 
@@ -3090,32 +3090,38 @@
 boolean thisNull
+Timing
  = this.date.equ
@@ -3155,32 +3155,38 @@
 oolean otherNull
+Timing
  = compareTiming
@@ -3225,32 +3225,38 @@
     if (thisNull
+Timing
  && otherNull) %7B
@@ -3244,32 +3244,38 @@
 ing && otherNull
+Timing
 ) %7B%0A            
@@ -3303,32 +3303,38 @@
 lse if (thisNull
+Timing
 ) %7B%0A            
@@ -3363,32 +3363,38 @@
 se if (otherNull
+Timing
 ) %7B%0A            

add null checks to fix comparable and view tasks in sorted order