package seedu.taskman.model.event;

import org.ocpsoft.prettytime.PrettyTime;
import com.google.common.base.Objects;

import seedu.taskman.commons.exceptions.IllegalValueException;
import seedu.taskman.logic.parser.DateTimeParser;

import java.time.Instant;

public class Frequency {
    // TODO: check for overflow because current validation allows years?
    public static final String MESSAGE_FREQUENCY_CONSTRAINTS =
            "Task frequency should only contain frequency and unit of time in the format: ";
    // differed: swap to multiple duration format
    public static final String FREQUENCY_VALIDATION_REGEX = "^" + DateTimeParser.SINGLE_DURATION + "$";

    public final Long seconds;
    public final PrettyTime p = new PrettyTime();

    public Frequency(String frequency) throws IllegalValueException {
        assert frequency != null;
        frequency = frequency.trim();
        if (!isValidFrequency(frequency)) {
            throw new IllegalValueException(MESSAGE_FREQUENCY_CONSTRAINTS);
        }
        this.seconds = DateTimeParser.durationToUnixTime(Instant.now().getEpochSecond(), frequency);
    }

    public Frequency(long seconds) {
        assert seconds >= 0;
        this.seconds = seconds;
    }

    public static boolean isValidFrequency(String test) {
        return test.matches(FREQUENCY_VALIDATION_REGEX);
    }

    @Override
    public String toString() {
    	// TODO: the long solution shows 17, 000 days even though frequency is 1 year, maybe didn't subtract somewhere
    	/*
        String[] temporalUnits = {"year", "month", "day", "hour", "min"};
        String duration = DurationFormatUtils.formatDuration(seconds * 1000, "y:M:d:H:m");
        String[] split = duration.split(":");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String temporalUnit = temporalUnits[i];
            long time = Long.valueOf(split[i]);
            if (time >= 1) {
                builder.append(time + " " + temporalUnit);

                // for plurality
                if (time > 1) {
                    builder.append("s");
                }

                builder.append(", ");
            }
        }

        String parsedDuration = builder.toString();
        // drop the last ", "
        if (!parsedDuration.isEmpty()) {
            parsedDuration = parsedDuration.substring(0, parsedDuration.length() - 2);
        }
        return parsedDuration.isEmpty()
                ? "0 mins"
                : parsedDuration;
        */
    	// TODO: the solution below does not give exactly 1 year, and shows the next time the schedule begins
        return DateTimeParser.epochSecondToShortDateTime(seconds);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Frequency frequency = (Frequency) o;
        return Objects.equal(seconds, frequency.seconds);
    }

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

@@ -225,16 +225,38 @@
 arser;%0A%0A
+import java.sql.Date;%0A
 import j
@@ -303,81 +303,8 @@
 y %7B%0A
-    // TODO: check for overflow because current validation allows years?%0A
     
@@ -455,16 +455,58 @@
 mat: %22;%0A
+    // TODO: What is this differed thing?%0A
     // d
@@ -650,16 +650,81 @@
 N + %22$%22;
+%0A    public static final int MULTIPLIER_TIME_UNIX_TO_JAVA = 1000;
 %0A%0A    pu
@@ -1469,1311 +1469,133 @@
 DO: 
-the long solution shows 17, 000 days even though frequency is 1 year, maybe didn't subtract somewhere%0A    %09/*%0A        String%5B%5D temporalUnits = %7B%22year%22, %22month%22, %22day%22, %22hour%22, %22min%22%7D;%0A        String duration = DurationFormatUtils.formatDuration(seconds * 1000, %22y:M:d:H:m%22);%0A        String%5B%5D split = duration.split(%22:%22);%0A        StringBuilder builder = new StringBuilder();%0A        for (int i = 0; i %3C split.length; i++) %7B%0A            String temporalUnit = temporalUnits%5Bi%5D;%0A            long time = Long.valueOf(split%5Bi%5D);%0A            if (time %3E= 1) %7B%0A                builder.append(time + %22 %22 + temporalUnit);%0A%0A                // for plurality%0A                if (time %3E 1) %7B%0A                    builder.append(%22s%22);%0A                %7D%0A%0A                builder.append(%22, %22);%0A            %7D%0A        %7D%0A%0A        String parsedDuration = builder.toString();%0A        // drop the last %22, %22%0A        if (!parsedDuration.isEmpty()) %7B%0A            parsedDuration = parsedDuration.substring(0, parsedDuration.length() - 2);%0A        %7D%0A        return parsedDuration.isEmpty()%0A                ? %220 mins%22%0A                : parsedDuration;%0A        */%0A    %09// TODO: the solution below does not give exactly 1 year, and shows the next time the schedule begins%0A        return DateTimeParser.epochSecondToShortDateTime(seconds
+Verify if it is %22exactly 1 year instead of 360 days%22%0A        return p.format(new Date(seconds * MULTIPLIER_TIME_UNIX_TO_JAVA)
 );%0A 

Format Frequency toString via PrettyTime