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

// MessagesGetSavedReactionTagsRequest represents TL type `messages.getSavedReactionTags#3637e05b`.
// Fetch the full list of saved message tags¹ created by the user.
//
// Links:
//  1. https://core.telegram.org/api/saved-messages#tags
//
// See https://core.telegram.org/method/messages.getSavedReactionTags for reference.
type MessagesGetSavedReactionTagsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, returns tags only used in the specified saved message dialog¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/saved-messages#saved-message-dialogs
	//
	// Use SetPeer and GetPeer helpers.
	Peer InputPeerClass
	// Hash used for caching, for more info click here¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
}

// MessagesGetSavedReactionTagsRequestTypeID is TL type id of MessagesGetSavedReactionTagsRequest.
const MessagesGetSavedReactionTagsRequestTypeID = 0x3637e05b

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

func (g *MessagesGetSavedReactionTagsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetSavedReactionTagsRequest from given interface.
func (g *MessagesGetSavedReactionTagsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass, ok bool)
	GetHash() (value int64)
}) {
	if val, ok := from.GetPeer(); ok {
		g.Peer = val
	}

	g.Hash = from.GetHash()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesGetSavedReactionTagsRequest) TypeName() string {
	return "messages.getSavedReactionTags"
}

// TypeInfo returns info about TL type.
func (g *MessagesGetSavedReactionTagsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getSavedReactionTags",
		ID:   MessagesGetSavedReactionTagsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *MessagesGetSavedReactionTagsRequest) SetFlags() {
	if !(g.Peer == nil) {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *MessagesGetSavedReactionTagsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSavedReactionTags#3637e05b as nil")
	}
	b.PutID(MessagesGetSavedReactionTagsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetSavedReactionTagsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSavedReactionTags#3637e05b as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSavedReactionTags#3637e05b: field flags: %w", err)
	}
	if g.Flags.Has(0) {
		if g.Peer == nil {
			return fmt.Errorf("unable to encode messages.getSavedReactionTags#3637e05b: field peer is nil")
		}
		if err := g.Peer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.getSavedReactionTags#3637e05b: field peer: %w", err)
		}
	}
	b.PutLong(g.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (g *MessagesGetSavedReactionTagsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSavedReactionTags#3637e05b to nil")
	}
	if err := b.ConsumeID(MessagesGetSavedReactionTagsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.getSavedReactionTags#3637e05b: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetSavedReactionTagsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSavedReactionTags#3637e05b to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getSavedReactionTags#3637e05b: field flags: %w", err)
		}
	}
	if g.Flags.Has(0) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedReactionTags#3637e05b: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedReactionTags#3637e05b: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

// SetPeer sets value of Peer conditional field.
func (g *MessagesGetSavedReactionTagsRequest) SetPeer(value InputPeerClass) {
	g.Flags.Set(0)
	g.Peer = value
}

// GetPeer returns value of Peer conditional field and
// boolean which is true if field was set.
func (g *MessagesGetSavedReactionTagsRequest) GetPeer() (value InputPeerClass, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.Peer, true
}

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

// MessagesGetSavedReactionTags invokes method messages.getSavedReactionTags#3637e05b returning error if any.
// Fetch the full list of saved message tags¹ created by the user.
//
// Links:
//  1. https://core.telegram.org/api/saved-messages#tags
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.getSavedReactionTags for reference.
func (c *Client) MessagesGetSavedReactionTags(ctx context.Context, request *MessagesGetSavedReactionTagsRequest) (MessagesSavedReactionTagsClass, error) {
	var result MessagesSavedReactionTagsBox

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