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

package rfc8888

import (
	

	
	
)

type packetReport struct {
	arrivalTime time.Time
	ecn         uint8
}

// Recorder records incoming RTP packets and their arrival times. Recorder can
// be used to create feedback reports as defined by RFC 8888.
type Recorder struct {
	ssrc    uint32
	streams map[uint32]*streamLog
}

// NewRecorder creates a new Recorder.
func () *Recorder {
	return &Recorder{
		streams: map[uint32]*streamLog{},
	}
}

// AddPacket writes a packet to the underlying stream.
func ( *Recorder) ( time.Time,  uint32,  uint16,  uint8) {
	,  := .streams[]
	if ! {
		 = newStreamLog()
		.streams[] = 
	}
	.add(, , )
}

// BuildReport creates a new rtcp.CCFeedbackReport containing all packets that
// were added by AddPacket and missing packets.
func ( *Recorder) ( time.Time,  int) *rtcp.CCFeedbackReport {
	 := &rtcp.CCFeedbackReport{
		SenderSSRC:      .ssrc,
		ReportBlocks:    []rtcp.CCFeedbackReportBlock{},
		ReportTimestamp: ntp.ToNTP32(),
	}

	 := ( - 12 - (8 * len(.streams))) / 2
	var  int
	if len(.streams) > 1 {
		 =  / (len(.streams) - 1)
	} else {
		 = 
	}

	for ,  := range .streams {
		if len(.streams) > 1 && int() == len(.streams)-1 {
			 =  % len(.streams)
		}
		 := .metricsAfter(, int64())
		.ReportBlocks = append(.ReportBlocks, )
	}

	return 
}