package aoiprxkit

import "sync/atomic"

type Stats struct {
	PacketsReceived  uint64 `json:"packetsReceived"`
	PacketsParsed    uint64 `json:"packetsParsed"`
	PacketsDelivered uint64 `json:"packetsDelivered"`
	PacketsLateDrop  uint64 `json:"packetsLateDrop"`
	PacketsGapLoss   uint64 `json:"packetsGapLoss"`
	PacketsWrongPT   uint64 `json:"packetsWrongPayloadType"`
	PacketsShort     uint64 `json:"packetsTooShort"`
	JitterReorders   uint64 `json:"jitterReorders"`
	DecodeErrors     uint64 `json:"decodeErrors"`
	SamplesDelivered uint64 `json:"samplesDelivered"`
	FramesDelivered  uint64 `json:"framesDelivered"`
	LastSequence     uint32 `json:"lastSequence"`
	SequenceValid    uint32 `json:"sequenceValid"`
}

type statsAtomic struct {
	packetsReceived  atomic.Uint64
	packetsParsed    atomic.Uint64
	packetsDelivered atomic.Uint64
	packetsLateDrop  atomic.Uint64
	packetsGapLoss   atomic.Uint64
	packetsWrongPT   atomic.Uint64
	packetsShort     atomic.Uint64
	jitterReorders   atomic.Uint64
	decodeErrors     atomic.Uint64
	samplesDelivered atomic.Uint64
	framesDelivered  atomic.Uint64
	lastSequence     atomic.Uint32
	sequenceValid    atomic.Uint32
}

func (s *statsAtomic) snapshot() Stats {
	return Stats{
		PacketsReceived:  s.packetsReceived.Load(),
		PacketsParsed:    s.packetsParsed.Load(),
		PacketsDelivered: s.packetsDelivered.Load(),
		PacketsLateDrop:  s.packetsLateDrop.Load(),
		PacketsGapLoss:   s.packetsGapLoss.Load(),
		PacketsWrongPT:   s.packetsWrongPT.Load(),
		PacketsShort:     s.packetsShort.Load(),
		JitterReorders:   s.jitterReorders.Load(),
		DecodeErrors:     s.decodeErrors.Load(),
		SamplesDelivered: s.samplesDelivered.Load(),
		FramesDelivered:  s.framesDelivered.Load(),
		LastSequence:     s.lastSequence.Load(),
		SequenceValid:    s.sequenceValid.Load(),
	}
}
