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

// EmojiStatuses represents TL type `emojiStatuses#45127a70`.
type EmojiStatuses struct {
	// The list of emoji statuses identifiers
	EmojiStatuses []EmojiStatus
}

// EmojiStatusesTypeID is TL type id of EmojiStatuses.
const EmojiStatusesTypeID = 0x45127a70

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

func (e *EmojiStatuses) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.EmojiStatuses == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmojiStatuses) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emojiStatuses",
		ID:   EmojiStatusesTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "EmojiStatuses",
			SchemaName: "emoji_statuses",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmojiStatuses) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiStatuses#45127a70 as nil")
	}
	b.PutID(EmojiStatusesTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmojiStatuses) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiStatuses#45127a70 as nil")
	}
	b.PutInt(len(e.EmojiStatuses))
	for idx, v := range e.EmojiStatuses {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare emojiStatuses#45127a70: field emoji_statuses element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *EmojiStatuses) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiStatuses#45127a70 to nil")
	}
	if err := b.ConsumeID(EmojiStatusesTypeID); err != nil {
		return fmt.Errorf("unable to decode emojiStatuses#45127a70: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmojiStatuses) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiStatuses#45127a70 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode emojiStatuses#45127a70: field emoji_statuses: %w", err)
		}

		if headerLen > 0 {
			e.EmojiStatuses = make([]EmojiStatus, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value EmojiStatus
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare emojiStatuses#45127a70: field emoji_statuses: %w", err)
			}
			e.EmojiStatuses = append(e.EmojiStatuses, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmojiStatuses) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiStatuses#45127a70 as nil")
	}
	b.ObjStart()
	b.PutID("emojiStatuses")
	b.Comma()
	b.FieldStart("emoji_statuses")
	b.ArrStart()
	for idx, v := range e.EmojiStatuses {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode emojiStatuses#45127a70: field emoji_statuses element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmojiStatuses) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiStatuses#45127a70 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emojiStatuses"); err != nil {
				return fmt.Errorf("unable to decode emojiStatuses#45127a70: %w", err)
			}
		case "emoji_statuses":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value EmojiStatus
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode emojiStatuses#45127a70: field emoji_statuses: %w", err)
				}
				e.EmojiStatuses = append(e.EmojiStatuses, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode emojiStatuses#45127a70: field emoji_statuses: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetEmojiStatuses returns value of EmojiStatuses field.
func (e *EmojiStatuses) GetEmojiStatuses() (value []EmojiStatus) {
	if e == nil {
		return
	}
	return e.EmojiStatuses
}
