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

package sdp

import (
	
	
)

// SessionDescription is a a well-defined format for conveying sufficient
// information to discover and participate in a multimedia session.
type SessionDescription struct {
	// v=0
	// https://tools.ietf.org/html/rfc4566#section-5.1
	Version Version

	// o=<username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address>
	// https://tools.ietf.org/html/rfc4566#section-5.2
	Origin Origin

	// s=<session name>
	// https://tools.ietf.org/html/rfc4566#section-5.3
	SessionName SessionName

	// i=<session description>
	// https://tools.ietf.org/html/rfc4566#section-5.4
	SessionInformation *Information

	// u=<uri>
	// https://tools.ietf.org/html/rfc4566#section-5.5
	URI *url.URL

	// e=<email-address>
	// https://tools.ietf.org/html/rfc4566#section-5.6
	EmailAddress *EmailAddress

	// p=<phone-number>
	// https://tools.ietf.org/html/rfc4566#section-5.6
	PhoneNumber *PhoneNumber

	// c=<nettype> <addrtype> <connection-address>
	// https://tools.ietf.org/html/rfc4566#section-5.7
	ConnectionInformation *ConnectionInformation

	// b=<bwtype>:<bandwidth>
	// https://tools.ietf.org/html/rfc4566#section-5.8
	Bandwidth []Bandwidth

	// https://tools.ietf.org/html/rfc4566#section-5.9
	// https://tools.ietf.org/html/rfc4566#section-5.10
	TimeDescriptions []TimeDescription

	// z=<adjustment time> <offset> <adjustment time> <offset> ...
	// https://tools.ietf.org/html/rfc4566#section-5.11
	TimeZones []TimeZone

	// k=<method>
	// k=<method>:<encryption key>
	// https://tools.ietf.org/html/rfc4566#section-5.12
	EncryptionKey *EncryptionKey

	// a=<attribute>
	// a=<attribute>:<value>
	// https://tools.ietf.org/html/rfc4566#section-5.13
	Attributes []Attribute

	// https://tools.ietf.org/html/rfc4566#section-5.14
	MediaDescriptions []*MediaDescription
}

// Attribute returns the value of an attribute and if it exists.
func ( *SessionDescription) ( string) (string, bool) {
	for ,  := range .Attributes {
		if .Key ==  {
			return .Value, true
		}
	}

	return "", false
}

// Version describes the value provided by the "v=" field which gives
// the version of the Session Description Protocol.
type Version int

func ( Version) () string {
	return stringFromMarshal(.marshalInto, .marshalSize)
}

func ( Version) ( []byte) []byte {
	return strconv.AppendInt(, int64(), 10)
}

func ( Version) () ( int) {
	return lenInt(int64())
}

// Origin defines the structure for the "o=" field which provides the
// originator of the session plus a session identifier and version number.
type Origin struct {
	Username       string
	SessionID      uint64
	SessionVersion uint64
	NetworkType    string
	AddressType    string
	UnicastAddress string
}

func ( Origin) () string {
	return stringFromMarshal(.marshalInto, .marshalSize)
}

func ( Origin) ( []byte) []byte {
	 = append(append(, .Username...), ' ')
	 = append(strconv.AppendUint(, .SessionID, 10), ' ')
	 = append(strconv.AppendUint(, .SessionVersion, 10), ' ')
	 = append(append(, .NetworkType...), ' ')
	 = append(append(, .AddressType...), ' ')

	return append(, .UnicastAddress...)
}

func ( Origin) () ( int) {
	return len(.Username) +
		lenUint(.SessionID) +
		lenUint(.SessionVersion) +
		len(.NetworkType) +
		len(.AddressType) +
		len(.UnicastAddress) +
		5
}

// SessionName describes a structured representations for the "s=" field
// and is the textual session name.
type SessionName string

func ( SessionName) () string {
	return stringFromMarshal(.marshalInto, .marshalSize)
}

func ( SessionName) ( []byte) []byte {
	return append(, ...)
}

func ( SessionName) () ( int) {
	return len()
}

// EmailAddress describes a structured representations for the "e=" line
// which specifies email contact information for the person responsible for
// the conference.
type EmailAddress string

func ( EmailAddress) () string {
	return stringFromMarshal(.marshalInto, .marshalSize)
}

func ( EmailAddress) ( []byte) []byte {
	return append(, ...)
}

func ( EmailAddress) () ( int) {
	return len()
}

// PhoneNumber describes a structured representations for the "p=" line
// specify phone contact information for the person responsible for the
// conference.
type PhoneNumber string

func ( PhoneNumber) () string {
	return stringFromMarshal(.marshalInto, .marshalSize)
}

func ( PhoneNumber) ( []byte) []byte {
	return append(, ...)
}

func ( PhoneNumber) () ( int) {
	return len()
}

// TimeZone defines the structured object for "z=" line which describes
// repeated sessions scheduling.
type TimeZone struct {
	AdjustmentTime uint64
	Offset         int64
}

func ( TimeZone) () string {
	return stringFromMarshal(.marshalInto, .marshalSize)
}

func ( TimeZone) ( []byte) []byte {
	 = strconv.AppendUint(, .AdjustmentTime, 10)
	 = append(, ' ')

	return strconv.AppendInt(, .Offset, 10)
}

func ( TimeZone) () ( int) {
	return lenUint(.AdjustmentTime) + 1 + lenInt(.Offset)
}