package com.jdblabs.timestamper.core;

import java.util.Date;
import java.util.UUID;

/**
* @author Jonathan Bernard {@literal <jdbernard@gmail.com>}
* This represents a marker on the timeline.
* The date of the marker and the mark cannot be changed once assigned.
*/
public class TimelineMarker implements Comparable<TimelineMarker> {

    private final Date timestamp;
    private final String mark;
    private final UUID uuid;
    private String notes;

    public TimelineMarker(Date timestamp, String mark, String notes) {
        this(timestamp, mark, notes, UUID.randomUUID()); }

    public TimelineMarker(Date timestamp, String mark, String notes, UUID uuid) {
        if (timestamp == null || mark == null)
            throw new IllegalArgumentException("Null timestamp or mark"
                    + " is not permitted.");

        // We truncate milliseconds.
        long seconds = 1000l * (timestamp.getTime() / 1000l);
        this.timestamp = new Date(seconds);
        this.mark = mark;
        this.notes = notes;
        this.uuid = uuid;
    }

    public Date getTimestamp() { return timestamp; }

    public String getMark() { return mark; }

    public String getNotes() { return notes; }

    public UUID getUuid() { return uuid; }

    public void setNotes(String notes) { this.notes = notes; }

    @Override
    public int compareTo(TimelineMarker that) {
        // Always greater than null
        if (that == null) return Integer.MAX_VALUE;

        // Always equal to other instances of itself (same UUID).
        if (this.uuid.equals(that.uuid)) return 0;

        // Check the timestamp, then the mark if the timestamps are equal.
        int val = this.timestamp.compareTo(that.timestamp);
        if (val == 0) val = this.mark.compareTo(that.mark);

        return val;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (!(o instanceof TimelineMarker)) return false;

        TimelineMarker that = (TimelineMarker) o;
        return this.uuid.equals(that.uuid);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 61 * hash + (this.timestamp != null ? this.timestamp.hashCode() : 0);
        hash = 61 * hash + (this.mark != null ? this.mark.hashCode() : 0);
        return hash;
    }

}

