// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package ice

import (
	
	
	

	
)

func newCandidatePair(,  Candidate,  bool) *CandidatePair {
	return &CandidatePair{
		iceRoleControlling: ,
		Remote:             ,
		Local:              ,
		state:              CandidatePairStateWaiting,
	}
}

// CandidatePair is a combination of a local and remote candidate.
type CandidatePair struct {
	iceRoleControlling       bool
	Remote                   Candidate
	Local                    Candidate
	bindingRequestCount      uint16
	state                    CandidatePairState
	nominated                bool
	nominateOnBindingSuccess bool

	// stats
	currentRoundTripTime int64 // in ns
	totalRoundTripTime   int64 // in ns

	requestsReceived  uint64
	requestsSent      uint64
	responsesReceived uint64
	responsesSent     uint64

	firstRequestSentAt     atomic.Value // time.Time
	lastRequestSentAt      atomic.Value // time.Time
	firstReponseReceivedAt atomic.Value // time.Time
	lastResponseReceivedAt atomic.Value // time.Time
	firstRequestReceivedAt atomic.Value // time.Time
	lastRequestReceivedAt  atomic.Value // time.Time
}

func ( *CandidatePair) () string {
	if  == nil {
		return ""
	}

	return fmt.Sprintf(
		"prio %d (local, prio %d) %s <-> %s (remote, prio %d), state: %s, nominated: %v, nominateOnBindingSuccess: %v",
		.priority(),
		.Local.Priority(),
		.Local,
		.Remote,
		.Remote.Priority(),
		.state,
		.nominated,
		.nominateOnBindingSuccess,
	)
}

func ( *CandidatePair) ( *CandidatePair) bool {
	if  == nil &&  == nil {
		return true
	}
	if  == nil ||  == nil {
		return false
	}

	return .Local.Equal(.Local) && .Remote.Equal(.Remote)
}

// RFC 5245 - 5.7.2.  Computing Pair Priority and Ordering Pairs
// Let G be the priority for the candidate provided by the controlling
// agent.  Let D be the priority for the candidate provided by the
// controlled agent.
// pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0).
func ( *CandidatePair) () uint64 {
	var ,  uint32 //nolint:varnamelen // clearer to use g and d here
	if .iceRoleControlling {
		 = .Local.Priority()
		 = .Remote.Priority()
	} else {
		 = .Remote.Priority()
		 = .Local.Priority()
	}

	// Just implement these here rather
	// than fooling around with the math package
	 := func(,  uint32) uint64 {
		if  <  {
			return uint64()
		}

		return uint64()
	}
	 := func(,  uint32) uint64 {
		if  >  {
			return uint64()
		}

		return uint64()
	}
	 := func(,  uint32) uint64 {
		if  >  {
			return uint64(1)
		}

		return uint64(0)
	}

	// 1<<32 overflows uint32; and if both g && d are
	// maxUint32, this result would overflow uint64
	return (1<<32-1)*(, ) + 2*(, ) + (, )
}

func ( *CandidatePair) ( []byte) (int, error) {
	return .Local.writeTo(, .Remote)
}

func ( *Agent) ( *stun.Message, ,  Candidate) {
	,  := .writeTo(.Raw, )
	if  != nil {
		.log.Tracef("Failed to send STUN message: %s", )
	}
}

// UpdateRoundTripTime sets the current round time of this pair and
// accumulates total round trip time and responses received.
func ( *CandidatePair) ( time.Duration) {
	 := .Nanoseconds()
	atomic.StoreInt64(&.currentRoundTripTime, )
	atomic.AddInt64(&.totalRoundTripTime, )
	atomic.AddUint64(&.responsesReceived, 1)

	 := time.Now()
	.firstReponseReceivedAt.CompareAndSwap(nil, )
	.lastResponseReceivedAt.Store()
}

// CurrentRoundTripTime returns the current round trip time in seconds
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
func ( *CandidatePair) () float64 {
	return time.Duration(atomic.LoadInt64(&.currentRoundTripTime)).Seconds()
}

// TotalRoundTripTime returns the current round trip time in seconds
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime
func ( *CandidatePair) () float64 {
	return time.Duration(atomic.LoadInt64(&.totalRoundTripTime)).Seconds()
}

// RequestsReceived returns the total number of connectivity checks received
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-requestsreceived
func ( *CandidatePair) () uint64 {
	return atomic.LoadUint64(&.requestsReceived)
}

// RequestsSent returns the total number of connectivity checks sent
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-requestssent
func ( *CandidatePair) () uint64 {
	return atomic.LoadUint64(&.requestsSent)
}

// ResponsesReceived returns the total number of connectivity responses received
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-responsesreceived
func ( *CandidatePair) () uint64 {
	return atomic.LoadUint64(&.responsesReceived)
}

// ResponsesSent returns the total number of connectivity responses sent
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-responsessent
func ( *CandidatePair) () uint64 {
	return atomic.LoadUint64(&.responsesSent)
}

// FirstRequestSentAt returns the timestamp of the first connectivity check sent.
func ( *CandidatePair) () time.Time {
	if ,  := .firstRequestSentAt.Load().(time.Time);  {
		return 
	}

	return time.Time{}
}

// LastRequestSentAt returns the timestamp of the last connectivity check sent.
func ( *CandidatePair) () time.Time {
	if ,  := .lastRequestSentAt.Load().(time.Time);  {
		return 
	}

	return time.Time{}
}

// FirstReponseReceivedAt returns the timestamp of the first connectivity response received.
func ( *CandidatePair) () time.Time {
	if ,  := .firstReponseReceivedAt.Load().(time.Time);  {
		return 
	}

	return time.Time{}
}

// LastResponseReceivedAt returns the timestamp of the last connectivity response received.
func ( *CandidatePair) () time.Time {
	if ,  := .lastResponseReceivedAt.Load().(time.Time);  {
		return 
	}

	return time.Time{}
}

// FirstRequestReceivedAt returns the timestamp of the first connectivity check received.
func ( *CandidatePair) () time.Time {
	if ,  := .firstRequestReceivedAt.Load().(time.Time);  {
		return 
	}

	return time.Time{}
}

// LastRequestReceivedAt returns the timestamp of the last connectivity check received.
func ( *CandidatePair) () time.Time {
	if ,  := .lastRequestReceivedAt.Load().(time.Time);  {
		return 
	}

	return time.Time{}
}

// UpdateRequestSent increments the number of requests sent and updates the timestamp.
func ( *CandidatePair) () {
	atomic.AddUint64(&.requestsSent, 1)
	 := time.Now()
	.firstRequestSentAt.CompareAndSwap(nil, )
	.lastRequestSentAt.Store()
}

// UpdateResponseSent increments the number of responses sent.
func ( *CandidatePair) () {
	atomic.AddUint64(&.responsesSent, 1)
}

// UpdateRequestReceived increments the number of requests received and updates the timestamp.
func ( *CandidatePair) () {
	atomic.AddUint64(&.requestsReceived, 1)
	 := time.Now()
	.firstRequestReceivedAt.CompareAndSwap(nil, )
	.lastRequestReceivedAt.Store()
}