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

// StickersSetStickerSetThumbRequest represents TL type `stickers.setStickerSetThumb#a76a5392`.
// Set stickerset thumbnail
//
// See https://core.telegram.org/method/stickers.setStickerSetThumb for reference.
type StickersSetStickerSetThumbRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Stickerset
	Stickerset InputStickerSetClass
	// Thumbnail (only for normal stickersets, not custom emoji stickersets).
	//
	// Use SetThumb and GetThumb helpers.
	Thumb InputDocumentClass
	// Only for custom emoji stickersets¹, ID of a custom emoji present in the set to use as
	// thumbnail; pass 0 to fallback to the first custom emoji of the set.
	//
	// Links:
	//  1) https://core.telegram.org/api/custom-emoji
	//
	// Use SetThumbDocumentID and GetThumbDocumentID helpers.
	ThumbDocumentID int64
}

// StickersSetStickerSetThumbRequestTypeID is TL type id of StickersSetStickerSetThumbRequest.
const StickersSetStickerSetThumbRequestTypeID = 0xa76a5392

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

func (s *StickersSetStickerSetThumbRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Stickerset == nil) {
		return false
	}
	if !(s.Thumb == nil) {
		return false
	}
	if !(s.ThumbDocumentID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StickersSetStickerSetThumbRequest from given interface.
func (s *StickersSetStickerSetThumbRequest) FillFrom(from interface {
	GetStickerset() (value InputStickerSetClass)
	GetThumb() (value InputDocumentClass, ok bool)
	GetThumbDocumentID() (value int64, ok bool)
}) {
	s.Stickerset = from.GetStickerset()
	if val, ok := from.GetThumb(); ok {
		s.Thumb = val
	}

	if val, ok := from.GetThumbDocumentID(); ok {
		s.ThumbDocumentID = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*StickersSetStickerSetThumbRequest) TypeName() string {
	return "stickers.setStickerSetThumb"
}

// TypeInfo returns info about TL type.
func (s *StickersSetStickerSetThumbRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stickers.setStickerSetThumb",
		ID:   StickersSetStickerSetThumbRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Stickerset",
			SchemaName: "stickerset",
		},
		{
			Name:       "Thumb",
			SchemaName: "thumb",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "ThumbDocumentID",
			SchemaName: "thumb_document_id",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StickersSetStickerSetThumbRequest) SetFlags() {
	if !(s.Thumb == nil) {
		s.Flags.Set(0)
	}
	if !(s.ThumbDocumentID == 0) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *StickersSetStickerSetThumbRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stickers.setStickerSetThumb#a76a5392 as nil")
	}
	b.PutID(StickersSetStickerSetThumbRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StickersSetStickerSetThumbRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stickers.setStickerSetThumb#a76a5392 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stickers.setStickerSetThumb#a76a5392: field flags: %w", err)
	}
	if s.Stickerset == nil {
		return fmt.Errorf("unable to encode stickers.setStickerSetThumb#a76a5392: field stickerset is nil")
	}
	if err := s.Stickerset.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stickers.setStickerSetThumb#a76a5392: field stickerset: %w", err)
	}
	if s.Flags.Has(0) {
		if s.Thumb == nil {
			return fmt.Errorf("unable to encode stickers.setStickerSetThumb#a76a5392: field thumb is nil")
		}
		if err := s.Thumb.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stickers.setStickerSetThumb#a76a5392: field thumb: %w", err)
		}
	}
	if s.Flags.Has(1) {
		b.PutLong(s.ThumbDocumentID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StickersSetStickerSetThumbRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stickers.setStickerSetThumb#a76a5392 to nil")
	}
	if err := b.ConsumeID(StickersSetStickerSetThumbRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stickers.setStickerSetThumb#a76a5392: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StickersSetStickerSetThumbRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stickers.setStickerSetThumb#a76a5392 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stickers.setStickerSetThumb#a76a5392: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputStickerSet(b)
		if err != nil {
			return fmt.Errorf("unable to decode stickers.setStickerSetThumb#a76a5392: field stickerset: %w", err)
		}
		s.Stickerset = value
	}
	if s.Flags.Has(0) {
		value, err := DecodeInputDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode stickers.setStickerSetThumb#a76a5392: field thumb: %w", err)
		}
		s.Thumb = value
	}
	if s.Flags.Has(1) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode stickers.setStickerSetThumb#a76a5392: field thumb_document_id: %w", err)
		}
		s.ThumbDocumentID = value
	}
	return nil
}

// GetStickerset returns value of Stickerset field.
func (s *StickersSetStickerSetThumbRequest) GetStickerset() (value InputStickerSetClass) {
	if s == nil {
		return
	}
	return s.Stickerset
}

// SetThumb sets value of Thumb conditional field.
func (s *StickersSetStickerSetThumbRequest) SetThumb(value InputDocumentClass) {
	s.Flags.Set(0)
	s.Thumb = value
}

// GetThumb returns value of Thumb conditional field and
// boolean which is true if field was set.
func (s *StickersSetStickerSetThumbRequest) GetThumb() (value InputDocumentClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Thumb, true
}

// SetThumbDocumentID sets value of ThumbDocumentID conditional field.
func (s *StickersSetStickerSetThumbRequest) SetThumbDocumentID(value int64) {
	s.Flags.Set(1)
	s.ThumbDocumentID = value
}

// GetThumbDocumentID returns value of ThumbDocumentID conditional field and
// boolean which is true if field was set.
func (s *StickersSetStickerSetThumbRequest) GetThumbDocumentID() (value int64, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.ThumbDocumentID, true
}

// GetThumbAsNotEmpty returns mapped value of Thumb conditional field and
// boolean which is true if field was set.
func (s *StickersSetStickerSetThumbRequest) GetThumbAsNotEmpty() (*InputDocument, bool) {
	if value, ok := s.GetThumb(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}

// StickersSetStickerSetThumb invokes method stickers.setStickerSetThumb#a76a5392 returning error if any.
// Set stickerset thumbnail
//
// Possible errors:
//
//	400 STICKERSET_INVALID: The provided sticker set is invalid.
//	400 STICKER_THUMB_PNG_NOPNG: Incorrect stickerset thumb file provided, PNG / WEBP expected.
//	400 STICKER_THUMB_TGS_NOTGS: Incorrect stickerset TGS thumb file provided.
//
// See https://core.telegram.org/method/stickers.setStickerSetThumb for reference.
func (c *Client) StickersSetStickerSetThumb(ctx context.Context, request *StickersSetStickerSetThumbRequest) (MessagesStickerSetClass, error) {
	var result MessagesStickerSetBox

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