package com.medavox.library.mutime;

import android.os.SystemClock;

import java.util.Date;

/**
 * @author Adam Howard
 * created on 16/10/17.
 */
class TimeData {
    private long sntpTime;
    private long uptimeOffset;
    private long clockOffset;

    /**Optional constructor. It's strongly recommended NOT TO USE THIS constructor,
     * as the 3 identical-type arguments can easily be mis-ordered.
     * @deprecated */
    public TimeData(long sntpTime, long uptimeOffset, long clockOffset) {
        this.sntpTime = sntpTime;
        this.uptimeOffset = uptimeOffset;
        this.clockOffset = clockOffset;
    }

    /**
     * time value computed from NTP server response
     */
    public long getRoundTripDelay() {
        return sntpTime;
    }

    /**
     * Device uptime corresponding to the SNTP time.
     * In other words, this is the value of {@link SystemClock#elapsedRealtime()} ()}
     * at the time represented by {@link #getRoundTripDelay()}.
     */
    public long getUptimeOffset() {
        return uptimeOffset;
    }

    /**The System Clock value corresponding to the SNTP time.
     * In other words, this is the value of {@link System#currentTimeMillis()}
     * at the time represented by {@link #getRoundTripDelay()}.*/
    public long getClockOffset() {
        return clockOffset;
    }

    /**Optional Builder class.
     * Recommended because this prevents accidental mis-ordering of arguments.*/
    public static class Builder {

        private long roundTripDelay;
        private long uptimeOffset;
        private long systemClockOffset;

        public Builder() {

        }

        public Builder(TimeData existing) {
            roundTripDelay = existing.getRoundTripDelay();
            uptimeOffset = existing.getUptimeOffset();
            systemClockOffset = existing.getClockOffset();
        }

        public Builder roundTripDelay(long time) {
            roundTripDelay = time;
            return this;
        }

        public Builder uptimeOffset(long time) {
            uptimeOffset = time;
            return this;
        }

        public Builder systemClockOffset(long time) {
            systemClockOffset = time;
            return this;
        }

        public TimeData build() {
            if(roundTripDelay != 0 && uptimeOffset != 0 && systemClockOffset != 0) {
                return new TimeData(roundTripDelay, uptimeOffset, systemClockOffset);
            }
            else {
                throw new IllegalArgumentException("All arguments must be set. Passed Values:"+
                "roundTripDelay="+ roundTripDelay +"; uptimeOffset="+ uptimeOffset
                        +"; systemClockOffset="+ systemClockOffset);
            }
        }
    }
    @Override
    public String toString() {
        return "TimeData ["
                +"Round Trip Delay: "+new Date(sntpTime)
                +"; System Clock offset from true time: "+ clockOffset
                +"; Device Uptime offset from true time: "+ uptimeOffset
                +"]";
    }
}

@@ -66,32 +66,8 @@
 k;%0A%0A
-import java.util.Date;%0A%0A
 /**%0A
@@ -151,24 +151,30 @@
 te long 
-sntpTime
+roundTripDelay
 ;%0A    pr
@@ -428,24 +428,30 @@
 ta(long 
-sntpTime
+roundTripDelay
 , long u
@@ -500,27 +500,39 @@
 his.
-sntpTime = sntpTime
+roundTripDelay = roundTripDelay
 ;%0A  
@@ -739,24 +739,30 @@
  return 
-sntpTime
+roundTripDelay
 ;%0A    %7D%0A
@@ -2856,26 +2856,23 @@
 : %22+
-new Date(sntpTime)
+ roundTripDelay
 %0A   
@@ -2903,39 +2903,24 @@
 Clock offset
- from true time
 : %22+ clockOf
@@ -2968,23 +2968,8 @@
 fset
- from true time
 : %22+

tidy up TimeData