package mtproto

import (
	
	
	

	
)

// ServerTime tracks the difference between the server clock and the local
// clock, so that message ids and the msg_id time window use server time even
// when the local clock is off.
//
// Like TDLib (AuthData::update_server_time_difference), the difference is
// taken from the time encoded in every received msg_id (and the server_time
// of the key exchange): the first observation sets it, later ones only raise
// it, since a message is always received after the server created it. A
// bad_msg_notification "msg_id too high" resets it from the notification's
// own msg_id.
//
// One ServerTime is shared by all connections of a client. It is safe for
// concurrent use.
type ServerTime struct {
	base clock.Clock

	mux sync.Mutex
	// offset is server time minus local time, in nanoseconds.
	offset atomic.Int64
	// known reports whether offset was observed (not only seeded).
	known bool
}

// NewServerTime creates a ServerTime over the base clock (clock.System if
// nil), starting with the given offset, e.g. a persisted one. The offset is
// replaced by the first observed server time.
func ( clock.Clock,  time.Duration) *ServerTime {
	if  == nil {
		 = clock.System
	}
	 := &ServerTime{base: }
	.offset.Store(int64())
	return 
}

// Now returns the current server time.
func ( *ServerTime) () time.Time {
	return .base.Now().Add(time.Duration(.offset.Load()))
}

// Offset returns server time minus local time.
func ( *ServerTime) () time.Duration {
	return time.Duration(.offset.Load())
}

// Seed sets the offset, e.g. one persisted by a previous run, unless it was
// already observed from the server.
func ( *ServerTime) ( time.Duration) {
	if .Known() {
		return
	}
	.offset.Store(int64())
}

// Known reports whether the offset was observed from the server.
func ( *ServerTime) () bool {
	.mux.Lock()
	defer .mux.Unlock()
	return .known
}

// minOffsetStep is the smallest increase of the offset that is applied, as in
// TDLib (1e-4 s).
const minOffsetStep = 100 * time.Microsecond

// observe updates the offset from a server time point received now. It
// reports whether the offset changed.
func ( *ServerTime) ( time.Time) bool {
	 := int64(.Sub(.base.Now()))

	.mux.Lock()
	defer .mux.Unlock()
	if !.known {
		.known = true
		.offset.Store()
		return true
	}
	if  > .offset.Load()+int64(minOffsetStep) {
		.offset.Store()
		return true
	}
	return false
}

// reset sets the offset from a server time point received now, also when it
// is lower than the current one (the local clock went forward or the offset
// was too high).
func ( *ServerTime) ( time.Time) {
	 := int64(.Sub(.base.Now()))

	.mux.Lock()
	defer .mux.Unlock()
	.known = true
	.offset.Store()
}