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

// Animations represents TL type `animations#2ce4157c`.
type Animations struct {
	// List of animations
	Animations []Animation
}

// AnimationsTypeID is TL type id of Animations.
const AnimationsTypeID = 0x2ce4157c

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *Animations) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "animations",
		ID:   AnimationsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Animations",
			SchemaName: "animations",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *Animations) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode animations#2ce4157c as nil")
	}
	b.PutID(AnimationsTypeID)
	return a.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (a *Animations) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode animations#2ce4157c to nil")
	}
	if err := b.ConsumeID(AnimationsTypeID); err != nil {
		return fmt.Errorf("unable to decode animations#2ce4157c: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *Animations) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode animations#2ce4157c to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode animations#2ce4157c: field animations: %w", err)
		}

		if headerLen > 0 {
			a.Animations = make([]Animation, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Animation
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare animations#2ce4157c: field animations: %w", err)
			}
			a.Animations = append(a.Animations, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *Animations) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode animations#2ce4157c as nil")
	}
	b.ObjStart()
	b.PutID("animations")
	b.Comma()
	b.FieldStart("animations")
	b.ArrStart()
	for idx, v := range a.Animations {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode animations#2ce4157c: field animations 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 (a *Animations) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode animations#2ce4157c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("animations"); err != nil {
				return fmt.Errorf("unable to decode animations#2ce4157c: %w", err)
			}
		case "animations":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value Animation
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode animations#2ce4157c: field animations: %w", err)
				}
				a.Animations = append(a.Animations, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode animations#2ce4157c: field animations: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetAnimations returns value of Animations field.
func (a *Animations) GetAnimations() (value []Animation) {
	if a == nil {
		return
	}
	return a.Animations
}
