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

package rtcp

import (
	
	
)

// The RapidResynchronizationRequest packet informs the encoder about the loss of an undefined amount of coded video data belonging to one or more pictures
type RapidResynchronizationRequest struct {
	// SSRC of sender
	SenderSSRC uint32

	// SSRC of the media source
	MediaSSRC uint32
}

// RapidResynchronisationRequest is provided as RFC 6051 spells resynchronization with an s.
// We provide both names to be consistent with other RFCs which spell resynchronization with a z.
type RapidResynchronisationRequest = RapidResynchronizationRequest

const (
	rrrLength       = 2
	rrrHeaderLength = ssrcLength * 2
	rrrMediaOffset  = 4
)

// Marshal encodes the RapidResynchronizationRequest in binary
func ( RapidResynchronizationRequest) () ([]byte, error) {
	/*
	 * RRR does not require parameters.  Therefore, the length field MUST be
	 * 2, and there MUST NOT be any Feedback Control Information.
	 *
	 * The semantics of this FB message is independent of the payload type.
	 */
	 := make([]byte, .MarshalSize())
	 := [headerLength:]

	binary.BigEndian.PutUint32(, .SenderSSRC)
	binary.BigEndian.PutUint32([rrrMediaOffset:], .MediaSSRC)

	,  := .Header().Marshal()
	if  != nil {
		return nil, 
	}
	copy(, )

	return , nil
}

// Unmarshal decodes the RapidResynchronizationRequest from binary
func ( *RapidResynchronizationRequest) ( []byte) error {
	if len() < (headerLength + (ssrcLength * 2)) {
		return errPacketTooShort
	}

	var  Header
	if  := .Unmarshal();  != nil {
		return 
	}

	if .Type != TypeTransportSpecificFeedback || .Count != FormatRRR {
		return errWrongType
	}

	.SenderSSRC = binary.BigEndian.Uint32([headerLength:])
	.MediaSSRC = binary.BigEndian.Uint32([headerLength+ssrcLength:])
	return nil
}

// MarshalSize returns the size of the packet once marshaled
func ( *RapidResynchronizationRequest) () int {
	return headerLength + rrrHeaderLength
}

// Header returns the Header associated with this packet.
func ( *RapidResynchronizationRequest) () Header {
	return Header{
		Count:  FormatRRR,
		Type:   TypeTransportSpecificFeedback,
		Length: rrrLength,
	}
}

// DestinationSSRC returns an array of SSRC values that this packet refers to.
func ( *RapidResynchronizationRequest) () []uint32 {
	return []uint32{.MediaSSRC}
}

func ( *RapidResynchronizationRequest) () string {
	return fmt.Sprintf("RapidResynchronizationRequest %x %x", .SenderSSRC, .MediaSSRC)
}