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

// SearchStickerSetRequest represents TL type `searchStickerSet#63eec702`.
type SearchStickerSetRequest struct {
	// Name of the sticker set
	Name string
	// Pass true to ignore local cache of sticker sets and always send a network request
	IgnoreCache bool
}

// SearchStickerSetRequestTypeID is TL type id of SearchStickerSetRequest.
const SearchStickerSetRequestTypeID = 0x63eec702

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

func (s *SearchStickerSetRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Name == "") {
		return false
	}
	if !(s.IgnoreCache == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchStickerSetRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchStickerSet",
		ID:   SearchStickerSetRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "IgnoreCache",
			SchemaName: "ignore_cache",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchStickerSetRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchStickerSet#63eec702 as nil")
	}
	b.PutID(SearchStickerSetRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchStickerSetRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchStickerSet#63eec702 as nil")
	}
	b.PutString(s.Name)
	b.PutBool(s.IgnoreCache)
	return nil
}

// Decode implements bin.Decoder.
func (s *SearchStickerSetRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchStickerSet#63eec702 to nil")
	}
	if err := b.ConsumeID(SearchStickerSetRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode searchStickerSet#63eec702: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SearchStickerSetRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchStickerSet#63eec702 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchStickerSet#63eec702: field name: %w", err)
		}
		s.Name = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode searchStickerSet#63eec702: field ignore_cache: %w", err)
		}
		s.IgnoreCache = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchStickerSetRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchStickerSet#63eec702 as nil")
	}
	b.ObjStart()
	b.PutID("searchStickerSet")
	b.Comma()
	b.FieldStart("name")
	b.PutString(s.Name)
	b.Comma()
	b.FieldStart("ignore_cache")
	b.PutBool(s.IgnoreCache)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchStickerSetRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchStickerSet#63eec702 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchStickerSet"); err != nil {
				return fmt.Errorf("unable to decode searchStickerSet#63eec702: %w", err)
			}
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchStickerSet#63eec702: field name: %w", err)
			}
			s.Name = value
		case "ignore_cache":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode searchStickerSet#63eec702: field ignore_cache: %w", err)
			}
			s.IgnoreCache = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetName returns value of Name field.
func (s *SearchStickerSetRequest) GetName() (value string) {
	if s == nil {
		return
	}
	return s.Name
}

// GetIgnoreCache returns value of IgnoreCache field.
func (s *SearchStickerSetRequest) GetIgnoreCache() (value bool) {
	if s == nil {
		return
	}
	return s.IgnoreCache
}

// SearchStickerSet invokes method searchStickerSet#63eec702 returning error if any.
func (c *Client) SearchStickerSet(ctx context.Context, request *SearchStickerSetRequest) (*StickerSet, error) {
	var result StickerSet

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