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

// AnimatedEmoji represents TL type `animatedEmoji#52309ebf`.
type AnimatedEmoji struct {
	// Sticker for the emoji; may be null if yet unknown for a custom emoji. If the sticker
	// is a custom emoji, then it can have arbitrary format
	Sticker Sticker
	// Expected width of the sticker, which can be used if the sticker is null
	StickerWidth int32
	// Expected height of the sticker, which can be used if the sticker is null
	StickerHeight int32
	// Emoji modifier fitzpatrick type; 0-6; 0 if none
	FitzpatrickType int32
	// File containing the sound to be played when the sticker is clicked; may be null. The
	// sound is encoded with the Opus codec, and stored inside an OGG container
	Sound File
}

// AnimatedEmojiTypeID is TL type id of AnimatedEmoji.
const AnimatedEmojiTypeID = 0x52309ebf

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

func (a *AnimatedEmoji) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Sticker.Zero()) {
		return false
	}
	if !(a.StickerWidth == 0) {
		return false
	}
	if !(a.StickerHeight == 0) {
		return false
	}
	if !(a.FitzpatrickType == 0) {
		return false
	}
	if !(a.Sound.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AnimatedEmoji) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "animatedEmoji",
		ID:   AnimatedEmojiTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
		{
			Name:       "StickerWidth",
			SchemaName: "sticker_width",
		},
		{
			Name:       "StickerHeight",
			SchemaName: "sticker_height",
		},
		{
			Name:       "FitzpatrickType",
			SchemaName: "fitzpatrick_type",
		},
		{
			Name:       "Sound",
			SchemaName: "sound",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AnimatedEmoji) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode animatedEmoji#52309ebf as nil")
	}
	b.PutID(AnimatedEmojiTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AnimatedEmoji) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode animatedEmoji#52309ebf as nil")
	}
	if err := a.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode animatedEmoji#52309ebf: field sticker: %w", err)
	}
	b.PutInt32(a.StickerWidth)
	b.PutInt32(a.StickerHeight)
	b.PutInt32(a.FitzpatrickType)
	if err := a.Sound.Encode(b); err != nil {
		return fmt.Errorf("unable to encode animatedEmoji#52309ebf: field sound: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AnimatedEmoji) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode animatedEmoji#52309ebf to nil")
	}
	if err := b.ConsumeID(AnimatedEmojiTypeID); err != nil {
		return fmt.Errorf("unable to decode animatedEmoji#52309ebf: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AnimatedEmoji) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode animatedEmoji#52309ebf to nil")
	}
	{
		if err := a.Sticker.Decode(b); err != nil {
			return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sticker: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sticker_width: %w", err)
		}
		a.StickerWidth = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sticker_height: %w", err)
		}
		a.StickerHeight = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field fitzpatrick_type: %w", err)
		}
		a.FitzpatrickType = value
	}
	{
		if err := a.Sound.Decode(b); err != nil {
			return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sound: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AnimatedEmoji) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode animatedEmoji#52309ebf as nil")
	}
	b.ObjStart()
	b.PutID("animatedEmoji")
	b.Comma()
	b.FieldStart("sticker")
	if err := a.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode animatedEmoji#52309ebf: field sticker: %w", err)
	}
	b.Comma()
	b.FieldStart("sticker_width")
	b.PutInt32(a.StickerWidth)
	b.Comma()
	b.FieldStart("sticker_height")
	b.PutInt32(a.StickerHeight)
	b.Comma()
	b.FieldStart("fitzpatrick_type")
	b.PutInt32(a.FitzpatrickType)
	b.Comma()
	b.FieldStart("sound")
	if err := a.Sound.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode animatedEmoji#52309ebf: field sound: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AnimatedEmoji) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode animatedEmoji#52309ebf to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("animatedEmoji"); err != nil {
				return fmt.Errorf("unable to decode animatedEmoji#52309ebf: %w", err)
			}
		case "sticker":
			if err := a.Sticker.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sticker: %w", err)
			}
		case "sticker_width":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sticker_width: %w", err)
			}
			a.StickerWidth = value
		case "sticker_height":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sticker_height: %w", err)
			}
			a.StickerHeight = value
		case "fitzpatrick_type":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field fitzpatrick_type: %w", err)
			}
			a.FitzpatrickType = value
		case "sound":
			if err := a.Sound.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode animatedEmoji#52309ebf: field sound: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSticker returns value of Sticker field.
func (a *AnimatedEmoji) GetSticker() (value Sticker) {
	if a == nil {
		return
	}
	return a.Sticker
}

// GetStickerWidth returns value of StickerWidth field.
func (a *AnimatedEmoji) GetStickerWidth() (value int32) {
	if a == nil {
		return
	}
	return a.StickerWidth
}

// GetStickerHeight returns value of StickerHeight field.
func (a *AnimatedEmoji) GetStickerHeight() (value int32) {
	if a == nil {
		return
	}
	return a.StickerHeight
}

// GetFitzpatrickType returns value of FitzpatrickType field.
func (a *AnimatedEmoji) GetFitzpatrickType() (value int32) {
	if a == nil {
		return
	}
	return a.FitzpatrickType
}

// GetSound returns value of Sound field.
func (a *AnimatedEmoji) GetSound() (value File) {
	if a == nil {
		return
	}
	return a.Sound
}
