// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesSearchStickerSetsRequest represents TL type `messages.searchStickerSets#35705b8a`.
// Search for stickersets
//
// See https://core.telegram.org/method/messages.searchStickerSets for reference.
type MessagesSearchStickerSetsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Exclude featured stickersets from results
	ExcludeFeatured bool
	// Query string
	Q string
	// Hash for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
}

// MessagesSearchStickerSetsRequestTypeID is TL type id of MessagesSearchStickerSetsRequest.
const MessagesSearchStickerSetsRequestTypeID = 0x35705b8a

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

func (s *MessagesSearchStickerSetsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.ExcludeFeatured == false) {
		return false
	}
	if !(s.Q == "") {
		return false
	}
	if !(s.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSearchStickerSetsRequest from given interface.
func (s *MessagesSearchStickerSetsRequest) FillFrom(from interface {
	GetExcludeFeatured() (value bool)
	GetQ() (value string)
	GetHash() (value int64)
}) {
	s.ExcludeFeatured = from.GetExcludeFeatured()
	s.Q = from.GetQ()
	s.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesSearchStickerSetsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.searchStickerSets",
		ID:   MessagesSearchStickerSetsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ExcludeFeatured",
			SchemaName: "exclude_featured",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Q",
			SchemaName: "q",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSearchStickerSetsRequest) SetFlags() {
	if !(s.ExcludeFeatured == false) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSearchStickerSetsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.searchStickerSets#35705b8a as nil")
	}
	b.PutID(MessagesSearchStickerSetsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSearchStickerSetsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.searchStickerSets#35705b8a as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.searchStickerSets#35705b8a: field flags: %w", err)
	}
	b.PutString(s.Q)
	b.PutLong(s.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSearchStickerSetsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.searchStickerSets#35705b8a to nil")
	}
	if err := b.ConsumeID(MessagesSearchStickerSetsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.searchStickerSets#35705b8a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSearchStickerSetsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.searchStickerSets#35705b8a to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.searchStickerSets#35705b8a: field flags: %w", err)
		}
	}
	s.ExcludeFeatured = s.Flags.Has(0)
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchStickerSets#35705b8a: field q: %w", err)
		}
		s.Q = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchStickerSets#35705b8a: field hash: %w", err)
		}
		s.Hash = value
	}
	return nil
}

// SetExcludeFeatured sets value of ExcludeFeatured conditional field.
func (s *MessagesSearchStickerSetsRequest) SetExcludeFeatured(value bool) {
	if value {
		s.Flags.Set(0)
		s.ExcludeFeatured = true
	} else {
		s.Flags.Unset(0)
		s.ExcludeFeatured = false
	}
}

// GetExcludeFeatured returns value of ExcludeFeatured conditional field.
func (s *MessagesSearchStickerSetsRequest) GetExcludeFeatured() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetQ returns value of Q field.
func (s *MessagesSearchStickerSetsRequest) GetQ() (value string) {
	if s == nil {
		return
	}
	return s.Q
}

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

// MessagesSearchStickerSets invokes method messages.searchStickerSets#35705b8a returning error if any.
// Search for stickersets
//
// See https://core.telegram.org/method/messages.searchStickerSets for reference.
func (c *Client) MessagesSearchStickerSets(ctx context.Context, request *MessagesSearchStickerSetsRequest) (MessagesFoundStickerSetsClass, error) {
	var result MessagesFoundStickerSetsBox

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