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

package extension

import (
	
)

const (
	useSRTPHeaderSize = 6
)

// UseSRTP allows a Client/Server to negotiate what SRTPProtectionProfiles
// they both support
//
// https://tools.ietf.org/html/rfc8422
type UseSRTP struct {
	ProtectionProfiles  []SRTPProtectionProfile
	MasterKeyIdentifier []byte
}

// TypeValue returns the extension TypeValue.
func ( UseSRTP) () TypeValue {
	return UseSRTPTypeValue
}

// Marshal encodes the extension.
func ( *UseSRTP) () ([]byte, error) {
	 := make([]byte, useSRTPHeaderSize)

	binary.BigEndian.PutUint16(, uint16(.TypeValue()))
	//nolint:gosec // G115
	binary.BigEndian.PutUint16(
		[2:],
		uint16(2+(len(.ProtectionProfiles)*2)+ /* MKI Length */ 1+len(.MasterKeyIdentifier)),
	)
	binary.BigEndian.PutUint16([4:], uint16(len(.ProtectionProfiles)*2)) //nolint:gosec // G115

	for ,  := range .ProtectionProfiles {
		 = append(, []byte{0x00, 0x00}...) //nolint:makezero // todo: fix
		binary.BigEndian.PutUint16([len()-2:], uint16())
	}
	if len(.MasterKeyIdentifier) > 255 {
		return nil, errMasterKeyIdentifierTooLarge
	}

	 = append(, byte(len(.MasterKeyIdentifier))) //nolint:makezero // todo: fix
	 = append(, .MasterKeyIdentifier...)         //nolint:makezero // todo: fix

	return , nil
}

// Unmarshal populates the extension from encoded data.
func ( *UseSRTP) ( []byte) error {
	if len() <= useSRTPHeaderSize {
		return errBufferTooSmall
	} else if TypeValue(binary.BigEndian.Uint16()) != .TypeValue() {
		return errInvalidExtensionType
	}

	 := int(binary.BigEndian.Uint16([4:]) / 2)
	 := supportedGroupsHeaderSize + ( * 2)
	if +1 > len() {
		return errLengthMismatch
	}

	for  := 0;  < ; ++ {
		 := SRTPProtectionProfile(binary.BigEndian.Uint16([(useSRTPHeaderSize + ( * 2)):]))
		if ,  := srtpProtectionProfiles()[];  {
			.ProtectionProfiles = append(.ProtectionProfiles, )
		}
	}

	 := int([])
	if + >= len() {
		return errLengthMismatch
	}

	.MasterKeyIdentifier = append(
		[]byte{},
		[+1:+1+]...,
	)

	return nil
}