// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesAvailableEffectsNotModified represents TL type `messages.availableEffectsNotModified#d1ed9a5b`.
// The full list of usable animated message effects »¹ hasn't changed.
//
// Links:
//  1. https://core.telegram.org/api/effects
//
// See https://core.telegram.org/constructor/messages.availableEffectsNotModified for reference.
type MessagesAvailableEffectsNotModified struct {
}

// MessagesAvailableEffectsNotModifiedTypeID is TL type id of MessagesAvailableEffectsNotModified.
const MessagesAvailableEffectsNotModifiedTypeID = 0xd1ed9a5b

// construct implements constructor of MessagesAvailableEffectsClass.
func (a MessagesAvailableEffectsNotModified) construct() MessagesAvailableEffectsClass { return &a }

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

	_ MessagesAvailableEffectsClass = &MessagesAvailableEffectsNotModified{}
)

func (a *MessagesAvailableEffectsNotModified) Zero() bool {
	if a == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*MessagesAvailableEffectsNotModified) TypeName() string {
	return "messages.availableEffectsNotModified"
}

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

// Encode implements bin.Encoder.
func (a *MessagesAvailableEffectsNotModified) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.availableEffectsNotModified#d1ed9a5b as nil")
	}
	b.PutID(MessagesAvailableEffectsNotModifiedTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *MessagesAvailableEffectsNotModified) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.availableEffectsNotModified#d1ed9a5b as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *MessagesAvailableEffectsNotModified) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.availableEffectsNotModified#d1ed9a5b to nil")
	}
	if err := b.ConsumeID(MessagesAvailableEffectsNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.availableEffectsNotModified#d1ed9a5b: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *MessagesAvailableEffectsNotModified) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.availableEffectsNotModified#d1ed9a5b to nil")
	}
	return nil
}

// MessagesAvailableEffects represents TL type `messages.availableEffects#bddb616e`.
// The full list of usable animated message effects »¹.
//
// Links:
//  1. https://core.telegram.org/api/effects
//
// See https://core.telegram.org/constructor/messages.availableEffects for reference.
type MessagesAvailableEffects struct {
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int
	// Message effects
	Effects []AvailableEffect
	// Documents specified in the effects constructors.
	Documents []DocumentClass
}

// MessagesAvailableEffectsTypeID is TL type id of MessagesAvailableEffects.
const MessagesAvailableEffectsTypeID = 0xbddb616e

// construct implements constructor of MessagesAvailableEffectsClass.
func (a MessagesAvailableEffects) construct() MessagesAvailableEffectsClass { return &a }

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

	_ MessagesAvailableEffectsClass = &MessagesAvailableEffects{}
)

func (a *MessagesAvailableEffects) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Hash == 0) {
		return false
	}
	if !(a.Effects == nil) {
		return false
	}
	if !(a.Documents == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesAvailableEffects from given interface.
func (a *MessagesAvailableEffects) FillFrom(from interface {
	GetHash() (value int)
	GetEffects() (value []AvailableEffect)
	GetDocuments() (value []DocumentClass)
}) {
	a.Hash = from.GetHash()
	a.Effects = from.GetEffects()
	a.Documents = from.GetDocuments()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesAvailableEffects) TypeName() string {
	return "messages.availableEffects"
}

// TypeInfo returns info about TL type.
func (a *MessagesAvailableEffects) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.availableEffects",
		ID:   MessagesAvailableEffectsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
		{
			Name:       "Effects",
			SchemaName: "effects",
		},
		{
			Name:       "Documents",
			SchemaName: "documents",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *MessagesAvailableEffects) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.availableEffects#bddb616e as nil")
	}
	b.PutID(MessagesAvailableEffectsTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *MessagesAvailableEffects) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.availableEffects#bddb616e as nil")
	}
	b.PutInt(a.Hash)
	b.PutVectorHeader(len(a.Effects))
	for idx, v := range a.Effects {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.availableEffects#bddb616e: field effects element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(a.Documents))
	for idx, v := range a.Documents {
		if v == nil {
			return fmt.Errorf("unable to encode messages.availableEffects#bddb616e: field documents element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.availableEffects#bddb616e: field documents element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *MessagesAvailableEffects) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.availableEffects#bddb616e to nil")
	}
	if err := b.ConsumeID(MessagesAvailableEffectsTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.availableEffects#bddb616e: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *MessagesAvailableEffects) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.availableEffects#bddb616e to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.availableEffects#bddb616e: field hash: %w", err)
		}
		a.Hash = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.availableEffects#bddb616e: field effects: %w", err)
		}

		if headerLen > 0 {
			a.Effects = make([]AvailableEffect, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AvailableEffect
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode messages.availableEffects#bddb616e: field effects: %w", err)
			}
			a.Effects = append(a.Effects, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.availableEffects#bddb616e: field documents: %w", err)
		}

		if headerLen > 0 {
			a.Documents = make([]DocumentClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeDocument(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.availableEffects#bddb616e: field documents: %w", err)
			}
			a.Documents = append(a.Documents, value)
		}
	}
	return nil
}

// GetHash returns value of Hash field.
func (a *MessagesAvailableEffects) GetHash() (value int) {
	if a == nil {
		return
	}
	return a.Hash
}

// GetEffects returns value of Effects field.
func (a *MessagesAvailableEffects) GetEffects() (value []AvailableEffect) {
	if a == nil {
		return
	}
	return a.Effects
}

// GetDocuments returns value of Documents field.
func (a *MessagesAvailableEffects) GetDocuments() (value []DocumentClass) {
	if a == nil {
		return
	}
	return a.Documents
}

// MapDocuments returns field Documents wrapped in DocumentClassArray helper.
func (a *MessagesAvailableEffects) MapDocuments() (value DocumentClassArray) {
	return DocumentClassArray(a.Documents)
}

// MessagesAvailableEffectsClassName is schema name of MessagesAvailableEffectsClass.
const MessagesAvailableEffectsClassName = "messages.AvailableEffects"

// MessagesAvailableEffectsClass represents messages.AvailableEffects generic type.
//
// See https://core.telegram.org/type/messages.AvailableEffects for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesAvailableEffects(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesAvailableEffectsNotModified: // messages.availableEffectsNotModified#d1ed9a5b
//	case *tg.MessagesAvailableEffects: // messages.availableEffects#bddb616e
//	default: panic(v)
//	}
type MessagesAvailableEffectsClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesAvailableEffectsClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsModified tries to map MessagesAvailableEffectsClass to MessagesAvailableEffects.
	AsModified() (*MessagesAvailableEffects, bool)
}

// AsModified tries to map MessagesAvailableEffectsNotModified to MessagesAvailableEffects.
func (a *MessagesAvailableEffectsNotModified) AsModified() (*MessagesAvailableEffects, bool) {
	return nil, false
}

// AsModified tries to map MessagesAvailableEffects to MessagesAvailableEffects.
func (a *MessagesAvailableEffects) AsModified() (*MessagesAvailableEffects, bool) {
	return a, true
}

// DecodeMessagesAvailableEffects implements binary de-serialization for MessagesAvailableEffectsClass.
func DecodeMessagesAvailableEffects(buf *bin.Buffer) (MessagesAvailableEffectsClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessagesAvailableEffectsNotModifiedTypeID:
		// Decoding messages.availableEffectsNotModified#d1ed9a5b.
		v := MessagesAvailableEffectsNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesAvailableEffectsClass: %w", err)
		}
		return &v, nil
	case MessagesAvailableEffectsTypeID:
		// Decoding messages.availableEffects#bddb616e.
		v := MessagesAvailableEffects{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesAvailableEffectsClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesAvailableEffectsClass: %w", bin.NewUnexpectedID(id))
	}
}

// MessagesAvailableEffects boxes the MessagesAvailableEffectsClass providing a helper.
type MessagesAvailableEffectsBox struct {
	AvailableEffects MessagesAvailableEffectsClass
}

// Decode implements bin.Decoder for MessagesAvailableEffectsBox.
func (b *MessagesAvailableEffectsBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode MessagesAvailableEffectsBox to nil")
	}
	v, err := DecodeMessagesAvailableEffects(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.AvailableEffects = v
	return nil
}

// Encode implements bin.Encode for MessagesAvailableEffectsBox.
func (b *MessagesAvailableEffectsBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.AvailableEffects == nil {
		return fmt.Errorf("unable to encode MessagesAvailableEffectsClass as nil")
	}
	return b.AvailableEffects.Encode(buf)
}
