// 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{}
)

// UsersGetSavedMusicByIDRequest represents TL type `users.getSavedMusicByID#7573a4e9`.
// Check if the passed songs are still pinned to the user's profile, or refresh the file
// references of songs 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/method/users.getSavedMusicByID for reference.
type UsersGetSavedMusicByIDRequest struct {
	// The ID of the user.
	ID InputUserClass
	// The songs (here, file_reference can be empty to refresh file references).
	Documents []InputDocumentClass
}

// UsersGetSavedMusicByIDRequestTypeID is TL type id of UsersGetSavedMusicByIDRequest.
const UsersGetSavedMusicByIDRequestTypeID = 0x7573a4e9

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

func (g *UsersGetSavedMusicByIDRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ID == nil) {
		return false
	}
	if !(g.Documents == nil) {
		return false
	}

	return true
}

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

// FillFrom fills UsersGetSavedMusicByIDRequest from given interface.
func (g *UsersGetSavedMusicByIDRequest) FillFrom(from interface {
	GetID() (value InputUserClass)
	GetDocuments() (value []InputDocumentClass)
}) {
	g.ID = from.GetID()
	g.Documents = from.GetDocuments()
}

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

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

// TypeInfo returns info about TL type.
func (g *UsersGetSavedMusicByIDRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "users.getSavedMusicByID",
		ID:   UsersGetSavedMusicByIDRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Documents",
			SchemaName: "documents",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *UsersGetSavedMusicByIDRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode users.getSavedMusicByID#7573a4e9 as nil")
	}
	b.PutID(UsersGetSavedMusicByIDRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *UsersGetSavedMusicByIDRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode users.getSavedMusicByID#7573a4e9 as nil")
	}
	if g.ID == nil {
		return fmt.Errorf("unable to encode users.getSavedMusicByID#7573a4e9: field id is nil")
	}
	if err := g.ID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode users.getSavedMusicByID#7573a4e9: field id: %w", err)
	}
	b.PutVectorHeader(len(g.Documents))
	for idx, v := range g.Documents {
		if v == nil {
			return fmt.Errorf("unable to encode users.getSavedMusicByID#7573a4e9: field documents element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode users.getSavedMusicByID#7573a4e9: field documents element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *UsersGetSavedMusicByIDRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode users.getSavedMusicByID#7573a4e9 to nil")
	}
	if err := b.ConsumeID(UsersGetSavedMusicByIDRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode users.getSavedMusicByID#7573a4e9: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *UsersGetSavedMusicByIDRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode users.getSavedMusicByID#7573a4e9 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode users.getSavedMusicByID#7573a4e9: field id: %w", err)
		}
		g.ID = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode users.getSavedMusicByID#7573a4e9: field documents: %w", err)
		}

		if headerLen > 0 {
			g.Documents = make([]InputDocumentClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputDocument(b)
			if err != nil {
				return fmt.Errorf("unable to decode users.getSavedMusicByID#7573a4e9: field documents: %w", err)
			}
			g.Documents = append(g.Documents, value)
		}
	}
	return nil
}

// GetID returns value of ID field.
func (g *UsersGetSavedMusicByIDRequest) GetID() (value InputUserClass) {
	if g == nil {
		return
	}
	return g.ID
}

// GetDocuments returns value of Documents field.
func (g *UsersGetSavedMusicByIDRequest) GetDocuments() (value []InputDocumentClass) {
	if g == nil {
		return
	}
	return g.Documents
}

// MapDocuments returns field Documents wrapped in InputDocumentClassArray helper.
func (g *UsersGetSavedMusicByIDRequest) MapDocuments() (value InputDocumentClassArray) {
	return InputDocumentClassArray(g.Documents)
}

// UsersGetSavedMusicByID invokes method users.getSavedMusicByID#7573a4e9 returning error if any.
// Check if the passed songs are still pinned to the user's profile, or refresh the file
// references of songs pinned on a user's profile see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#music
//
// Possible errors:
//
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/users.getSavedMusicByID for reference.
func (c *Client) UsersGetSavedMusicByID(ctx context.Context, request *UsersGetSavedMusicByIDRequest) (UsersSavedMusicClass, error) {
	var result UsersSavedMusicBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.SavedMusic, nil
}
