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

package sdp

import (
	
	
	
	
)

// Constants for SDP attributes used in JSEP.
const (
	AttrKeyCandidate        = "candidate"
	AttrKeyEndOfCandidates  = "end-of-candidates"
	AttrKeyIdentity         = "identity"
	AttrKeyGroup            = "group"
	AttrKeySSRC             = "ssrc"
	AttrKeySSRCGroup        = "ssrc-group"
	AttrKeyMsid             = "msid"
	AttrKeyMsidSemantic     = "msid-semantic"
	AttrKeyConnectionSetup  = "setup"
	AttrKeyMID              = "mid"
	AttrKeyICELite          = "ice-lite"
	AttrKeyICEOptions       = "ice-options"
	AttrKeyRTCPMux          = "rtcp-mux"
	AttrKeyRTCPRsize        = "rtcp-rsize"
	AttrKeyInactive         = "inactive"
	AttrKeyRecvOnly         = "recvonly"
	AttrKeySendOnly         = "sendonly"
	AttrKeySendRecv         = "sendrecv"
	AttrKeyExtMap           = "extmap"
	AttrKeyExtMapAllowMixed = "extmap-allow-mixed"
)

// Constants for semantic tokens used in JSEP.
const (
	SemanticTokenLipSynchronization     = "LS"
	SemanticTokenFlowIdentification     = "FID"
	SemanticTokenForwardErrorCorrection = "FEC"
	// https://datatracker.ietf.org/doc/html/rfc5956#section-4.1
	SemanticTokenForwardErrorCorrectionFramework = "FEC-FR"
	SemanticTokenWebRTCMediaStreams              = "WMS"
)

// Constants for extmap key.
const (
	ExtMapValueTransportCC = 3
)

func extMapURI() map[int]string {
	return map[int]string{
		ExtMapValueTransportCC: "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
	}
}

// API to match draft-ietf-rtcweb-jsep
// Move to webrtc or its own package?

// NewJSEPSessionDescription creates a new SessionDescription with
// some settings that are required by the JSEP spec.
//
// Note: Since v2.4.0, session ID has been fixed to use crypto random according to
//
//	JSEP spec, so that NewJSEPSessionDescription now returns error as a second
//	return value.
func ( bool) (*SessionDescription, error) {
	,  := newSessionID()
	if  != nil {
		return nil, 
	}
	 := &SessionDescription{
		Version: 0,
		Origin: Origin{
			Username:       "-",
			SessionID:      ,
			SessionVersion: uint64(time.Now().Unix()), //nolint:gosec // G115
			NetworkType:    "IN",
			AddressType:    "IP4",
			UnicastAddress: "0.0.0.0",
		},
		SessionName: "-",
		TimeDescriptions: []TimeDescription{
			{
				Timing: Timing{
					StartTime: 0,
					StopTime:  0,
				},
				RepeatTimes: nil,
			},
		},
		Attributes: []Attribute{
			// 	"Attribute(ice-options:trickle)", // TODO: implement trickle ICE
		},
	}

	if  {
		.WithPropertyAttribute(AttrKeyIdentity)
	}

	return , nil
}

// WithPropertyAttribute adds a property attribute 'a=key' to the session description.
func ( *SessionDescription) ( string) *SessionDescription {
	.Attributes = append(.Attributes, NewPropertyAttribute())

	return 
}

// WithValueAttribute adds a value attribute 'a=key:value' to the session description.
func ( *SessionDescription) (,  string) *SessionDescription {
	.Attributes = append(.Attributes, NewAttribute(, ))

	return 
}

// WithICETrickleAdvertised advertises ICE trickle support in the session description.
// See https://datatracker.ietf.org/doc/html/rfc9429#section-5.2.1
func ( *SessionDescription) () *SessionDescription {
	return .WithValueAttribute(AttrKeyICEOptions, "trickle")
}

// WithFingerprint adds a fingerprint to the session description.
func ( *SessionDescription) (,  string) *SessionDescription {
	return .WithValueAttribute("fingerprint", +" "+)
}

// WithMedia adds a media description to the session description.
func ( *SessionDescription) ( *MediaDescription) *SessionDescription {
	.MediaDescriptions = append(.MediaDescriptions, )

	return 
}

// NewJSEPMediaDescription creates a new MediaName with
// some settings that are required by the JSEP spec.
func ( string,  []string) *MediaDescription {
	return &MediaDescription{
		MediaName: MediaName{
			Media:  ,
			Port:   RangedPort{Value: 9},
			Protos: []string{"UDP", "TLS", "RTP", "SAVPF"},
		},
		ConnectionInformation: &ConnectionInformation{
			NetworkType: "IN",
			AddressType: "IP4",
			Address: &Address{
				Address: "0.0.0.0",
			},
		},
	}
}

// WithPropertyAttribute adds a property attribute 'a=key' to the media description.
func ( *MediaDescription) ( string) *MediaDescription {
	.Attributes = append(.Attributes, NewPropertyAttribute())

	return 
}

// WithValueAttribute adds a value attribute 'a=key:value' to the media description.
func ( *MediaDescription) (,  string) *MediaDescription {
	.Attributes = append(.Attributes, NewAttribute(, ))

	return 
}

// WithFingerprint adds a fingerprint to the media description.
func ( *MediaDescription) (,  string) *MediaDescription {
	return .WithValueAttribute("fingerprint", +" "+)
}

// WithICECredentials adds ICE credentials to the media description.
func ( *MediaDescription) (,  string) *MediaDescription {
	return .
		WithValueAttribute("ice-ufrag", ).
		WithValueAttribute("ice-pwd", )
}

// WithCodec adds codec information to the media description.
func ( *MediaDescription) (
	 uint8,
	 string,
	 uint32,
	 uint16,
	 string,
) *MediaDescription {
	.MediaName.Formats = append(.MediaName.Formats, strconv.Itoa(int()))
	 := fmt.Sprintf("%d %s/%d", , , )
	if  > 0 {
		 += fmt.Sprintf("/%d", )
	}
	.WithValueAttribute("rtpmap", )
	if  != "" {
		.WithValueAttribute("fmtp", fmt.Sprintf("%d %s", , ))
	}

	return 
}

// WithMediaSource adds media source information to the media description.
func ( *MediaDescription) ( uint32, , ,  string) *MediaDescription {
	return .
		WithValueAttribute("ssrc", fmt.Sprintf("%d cname:%s", , )). // Deprecated but not phased out?
		WithValueAttribute("ssrc", fmt.Sprintf("%d msid:%s %s", , , )).
		WithValueAttribute("ssrc", fmt.Sprintf("%d mslabel:%s", , )). // Deprecated but not phased out?
		WithValueAttribute("ssrc", fmt.Sprintf("%d label:%s", , ))          // Deprecated but not phased out?
}

// WithCandidate adds an ICE candidate to the media description.
// Deprecated: use WithICECandidate instead.
func ( *MediaDescription) ( string) *MediaDescription {
	return .WithValueAttribute("candidate", )
}

// WithExtMap adds an extmap to the media description.
func ( *MediaDescription) ( ExtMap) *MediaDescription {
	return .WithPropertyAttribute(.Marshal())
}

// WithTransportCCExtMap adds an extmap to the media description.
func ( *MediaDescription) () *MediaDescription {
	,  := url.Parse(extMapURI()[ExtMapValueTransportCC])
	 := ExtMap{
		Value: ExtMapValueTransportCC,
		URI:   ,
	}

	return .WithExtMap()
}