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

package stun

// NewUsername returns Username with provided value.
func ( string) Username {
	return Username()
}

// Username represents USERNAME attribute.
//
// RFC 5389 Section 15.3
type Username []byte

func ( Username) () string {
	return string()
}

const maxUsernameB = 513

// AddTo adds USERNAME attribute to message.
func ( Username) ( *Message) error {
	return TextAttribute().AddToAs(, AttrUsername, maxUsernameB)
}

// GetFrom gets USERNAME from message.
func ( *Username) ( *Message) error {
	return (*TextAttribute)().GetFromAs(, AttrUsername)
}

// NewRealm returns Realm with provided value.
// Must be SASL-prepared.
func ( string) Realm {
	return Realm()
}

// Realm represents REALM attribute.
//
// RFC 5389 Section 15.7
type Realm []byte

func ( Realm) () string {
	return string()
}

const maxRealmB = 763

// AddTo adds NONCE to message.
func ( Realm) ( *Message) error {
	return TextAttribute().AddToAs(, AttrRealm, maxRealmB)
}

// GetFrom gets REALM from message.
func ( *Realm) ( *Message) error {
	return (*TextAttribute)().GetFromAs(, AttrRealm)
}

const softwareRawMaxB = 763

// Software is SOFTWARE attribute.
//
// RFC 5389 Section 15.10
type Software []byte

func ( Software) () string {
	return string()
}

// NewSoftware returns *Software from string.
func ( string) Software {
	return Software()
}

// AddTo adds Software attribute to m.
func ( Software) ( *Message) error {
	return TextAttribute().AddToAs(, AttrSoftware, softwareRawMaxB)
}

// GetFrom decodes Software from m.
func ( *Software) ( *Message) error {
	return (*TextAttribute)().GetFromAs(, AttrSoftware)
}

// Nonce represents NONCE attribute.
//
// RFC 5389 Section 15.8
type Nonce []byte

// NewNonce returns new Nonce from string.
func ( string) Nonce {
	return Nonce()
}

func ( Nonce) () string {
	return string()
}

const maxNonceB = 763

// AddTo adds NONCE to message.
func ( Nonce) ( *Message) error {
	return TextAttribute().AddToAs(, AttrNonce, maxNonceB)
}

// GetFrom gets NONCE from message.
func ( *Nonce) ( *Message) error {
	return (*TextAttribute)().GetFromAs(, AttrNonce)
}

// TextAttribute is helper for adding and getting text attributes.
type TextAttribute []byte

// AddToAs adds attribute with type t to m, checking maximum length. If maxLen
// is less than 0, no check is performed.
func ( TextAttribute) ( *Message,  AttrType,  int) error {
	if  := CheckOverflow(, len(), );  != nil {
		return 
	}
	.Add(, )
	return nil
}

// GetFromAs gets t attribute from m and appends its value to reseted v.
func ( *TextAttribute) ( *Message,  AttrType) error {
	,  := .Get()
	if  != nil {
		return 
	}
	* = 
	return nil
}