package com.jscape.util;

import java.util.concurrent.TimeUnit;

import static com.jscape.util.Parameters.assertGreaterOrEqual;
import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
@SuppressObfuscation
public class Time
        implements Comparable<Time> {

    public final long value;
    public final TimeUnit unit;

    public static Time nanos(long value) {
        return new Time(value, TimeUnit.NANOSECONDS);
    }

    public static Time micros(long value) {
        return new Time(value, TimeUnit.MICROSECONDS);
    }

    public static Time millis(long value) {
        return new Time(value, TimeUnit.MILLISECONDS);
    }

    public static Time seconds(long value) {
        return new Time(value, TimeUnit.SECONDS);
    }

    public static Time minutes(long value) {
        return new Time(value * 60, TimeUnit.SECONDS);
    }

    public static Time hours(long value) {
        return new Time(value * 60 * 60, TimeUnit.SECONDS);
    }

    public static Time days(long value) {
        return new Time(value * 60 * 60 * 24, TimeUnit.SECONDS);
    }

    public Time(long value, TimeUnit unit) {
        assertGreaterOrEqual(value, 0, "Bad time value.");
        this.value = value;

        assertNotNull(unit);
        this.unit = unit;
    }

    public long toNanos() {
        return this.unit.toNanos(this.value);
    }

    public long toMicros() {
        return this.unit.toMicros(this.value);
    }

    public long toMillis() {
        return this.unit.toMillis(this.value);
    }

    public long toSeconds() {
        return this.unit.toSeconds(this.value);
    }

    public long toMinutes() {
        return this.unit.toMinutes(this.value);
    }

    public long toHours() {
        return this.unit.toHours(this.value);
    }

    public long toDays() {
        return this.unit.toDays(this.value);
    }

    public void timedWait(Object obj)
            throws InterruptedException {
        this.unit.timedWait(obj, this.value);
    }

    public void timedJoin(Thread thread)
            throws InterruptedException {
        this.unit.timedJoin(thread, this.value);
    }

    public void sleep()
            throws InterruptedException {
        this.unit.sleep(this.value);
    }

    public void sleepSafe() {
        try {
            sleep();
        } catch (Exception e) {
            // ignore
        }
    }

    public int compareTo(Time that) {
        long thisNanos = toNanos();
        long thatNanos = that.toNanos();

        return thisNanos < thatNanos ? -1 : thisNanos == thatNanos ? 0 : 1;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Time that = (Time) o;

        return toNanos() == that.toNanos();
    }

    @Override
    public int hashCode() {
        int result = (int) (this.value ^ (this.value >>> 32));
        result = 31 * result + this.unit.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Time {"
                + "value=" + this.value
                + ", unit=" + this.unit
                + '}';
    }
}
