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

// EmojiCategory represents TL type `emojiCategory#220de4ef`.
type EmojiCategory struct {
	// Name of the category
	Name string
	// Custom emoji sticker, which represents icon of the category
	Icon Sticker
	// Source of stickers for the emoji category
	Source EmojiCategorySourceClass
	// True, if the category must be shown first when choosing a sticker for the start page
	IsGreeting bool
}

// EmojiCategoryTypeID is TL type id of EmojiCategory.
const EmojiCategoryTypeID = 0x220de4ef

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

func (e *EmojiCategory) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.Name == "") {
		return false
	}
	if !(e.Icon.Zero()) {
		return false
	}
	if !(e.Source == nil) {
		return false
	}
	if !(e.IsGreeting == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmojiCategory) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emojiCategory",
		ID:   EmojiCategoryTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Icon",
			SchemaName: "icon",
		},
		{
			Name:       "Source",
			SchemaName: "source",
		},
		{
			Name:       "IsGreeting",
			SchemaName: "is_greeting",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmojiCategory) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategory#220de4ef as nil")
	}
	b.PutID(EmojiCategoryTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmojiCategory) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategory#220de4ef as nil")
	}
	b.PutString(e.Name)
	if err := e.Icon.Encode(b); err != nil {
		return fmt.Errorf("unable to encode emojiCategory#220de4ef: field icon: %w", err)
	}
	if e.Source == nil {
		return fmt.Errorf("unable to encode emojiCategory#220de4ef: field source is nil")
	}
	if err := e.Source.Encode(b); err != nil {
		return fmt.Errorf("unable to encode emojiCategory#220de4ef: field source: %w", err)
	}
	b.PutBool(e.IsGreeting)
	return nil
}

// Decode implements bin.Decoder.
func (e *EmojiCategory) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategory#220de4ef to nil")
	}
	if err := b.ConsumeID(EmojiCategoryTypeID); err != nil {
		return fmt.Errorf("unable to decode emojiCategory#220de4ef: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmojiCategory) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategory#220de4ef to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode emojiCategory#220de4ef: field name: %w", err)
		}
		e.Name = value
	}
	{
		if err := e.Icon.Decode(b); err != nil {
			return fmt.Errorf("unable to decode emojiCategory#220de4ef: field icon: %w", err)
		}
	}
	{
		value, err := DecodeEmojiCategorySource(b)
		if err != nil {
			return fmt.Errorf("unable to decode emojiCategory#220de4ef: field source: %w", err)
		}
		e.Source = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode emojiCategory#220de4ef: field is_greeting: %w", err)
		}
		e.IsGreeting = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmojiCategory) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategory#220de4ef as nil")
	}
	b.ObjStart()
	b.PutID("emojiCategory")
	b.Comma()
	b.FieldStart("name")
	b.PutString(e.Name)
	b.Comma()
	b.FieldStart("icon")
	if err := e.Icon.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode emojiCategory#220de4ef: field icon: %w", err)
	}
	b.Comma()
	b.FieldStart("source")
	if e.Source == nil {
		return fmt.Errorf("unable to encode emojiCategory#220de4ef: field source is nil")
	}
	if err := e.Source.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode emojiCategory#220de4ef: field source: %w", err)
	}
	b.Comma()
	b.FieldStart("is_greeting")
	b.PutBool(e.IsGreeting)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmojiCategory) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategory#220de4ef to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emojiCategory"); err != nil {
				return fmt.Errorf("unable to decode emojiCategory#220de4ef: %w", err)
			}
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode emojiCategory#220de4ef: field name: %w", err)
			}
			e.Name = value
		case "icon":
			if err := e.Icon.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode emojiCategory#220de4ef: field icon: %w", err)
			}
		case "source":
			value, err := DecodeTDLibJSONEmojiCategorySource(b)
			if err != nil {
				return fmt.Errorf("unable to decode emojiCategory#220de4ef: field source: %w", err)
			}
			e.Source = value
		case "is_greeting":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode emojiCategory#220de4ef: field is_greeting: %w", err)
			}
			e.IsGreeting = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetName returns value of Name field.
func (e *EmojiCategory) GetName() (value string) {
	if e == nil {
		return
	}
	return e.Name
}

// GetIcon returns value of Icon field.
func (e *EmojiCategory) GetIcon() (value Sticker) {
	if e == nil {
		return
	}
	return e.Icon
}

// GetSource returns value of Source field.
func (e *EmojiCategory) GetSource() (value EmojiCategorySourceClass) {
	if e == nil {
		return
	}
	return e.Source
}

// GetIsGreeting returns value of IsGreeting field.
func (e *EmojiCategory) GetIsGreeting() (value bool) {
	if e == nil {
		return
	}
	return e.IsGreeting
}
