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

package webrtc

import (
	
	

	
)

// RTPCodecType determines the type of a codec.
type RTPCodecType int

const (
	// RTPCodecTypeUnknown is the enum's zero-value.
	RTPCodecTypeUnknown RTPCodecType = iota

	// RTPCodecTypeAudio indicates this is an audio codec.
	RTPCodecTypeAudio

	// RTPCodecTypeVideo indicates this is a video codec.
	RTPCodecTypeVideo
)

func ( RTPCodecType) () string {
	switch  {
	case RTPCodecTypeAudio:
		return "audio" //nolint: goconst
	case RTPCodecTypeVideo:
		return "video" //nolint: goconst
	default:
		return ErrUnknownType.Error()
	}
}

// NewRTPCodecType creates a RTPCodecType from a string.
func ( string) RTPCodecType {
	switch {
	case strings.EqualFold(, RTPCodecTypeAudio.String()):
		return RTPCodecTypeAudio
	case strings.EqualFold(, RTPCodecTypeVideo.String()):
		return RTPCodecTypeVideo
	default:
		return RTPCodecType(0)
	}
}

// RTPCodecCapability provides information about codec capabilities.
//
// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpcodeccapability-members
type RTPCodecCapability struct {
	MimeType     string
	ClockRate    uint32
	Channels     uint16
	SDPFmtpLine  string
	RTCPFeedback []RTCPFeedback
}

// RTPHeaderExtensionCapability is used to define a RFC5285 RTP header extension supported by the codec.
//
// https://w3c.github.io/webrtc-pc/#dom-rtcrtpcapabilities-headerextensions
type RTPHeaderExtensionCapability struct {
	URI string
}

// RTPHeaderExtensionParameter represents a negotiated RFC5285 RTP header extension.
//
// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpheaderextensionparameters-members
type RTPHeaderExtensionParameter struct {
	URI string
	ID  int
}

// RTPCodecParameters is a sequence containing the media codecs that an RtpSender
// will choose from, as well as entries for RTX, RED and FEC mechanisms. This also
// includes the PayloadType that has been negotiated
//
// https://w3c.github.io/webrtc-pc/#rtcrtpcodecparameters
type RTPCodecParameters struct {
	RTPCodecCapability
	PayloadType PayloadType

	statsID string
}

// RTPParameters is a list of negotiated codecs and header extensions
//
// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpparameters-members
type RTPParameters struct {
	HeaderExtensions []RTPHeaderExtensionParameter
	Codecs           []RTPCodecParameters
}

type codecMatchType int

const (
	codecMatchNone    codecMatchType = 0
	codecMatchPartial codecMatchType = 1
	codecMatchExact   codecMatchType = 2
)

// Do a fuzzy find for a codec in the list of codecs
// Used for lookup up a codec in an existing list to find a match
// Returns codecMatchExact, codecMatchPartial, or codecMatchNone.
func codecParametersFuzzySearch(
	 RTPCodecParameters,
	 []RTPCodecParameters,
) (RTPCodecParameters, codecMatchType) {
	 := fmtp.Parse(
		.RTPCodecCapability.MimeType,
		.RTPCodecCapability.ClockRate,
		.RTPCodecCapability.Channels,
		.RTPCodecCapability.SDPFmtpLine)

	// First attempt to match on MimeType + ClockRate + Channels + SDPFmtpLine
	for ,  := range  {
		 := fmtp.Parse(
			.RTPCodecCapability.MimeType,
			.RTPCodecCapability.ClockRate,
			.RTPCodecCapability.Channels,
			.RTPCodecCapability.SDPFmtpLine)

		if .Match() {
			return , codecMatchExact
		}
	}

	// Fallback to just MimeType + ClockRate + Channels
	for ,  := range  {
		if strings.EqualFold(.RTPCodecCapability.MimeType, .RTPCodecCapability.MimeType) &&
			fmtp.ClockRateEqual(.RTPCodecCapability.MimeType,
				.RTPCodecCapability.ClockRate,
				.RTPCodecCapability.ClockRate) &&
			fmtp.ChannelsEqual(.RTPCodecCapability.MimeType,
				.RTPCodecCapability.Channels,
				.RTPCodecCapability.Channels) {
			return , codecMatchPartial
		}
	}

	return RTPCodecParameters{}, codecMatchNone
}

// Given a CodecParameters find the RTX CodecParameters if one exists.
func findRTXPayloadType( PayloadType,  []RTPCodecParameters) PayloadType {
	 := fmt.Sprintf("apt=%d", )
	for ,  := range  {
		if  == .SDPFmtpLine {
			return .PayloadType
		}
	}

	return PayloadType(0)
}

// For now, only FlexFEC is supported.
func findFECPayloadType( []RTPCodecParameters) PayloadType {
	for ,  := range  {
		if strings.Contains(.RTPCodecCapability.MimeType, MimeTypeFlexFEC) {
			return .PayloadType
		}
	}

	return PayloadType(0)
}

func rtcpFeedbackIntersection(,  []RTCPFeedback) ( []RTCPFeedback) {
	for ,  := range  {
		for ,  := range  {
			if .Type == .Type && .Parameter == .Parameter {
				 = append(, )

				break
			}
		}
	}

	return
}