package pt.up.fe.aiad.utils;

import javafx.application.Platform;

import java.util.Calendar;

/**
 * Represent an interval between two dates
 */
public class TimeInterval {
    private long _startDate;
    private long _endDate;

    /**
     * Constructor
     * @param startDate Starting date of the interval in seconds since Unix Epoch
     * @param endDate Ending date of the interval in seconds since Unix Epoch
     */
    public TimeInterval(long startDate, long endDate) {
        if (startDate <= endDate) throw new IllegalArgumentException();
        _startDate = startDate;
        _endDate = endDate;
    }

    /**
     * Constructor
     * @param startDate Starting date of the interval
     * @param endDate Ending date of the interval
     */
    public TimeInterval(Calendar startDate, Calendar endDate) {
        if (startDate.getTimeInMillis() > endDate.getTimeInMillis())
            throw new IllegalArgumentException();
        _startDate = startDate.getTimeInMillis() / 1000;
        _endDate = endDate.getTimeInMillis() / 1000;

    }

    public TimeInterval(String string) {
        String[] parts = string.split(",");

        if (parts.length != 2)
            throw new IllegalArgumentException();

        _startDate = Long.parseLong(parts[0]);
        _endDate = Long.parseLong(parts[1]);
    }

    /**
     * @return Starting date of the interval in seconds since Unix Epoch
     */
    public long getStartDate() {
        return _startDate;
    }

    /**
     * @return Ending date of the interval in seconds since Unix Epoch
     */
    public long getEndDate() {
        return _endDate;
    }

    /**
     * @return Duration of the interval in seconds since Unix Epoch
     */
    public long getDuration() {
        return _endDate - _startDate;
    }

    /**
     * @param duration Duration of the event in seconds since Unix Epoch
     * @return True if the duration of the event
     */
    public boolean fits(long duration) {
        return getDuration() >= duration;
    }

    /**
     * @return String representation of the interval
     */
    @Override
    public String toString() {
        return toString(false);
    }

    public String toString(boolean simple) {
        Calendar sd = Calendar.getInstance();
        Calendar ed = Calendar.getInstance();
        sd.setTimeInMillis(_startDate*1000);
        ed.setTimeInMillis(_endDate*1000);
        if (simple) {
            return _startDate + "," + _endDate;
        } else {
            return "[" + (sd.getTime().toString()) + ", " + (sd.getTime().toString()) + "]";
        }
    }
}

@@ -164,16 +164,52 @@
 Interval
+ implements Comparable%3CTimeInterval%3E
  %7B%0A    p
@@ -2044,32 +2044,262 @@
 uration;%0A    %7D%0A%0A
+%0A    public boolean overlaps(TimeInterval other) %7B%0A        if (other.getStartDate() %3E= getStartDate() && other.getStartDate() %3C getEndDate())%0A            return true;%0A        return (other.getEndDate() %3E getStartDate());%0A    %7D%0A%0A%0A%0A
     /**%0A     * @
@@ -2855,11 +2855,212 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public int compareTo(TimeInterval o) %7B //only compares start date%0A%0A        Long sd = _startDate;%0A        Long otherSd = o.getStartDate();%0A        return sd.compareTo(otherSd);%0A    %7D
 %0A%7D%0A

Add compareTo and overlap methods to time interval