// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// GetAllStickerEmojisRequest represents TL type `getAllStickerEmojis#11ad2e30`.
type GetAllStickerEmojisRequest struct {
	// Type of the stickers to search for
	StickerType StickerTypeClass
	// Search query
	Query string
	// Chat identifier for which to find stickers
	ChatID int64
	// Pass true if only main emoji for each found sticker must be included in the result
	ReturnOnlyMainEmoji bool
}

// GetAllStickerEmojisRequestTypeID is TL type id of GetAllStickerEmojisRequest.
const GetAllStickerEmojisRequestTypeID = 0x11ad2e30

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

func (g *GetAllStickerEmojisRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.StickerType == nil) {
		return false
	}
	if !(g.Query == "") {
		return false
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.ReturnOnlyMainEmoji == false) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*GetAllStickerEmojisRequest) TypeName() string {
	return "getAllStickerEmojis"
}

// TypeInfo returns info about TL type.
func (g *GetAllStickerEmojisRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getAllStickerEmojis",
		ID:   GetAllStickerEmojisRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StickerType",
			SchemaName: "sticker_type",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "ReturnOnlyMainEmoji",
			SchemaName: "return_only_main_emoji",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetAllStickerEmojisRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getAllStickerEmojis#11ad2e30 as nil")
	}
	b.PutID(GetAllStickerEmojisRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetAllStickerEmojisRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getAllStickerEmojis#11ad2e30 as nil")
	}
	if g.StickerType == nil {
		return fmt.Errorf("unable to encode getAllStickerEmojis#11ad2e30: field sticker_type is nil")
	}
	if err := g.StickerType.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getAllStickerEmojis#11ad2e30: field sticker_type: %w", err)
	}
	b.PutString(g.Query)
	b.PutInt53(g.ChatID)
	b.PutBool(g.ReturnOnlyMainEmoji)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetAllStickerEmojisRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getAllStickerEmojis#11ad2e30 to nil")
	}
	if err := b.ConsumeID(GetAllStickerEmojisRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetAllStickerEmojisRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getAllStickerEmojis#11ad2e30 to nil")
	}
	{
		value, err := DecodeStickerType(b)
		if err != nil {
			return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field sticker_type: %w", err)
		}
		g.StickerType = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field query: %w", err)
		}
		g.Query = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field return_only_main_emoji: %w", err)
		}
		g.ReturnOnlyMainEmoji = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetAllStickerEmojisRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getAllStickerEmojis#11ad2e30 as nil")
	}
	b.ObjStart()
	b.PutID("getAllStickerEmojis")
	b.Comma()
	b.FieldStart("sticker_type")
	if g.StickerType == nil {
		return fmt.Errorf("unable to encode getAllStickerEmojis#11ad2e30: field sticker_type is nil")
	}
	if err := g.StickerType.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getAllStickerEmojis#11ad2e30: field sticker_type: %w", err)
	}
	b.Comma()
	b.FieldStart("query")
	b.PutString(g.Query)
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("return_only_main_emoji")
	b.PutBool(g.ReturnOnlyMainEmoji)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetAllStickerEmojisRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getAllStickerEmojis#11ad2e30 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getAllStickerEmojis"); err != nil {
				return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: %w", err)
			}
		case "sticker_type":
			value, err := DecodeTDLibJSONStickerType(b)
			if err != nil {
				return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field sticker_type: %w", err)
			}
			g.StickerType = value
		case "query":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field query: %w", err)
			}
			g.Query = value
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field chat_id: %w", err)
			}
			g.ChatID = value
		case "return_only_main_emoji":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getAllStickerEmojis#11ad2e30: field return_only_main_emoji: %w", err)
			}
			g.ReturnOnlyMainEmoji = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStickerType returns value of StickerType field.
func (g *GetAllStickerEmojisRequest) GetStickerType() (value StickerTypeClass) {
	if g == nil {
		return
	}
	return g.StickerType
}

// GetQuery returns value of Query field.
func (g *GetAllStickerEmojisRequest) GetQuery() (value string) {
	if g == nil {
		return
	}
	return g.Query
}

// GetChatID returns value of ChatID field.
func (g *GetAllStickerEmojisRequest) GetChatID() (value int64) {
	if g == nil {
		return
	}
	return g.ChatID
}

// GetReturnOnlyMainEmoji returns value of ReturnOnlyMainEmoji field.
func (g *GetAllStickerEmojisRequest) GetReturnOnlyMainEmoji() (value bool) {
	if g == nil {
		return
	}
	return g.ReturnOnlyMainEmoji
}

// GetAllStickerEmojis invokes method getAllStickerEmojis#11ad2e30 returning error if any.
func (c *Client) GetAllStickerEmojis(ctx context.Context, request *GetAllStickerEmojisRequest) (*Emojis, error) {
	var result Emojis

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