package be.iminds.ilabt.jfed.rspec.model.controller;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Cedric
 */
@XmlRootElement
public class Timestamp {
    private double ms;

    public Timestamp(double d) {
        ms = d;
    }
    
    public Timestamp(){
        this(0);
    }
    
    public double getTime(){
        return ms;
    }
    
    @XmlElement
    public void setTime(double ms){
        this.ms = ms;
    }
    
    public Timestamp add(Timestamp t){
        return new Timestamp(t.toMillis() + toMillis());
    }
    
    public double toMillis(){
        return ms;
    }
    
    public double toHours(){
        return ms / 1000 / 60 / 60;
    }
    
    public double toMinutes(){
        return ms / 1000 / 60;
    }
    
    public double toSeconds(){
        return ms / 1000;
    }
    
    /**
     * Max time = 99:59:59
     * @return a string representation of the Timestamp hh:mm:ss
     */
    @Override
    public String toString(){
        String t = "";
        
        double hours = toHours()%99;
        double minutes = toMinutes()%60;
        double seconds = toSeconds()%60;
        
        t+= (hours < 10 ? "0" + (int) hours : (int) hours);
        t+= ":";
        t+= (minutes< 10 ? "0" + (int) minutes : (int) minutes);
        t+= ":";
        t+= (seconds< 10 ? "0" + (int) seconds : (int) seconds);
        
        return t;
    }

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

        Timestamp timestamp = (Timestamp) o;

        if (Double.compare(timestamp.ms, ms) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(ms);
        return (int) (temp ^ (temp >>> 32));
    }
}
