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

package rtcp

import (
	
	
)

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

	// SSRC where the loss was experienced
	MediaSSRC uint32
}

const (
	pliLength = 2
)

// Marshal encodes the PictureLossIndication in binary
func ( PictureLossIndication) () ([]byte, error) {
	/*
	 * PLI 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([4:], .MediaSSRC)

	 := Header{
		Count:  FormatPLI,
		Type:   TypePayloadSpecificFeedback,
		Length: pliLength,
	}
	,  := .Marshal()
	if  != nil {
		return nil, 
	}
	copy(, )

	return , nil
}

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

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

	if .Type != TypePayloadSpecificFeedback || .Count != FormatPLI {
		return errWrongType
	}

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

// Header returns the Header associated with this packet.
func ( *PictureLossIndication) () Header {
	return Header{
		Count:  FormatPLI,
		Type:   TypePayloadSpecificFeedback,
		Length: pliLength,
	}
}

// MarshalSize returns the size of the packet once marshaled
func ( *PictureLossIndication) () int {
	return headerLength + ssrcLength*2
}

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

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