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

// InputSticker represents TL type `inputSticker#1f1d0998`.
type InputSticker struct {
	// File with the sticker; must fit in a 512x512 square. For WEBP stickers the file must
	// be in WEBP or PNG format, which will be converted to WEBP server-side.
	Sticker InputFileClass
	// Format of the sticker
	Format StickerFormatClass
	// String with 1-20 emoji corresponding to the sticker
	Emojis string
	// Position where the mask is placed; pass null if not specified
	MaskPosition MaskPosition
	// List of up to 20 keywords with total length up to 64 characters, which can be used to
	// find the sticker
	Keywords []string
}

// InputStickerTypeID is TL type id of InputSticker.
const InputStickerTypeID = 0x1f1d0998

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

func (i *InputSticker) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Sticker == nil) {
		return false
	}
	if !(i.Format == nil) {
		return false
	}
	if !(i.Emojis == "") {
		return false
	}
	if !(i.MaskPosition.Zero()) {
		return false
	}
	if !(i.Keywords == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputSticker) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputSticker",
		ID:   InputStickerTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
		{
			Name:       "Format",
			SchemaName: "format",
		},
		{
			Name:       "Emojis",
			SchemaName: "emojis",
		},
		{
			Name:       "MaskPosition",
			SchemaName: "mask_position",
		},
		{
			Name:       "Keywords",
			SchemaName: "keywords",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputSticker) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputSticker#1f1d0998 as nil")
	}
	b.PutID(InputStickerTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputSticker) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputSticker#1f1d0998 as nil")
	}
	if i.Sticker == nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field sticker is nil")
	}
	if err := i.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field sticker: %w", err)
	}
	if i.Format == nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field format is nil")
	}
	if err := i.Format.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field format: %w", err)
	}
	b.PutString(i.Emojis)
	if err := i.MaskPosition.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field mask_position: %w", err)
	}
	b.PutInt(len(i.Keywords))
	for _, v := range i.Keywords {
		b.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputSticker) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputSticker#1f1d0998 to nil")
	}
	if err := b.ConsumeID(InputStickerTypeID); err != nil {
		return fmt.Errorf("unable to decode inputSticker#1f1d0998: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputSticker) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputSticker#1f1d0998 to nil")
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputSticker#1f1d0998: field sticker: %w", err)
		}
		i.Sticker = value
	}
	{
		value, err := DecodeStickerFormat(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputSticker#1f1d0998: field format: %w", err)
		}
		i.Format = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputSticker#1f1d0998: field emojis: %w", err)
		}
		i.Emojis = value
	}
	{
		if err := i.MaskPosition.Decode(b); err != nil {
			return fmt.Errorf("unable to decode inputSticker#1f1d0998: field mask_position: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode inputSticker#1f1d0998: field keywords: %w", err)
		}

		if headerLen > 0 {
			i.Keywords = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: field keywords: %w", err)
			}
			i.Keywords = append(i.Keywords, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputSticker) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputSticker#1f1d0998 as nil")
	}
	b.ObjStart()
	b.PutID("inputSticker")
	b.Comma()
	b.FieldStart("sticker")
	if i.Sticker == nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field sticker is nil")
	}
	if err := i.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field sticker: %w", err)
	}
	b.Comma()
	b.FieldStart("format")
	if i.Format == nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field format is nil")
	}
	if err := i.Format.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field format: %w", err)
	}
	b.Comma()
	b.FieldStart("emojis")
	b.PutString(i.Emojis)
	b.Comma()
	b.FieldStart("mask_position")
	if err := i.MaskPosition.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputSticker#1f1d0998: field mask_position: %w", err)
	}
	b.Comma()
	b.FieldStart("keywords")
	b.ArrStart()
	for _, v := range i.Keywords {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputSticker) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputSticker#1f1d0998 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputSticker"); err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: %w", err)
			}
		case "sticker":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: field sticker: %w", err)
			}
			i.Sticker = value
		case "format":
			value, err := DecodeTDLibJSONStickerFormat(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: field format: %w", err)
			}
			i.Format = value
		case "emojis":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: field emojis: %w", err)
			}
			i.Emojis = value
		case "mask_position":
			if err := i.MaskPosition.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: field mask_position: %w", err)
			}
		case "keywords":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode inputSticker#1f1d0998: field keywords: %w", err)
				}
				i.Keywords = append(i.Keywords, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode inputSticker#1f1d0998: field keywords: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSticker returns value of Sticker field.
func (i *InputSticker) GetSticker() (value InputFileClass) {
	if i == nil {
		return
	}
	return i.Sticker
}

// GetFormat returns value of Format field.
func (i *InputSticker) GetFormat() (value StickerFormatClass) {
	if i == nil {
		return
	}
	return i.Format
}

// GetEmojis returns value of Emojis field.
func (i *InputSticker) GetEmojis() (value string) {
	if i == nil {
		return
	}
	return i.Emojis
}

// GetMaskPosition returns value of MaskPosition field.
func (i *InputSticker) GetMaskPosition() (value MaskPosition) {
	if i == nil {
		return
	}
	return i.MaskPosition
}

// GetKeywords returns value of Keywords field.
func (i *InputSticker) GetKeywords() (value []string) {
	if i == nil {
		return
	}
	return i.Keywords
}
