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

// StickersChangeStickerRequest represents TL type `stickers.changeSticker#f5537ebc`.
// Update the keywords, emojis or mask coordinates¹ of a sticker.
//
// Links:
//  1. https://core.telegram.org/api/stickers#mask-stickers
//
// See https://core.telegram.org/method/stickers.changeSticker for reference.
type StickersChangeStickerRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The sticker
	Sticker InputDocumentClass
	// If set, updates the emoji list associated to the sticker
	//
	// Use SetEmoji and GetEmoji helpers.
	Emoji string
	// If set, updates the mask coordinates¹
	//
	// Links:
	//  1) https://core.telegram.org/api/stickers#mask-stickers
	//
	// Use SetMaskCoords and GetMaskCoords helpers.
	MaskCoords MaskCoords
	// If set, updates the sticker keywords (separated by commas). Can't be provided for mask
	// stickers.
	//
	// Use SetKeywords and GetKeywords helpers.
	Keywords string
}

// StickersChangeStickerRequestTypeID is TL type id of StickersChangeStickerRequest.
const StickersChangeStickerRequestTypeID = 0xf5537ebc

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

func (c *StickersChangeStickerRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Sticker == nil) {
		return false
	}
	if !(c.Emoji == "") {
		return false
	}
	if !(c.MaskCoords.Zero()) {
		return false
	}
	if !(c.Keywords == "") {
		return false
	}

	return true
}

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

// FillFrom fills StickersChangeStickerRequest from given interface.
func (c *StickersChangeStickerRequest) FillFrom(from interface {
	GetSticker() (value InputDocumentClass)
	GetEmoji() (value string, ok bool)
	GetMaskCoords() (value MaskCoords, ok bool)
	GetKeywords() (value string, ok bool)
}) {
	c.Sticker = from.GetSticker()
	if val, ok := from.GetEmoji(); ok {
		c.Emoji = val
	}

	if val, ok := from.GetMaskCoords(); ok {
		c.MaskCoords = val
	}

	if val, ok := from.GetKeywords(); ok {
		c.Keywords = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (c *StickersChangeStickerRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stickers.changeSticker",
		ID:   StickersChangeStickerRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
		{
			Name:       "Emoji",
			SchemaName: "emoji",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "MaskCoords",
			SchemaName: "mask_coords",
			Null:       !c.Flags.Has(1),
		},
		{
			Name:       "Keywords",
			SchemaName: "keywords",
			Null:       !c.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *StickersChangeStickerRequest) SetFlags() {
	if !(c.Emoji == "") {
		c.Flags.Set(0)
	}
	if !(c.MaskCoords.Zero()) {
		c.Flags.Set(1)
	}
	if !(c.Keywords == "") {
		c.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (c *StickersChangeStickerRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode stickers.changeSticker#f5537ebc as nil")
	}
	b.PutID(StickersChangeStickerRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *StickersChangeStickerRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode stickers.changeSticker#f5537ebc as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stickers.changeSticker#f5537ebc: field flags: %w", err)
	}
	if c.Sticker == nil {
		return fmt.Errorf("unable to encode stickers.changeSticker#f5537ebc: field sticker is nil")
	}
	if err := c.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stickers.changeSticker#f5537ebc: field sticker: %w", err)
	}
	if c.Flags.Has(0) {
		b.PutString(c.Emoji)
	}
	if c.Flags.Has(1) {
		if err := c.MaskCoords.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stickers.changeSticker#f5537ebc: field mask_coords: %w", err)
		}
	}
	if c.Flags.Has(2) {
		b.PutString(c.Keywords)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *StickersChangeStickerRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode stickers.changeSticker#f5537ebc to nil")
	}
	if err := b.ConsumeID(StickersChangeStickerRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stickers.changeSticker#f5537ebc: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *StickersChangeStickerRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode stickers.changeSticker#f5537ebc to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stickers.changeSticker#f5537ebc: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode stickers.changeSticker#f5537ebc: field sticker: %w", err)
		}
		c.Sticker = value
	}
	if c.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stickers.changeSticker#f5537ebc: field emoji: %w", err)
		}
		c.Emoji = value
	}
	if c.Flags.Has(1) {
		if err := c.MaskCoords.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stickers.changeSticker#f5537ebc: field mask_coords: %w", err)
		}
	}
	if c.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stickers.changeSticker#f5537ebc: field keywords: %w", err)
		}
		c.Keywords = value
	}
	return nil
}

// GetSticker returns value of Sticker field.
func (c *StickersChangeStickerRequest) GetSticker() (value InputDocumentClass) {
	if c == nil {
		return
	}
	return c.Sticker
}

// SetEmoji sets value of Emoji conditional field.
func (c *StickersChangeStickerRequest) SetEmoji(value string) {
	c.Flags.Set(0)
	c.Emoji = value
}

// GetEmoji returns value of Emoji conditional field and
// boolean which is true if field was set.
func (c *StickersChangeStickerRequest) GetEmoji() (value string, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.Emoji, true
}

// SetMaskCoords sets value of MaskCoords conditional field.
func (c *StickersChangeStickerRequest) SetMaskCoords(value MaskCoords) {
	c.Flags.Set(1)
	c.MaskCoords = value
}

// GetMaskCoords returns value of MaskCoords conditional field and
// boolean which is true if field was set.
func (c *StickersChangeStickerRequest) GetMaskCoords() (value MaskCoords, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(1) {
		return value, false
	}
	return c.MaskCoords, true
}

// SetKeywords sets value of Keywords conditional field.
func (c *StickersChangeStickerRequest) SetKeywords(value string) {
	c.Flags.Set(2)
	c.Keywords = value
}

// GetKeywords returns value of Keywords conditional field and
// boolean which is true if field was set.
func (c *StickersChangeStickerRequest) GetKeywords() (value string, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(2) {
		return value, false
	}
	return c.Keywords, true
}

// GetStickerAsNotEmpty returns mapped value of Sticker field.
func (c *StickersChangeStickerRequest) GetStickerAsNotEmpty() (*InputDocument, bool) {
	return c.Sticker.AsNotEmpty()
}

// StickersChangeSticker invokes method stickers.changeSticker#f5537ebc returning error if any.
// Update the keywords, emojis or mask coordinates¹ of a sticker.
//
// Links:
//  1. https://core.telegram.org/api/stickers#mask-stickers
//
// Possible errors:
//
//	400 STICKER_INVALID: The provided sticker is invalid.
//
// See https://core.telegram.org/method/stickers.changeSticker for reference.
func (c *Client) StickersChangeSticker(ctx context.Context, request *StickersChangeStickerRequest) (MessagesStickerSetClass, error) {
	var result MessagesStickerSetBox

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