// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// UsersSavedMusicNotModified represents TL type `users.savedMusicNotModified#e3878aa4`.
// This subset of the songs currently pinned on a user's profile hasn't changed, see here
// »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#music
//
// See https://core.telegram.org/constructor/users.savedMusicNotModified for reference.
type UsersSavedMusicNotModified struct {
	// Total number of songs on the user's profile.
	Count int
}

// UsersSavedMusicNotModifiedTypeID is TL type id of UsersSavedMusicNotModified.
const UsersSavedMusicNotModifiedTypeID = 0xe3878aa4

// construct implements constructor of UsersSavedMusicClass.
func (s UsersSavedMusicNotModified) construct() UsersSavedMusicClass { return &s }

// Ensuring interfaces in compile-time for UsersSavedMusicNotModified.
var (
	_ bin.Encoder     = &UsersSavedMusicNotModified{}
	_ bin.Decoder     = &UsersSavedMusicNotModified{}
	_ bin.BareEncoder = &UsersSavedMusicNotModified{}
	_ bin.BareDecoder = &UsersSavedMusicNotModified{}

	_ UsersSavedMusicClass = &UsersSavedMusicNotModified{}
)

func (s *UsersSavedMusicNotModified) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Count == 0) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (s *UsersSavedMusicNotModified) String() string {
	if s == nil {
		return "UsersSavedMusicNotModified(nil)"
	}
	type Alias UsersSavedMusicNotModified
	return fmt.Sprintf("UsersSavedMusicNotModified%+v", Alias(*s))
}

// FillFrom fills UsersSavedMusicNotModified from given interface.
func (s *UsersSavedMusicNotModified) FillFrom(from interface {
	GetCount() (value int)
}) {
	s.Count = from.GetCount()
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*UsersSavedMusicNotModified) TypeID() uint32 {
	return UsersSavedMusicNotModifiedTypeID
}

// TypeName returns name of type in TL schema.
func (*UsersSavedMusicNotModified) TypeName() string {
	return "users.savedMusicNotModified"
}

// TypeInfo returns info about TL type.
func (s *UsersSavedMusicNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "users.savedMusicNotModified",
		ID:   UsersSavedMusicNotModifiedTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *UsersSavedMusicNotModified) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode users.savedMusicNotModified#e3878aa4 as nil")
	}
	b.PutID(UsersSavedMusicNotModifiedTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *UsersSavedMusicNotModified) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode users.savedMusicNotModified#e3878aa4 as nil")
	}
	b.PutInt(s.Count)
	return nil
}

// Decode implements bin.Decoder.
func (s *UsersSavedMusicNotModified) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode users.savedMusicNotModified#e3878aa4 to nil")
	}
	if err := b.ConsumeID(UsersSavedMusicNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode users.savedMusicNotModified#e3878aa4: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *UsersSavedMusicNotModified) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode users.savedMusicNotModified#e3878aa4 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode users.savedMusicNotModified#e3878aa4: field count: %w", err)
		}
		s.Count = value
	}
	return nil
}

// GetCount returns value of Count field.
func (s *UsersSavedMusicNotModified) GetCount() (value int) {
	if s == nil {
		return
	}
	return s.Count
}

// UsersSavedMusic represents TL type `users.savedMusic#34a2f297`.
// List of songs currently pinned on a user's profile, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#music
//
// See https://core.telegram.org/constructor/users.savedMusic for reference.
type UsersSavedMusic struct {
	// Total number of songs (can be bigger than documents depending on the passed limit, and
	// the default maximum limit in which case pagination is required).
	Count int
	// Songs.
	Documents []DocumentClass
}

// UsersSavedMusicTypeID is TL type id of UsersSavedMusic.
const UsersSavedMusicTypeID = 0x34a2f297

// construct implements constructor of UsersSavedMusicClass.
func (s UsersSavedMusic) construct() UsersSavedMusicClass { return &s }

// Ensuring interfaces in compile-time for UsersSavedMusic.
var (
	_ bin.Encoder     = &UsersSavedMusic{}
	_ bin.Decoder     = &UsersSavedMusic{}
	_ bin.BareEncoder = &UsersSavedMusic{}
	_ bin.BareDecoder = &UsersSavedMusic{}

	_ UsersSavedMusicClass = &UsersSavedMusic{}
)

func (s *UsersSavedMusic) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.Documents == nil) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (s *UsersSavedMusic) String() string {
	if s == nil {
		return "UsersSavedMusic(nil)"
	}
	type Alias UsersSavedMusic
	return fmt.Sprintf("UsersSavedMusic%+v", Alias(*s))
}

// FillFrom fills UsersSavedMusic from given interface.
func (s *UsersSavedMusic) FillFrom(from interface {
	GetCount() (value int)
	GetDocuments() (value []DocumentClass)
}) {
	s.Count = from.GetCount()
	s.Documents = from.GetDocuments()
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*UsersSavedMusic) TypeID() uint32 {
	return UsersSavedMusicTypeID
}

// TypeName returns name of type in TL schema.
func (*UsersSavedMusic) TypeName() string {
	return "users.savedMusic"
}

// TypeInfo returns info about TL type.
func (s *UsersSavedMusic) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "users.savedMusic",
		ID:   UsersSavedMusicTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Documents",
			SchemaName: "documents",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *UsersSavedMusic) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode users.savedMusic#34a2f297 as nil")
	}
	b.PutID(UsersSavedMusicTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *UsersSavedMusic) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode users.savedMusic#34a2f297 as nil")
	}
	b.PutInt(s.Count)
	b.PutVectorHeader(len(s.Documents))
	for idx, v := range s.Documents {
		if v == nil {
			return fmt.Errorf("unable to encode users.savedMusic#34a2f297: field documents element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode users.savedMusic#34a2f297: field documents element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *UsersSavedMusic) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode users.savedMusic#34a2f297 to nil")
	}
	if err := b.ConsumeID(UsersSavedMusicTypeID); err != nil {
		return fmt.Errorf("unable to decode users.savedMusic#34a2f297: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *UsersSavedMusic) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode users.savedMusic#34a2f297 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode users.savedMusic#34a2f297: field count: %w", err)
		}
		s.Count = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode users.savedMusic#34a2f297: field documents: %w", err)
		}

		if headerLen > 0 {
			s.Documents = make([]DocumentClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeDocument(b)
			if err != nil {
				return fmt.Errorf("unable to decode users.savedMusic#34a2f297: field documents: %w", err)
			}
			s.Documents = append(s.Documents, value)
		}
	}
	return nil
}

// GetCount returns value of Count field.
func (s *UsersSavedMusic) GetCount() (value int) {
	if s == nil {
		return
	}
	return s.Count
}

// GetDocuments returns value of Documents field.
func (s *UsersSavedMusic) GetDocuments() (value []DocumentClass) {
	if s == nil {
		return
	}
	return s.Documents
}

// MapDocuments returns field Documents wrapped in DocumentClassArray helper.
func (s *UsersSavedMusic) MapDocuments() (value DocumentClassArray) {
	return DocumentClassArray(s.Documents)
}

// UsersSavedMusicClassName is schema name of UsersSavedMusicClass.
const UsersSavedMusicClassName = "users.SavedMusic"

// UsersSavedMusicClass represents users.SavedMusic generic type.
//
// See https://core.telegram.org/type/users.SavedMusic for reference.
//
// Example:
//
//	g, err := tg.DecodeUsersSavedMusic(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.UsersSavedMusicNotModified: // users.savedMusicNotModified#e3878aa4
//	case *tg.UsersSavedMusic: // users.savedMusic#34a2f297
//	default: panic(v)
//	}
type UsersSavedMusicClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() UsersSavedMusicClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// Total number of songs on the user's profile.
	GetCount() (value int)

	// AsModified tries to map UsersSavedMusicClass to UsersSavedMusic.
	AsModified() (*UsersSavedMusic, bool)
}

// AsModified tries to map UsersSavedMusicNotModified to UsersSavedMusic.
func (s *UsersSavedMusicNotModified) AsModified() (*UsersSavedMusic, bool) {
	return nil, false
}

// AsModified tries to map UsersSavedMusic to UsersSavedMusic.
func (s *UsersSavedMusic) AsModified() (*UsersSavedMusic, bool) {
	return s, true
}

// DecodeUsersSavedMusic implements binary de-serialization for UsersSavedMusicClass.
func DecodeUsersSavedMusic(buf *bin.Buffer) (UsersSavedMusicClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case UsersSavedMusicNotModifiedTypeID:
		// Decoding users.savedMusicNotModified#e3878aa4.
		v := UsersSavedMusicNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode UsersSavedMusicClass: %w", err)
		}
		return &v, nil
	case UsersSavedMusicTypeID:
		// Decoding users.savedMusic#34a2f297.
		v := UsersSavedMusic{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode UsersSavedMusicClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode UsersSavedMusicClass: %w", bin.NewUnexpectedID(id))
	}
}

// UsersSavedMusic boxes the UsersSavedMusicClass providing a helper.
type UsersSavedMusicBox struct {
	SavedMusic UsersSavedMusicClass
}

// Decode implements bin.Decoder for UsersSavedMusicBox.
func (b *UsersSavedMusicBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode UsersSavedMusicBox to nil")
	}
	v, err := DecodeUsersSavedMusic(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.SavedMusic = v
	return nil
}

// Encode implements bin.Encode for UsersSavedMusicBox.
func (b *UsersSavedMusicBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.SavedMusic == nil {
		return fmt.Errorf("unable to encode UsersSavedMusicClass as nil")
	}
	return b.SavedMusic.Encode(buf)
}
