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

// UsersGetSavedMusicRequest represents TL type `users.getSavedMusic#788d7fe3`.
// Get songs pinned to the user's profile, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#music
//
// See https://core.telegram.org/method/users.getSavedMusic for reference.
type UsersGetSavedMusicRequest struct {
	// The ID of the user.
	ID InputUserClass
	// Offset for pagination.
	Offset int
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
	// Hash »¹ of the IDs of previously added songs, to avoid returning any result if there
	// was no change.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
}

// UsersGetSavedMusicRequestTypeID is TL type id of UsersGetSavedMusicRequest.
const UsersGetSavedMusicRequestTypeID = 0x788d7fe3

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

func (g *UsersGetSavedMusicRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ID == nil) {
		return false
	}
	if !(g.Offset == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}
	if !(g.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills UsersGetSavedMusicRequest from given interface.
func (g *UsersGetSavedMusicRequest) FillFrom(from interface {
	GetID() (value InputUserClass)
	GetOffset() (value int)
	GetLimit() (value int)
	GetHash() (value int64)
}) {
	g.ID = from.GetID()
	g.Offset = from.GetOffset()
	g.Limit = from.GetLimit()
	g.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (g *UsersGetSavedMusicRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "users.getSavedMusic",
		ID:   UsersGetSavedMusicRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *UsersGetSavedMusicRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode users.getSavedMusic#788d7fe3 as nil")
	}
	b.PutID(UsersGetSavedMusicRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *UsersGetSavedMusicRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode users.getSavedMusic#788d7fe3 as nil")
	}
	if g.ID == nil {
		return fmt.Errorf("unable to encode users.getSavedMusic#788d7fe3: field id is nil")
	}
	if err := g.ID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode users.getSavedMusic#788d7fe3: field id: %w", err)
	}
	b.PutInt(g.Offset)
	b.PutInt(g.Limit)
	b.PutLong(g.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *UsersGetSavedMusicRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode users.getSavedMusic#788d7fe3 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode users.getSavedMusic#788d7fe3: field id: %w", err)
		}
		g.ID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode users.getSavedMusic#788d7fe3: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode users.getSavedMusic#788d7fe3: field limit: %w", err)
		}
		g.Limit = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode users.getSavedMusic#788d7fe3: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

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

// GetOffset returns value of Offset field.
func (g *UsersGetSavedMusicRequest) GetOffset() (value int) {
	if g == nil {
		return
	}
	return g.Offset
}

// GetLimit returns value of Limit field.
func (g *UsersGetSavedMusicRequest) GetLimit() (value int) {
	if g == nil {
		return
	}
	return g.Limit
}

// GetHash returns value of Hash field.
func (g *UsersGetSavedMusicRequest) GetHash() (value int64) {
	if g == nil {
		return
	}
	return g.Hash
}

// UsersGetSavedMusic invokes method users.getSavedMusic#788d7fe3 returning error if any.
// Get songs pinned to the 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.getSavedMusic for reference.
func (c *Client) UsersGetSavedMusic(ctx context.Context, request *UsersGetSavedMusicRequest) (UsersSavedMusicClass, error) {
	var result UsersSavedMusicBox

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