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

// MessagesGetQuickReplyMessagesRequest represents TL type `messages.getQuickReplyMessages#94a495c3`.
// Fetch (a subset or all) messages in a quick reply shortcut »¹.
//
// Links:
//  1. https://core.telegram.org/api/business#quick-reply-shortcuts
//
// See https://core.telegram.org/method/messages.getQuickReplyMessages for reference.
type MessagesGetQuickReplyMessagesRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Quick reply shortcut ID.
	ShortcutID int
	// IDs of the messages to fetch, if empty fetches all of them.
	//
	// Use SetID and GetID helpers.
	ID []int
	// Hash for pagination, generated as specified here »¹ (not the usual algorithm used
	// for hash generation).
	//
	// Links:
	//  1) https://core.telegram.org/api/business#quick-reply-shortcuts
	Hash int64
}

// MessagesGetQuickReplyMessagesRequestTypeID is TL type id of MessagesGetQuickReplyMessagesRequest.
const MessagesGetQuickReplyMessagesRequestTypeID = 0x94a495c3

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

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

	return true
}

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

// FillFrom fills MessagesGetQuickReplyMessagesRequest from given interface.
func (g *MessagesGetQuickReplyMessagesRequest) FillFrom(from interface {
	GetShortcutID() (value int)
	GetID() (value []int, ok bool)
	GetHash() (value int64)
}) {
	g.ShortcutID = from.GetShortcutID()
	if val, ok := from.GetID(); ok {
		g.ID = val
	}

	g.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetQuickReplyMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getQuickReplyMessages",
		ID:   MessagesGetQuickReplyMessagesRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ShortcutID",
			SchemaName: "shortcut_id",
		},
		{
			Name:       "ID",
			SchemaName: "id",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (g *MessagesGetQuickReplyMessagesRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getQuickReplyMessages#94a495c3 as nil")
	}
	b.PutID(MessagesGetQuickReplyMessagesRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetQuickReplyMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getQuickReplyMessages#94a495c3 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getQuickReplyMessages#94a495c3: field flags: %w", err)
	}
	b.PutInt(g.ShortcutID)
	if g.Flags.Has(0) {
		b.PutVectorHeader(len(g.ID))
		for _, v := range g.ID {
			b.PutInt(v)
		}
	}
	b.PutLong(g.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetQuickReplyMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getQuickReplyMessages#94a495c3 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getQuickReplyMessages#94a495c3: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getQuickReplyMessages#94a495c3: field shortcut_id: %w", err)
		}
		g.ShortcutID = value
	}
	if g.Flags.Has(0) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getQuickReplyMessages#94a495c3: field id: %w", err)
		}

		if headerLen > 0 {
			g.ID = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode messages.getQuickReplyMessages#94a495c3: field id: %w", err)
			}
			g.ID = append(g.ID, value)
		}
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getQuickReplyMessages#94a495c3: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

// GetShortcutID returns value of ShortcutID field.
func (g *MessagesGetQuickReplyMessagesRequest) GetShortcutID() (value int) {
	if g == nil {
		return
	}
	return g.ShortcutID
}

// SetID sets value of ID conditional field.
func (g *MessagesGetQuickReplyMessagesRequest) SetID(value []int) {
	g.Flags.Set(0)
	g.ID = value
}

// GetID returns value of ID conditional field and
// boolean which is true if field was set.
func (g *MessagesGetQuickReplyMessagesRequest) GetID() (value []int, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.ID, true
}

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

// MessagesGetQuickReplyMessages invokes method messages.getQuickReplyMessages#94a495c3 returning error if any.
// Fetch (a subset or all) messages in a quick reply shortcut »¹.
//
// Links:
//  1. https://core.telegram.org/api/business#quick-reply-shortcuts
//
// Possible errors:
//
//	400 SHORTCUT_INVALID: The specified shortcut is invalid.
//
// See https://core.telegram.org/method/messages.getQuickReplyMessages for reference.
func (c *Client) MessagesGetQuickReplyMessages(ctx context.Context, request *MessagesGetQuickReplyMessagesRequest) (MessagesMessagesClass, error) {
	var result MessagesMessagesBox

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