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

// AvailableReactions represents TL type `availableReactions#3596f453`.
type AvailableReactions struct {
	// List of reactions to be shown at the top
	TopReactions []AvailableReaction
	// List of recently used reactions
	RecentReactions []AvailableReaction
	// List of popular reactions
	PopularReactions []AvailableReaction
	// True, if any custom emoji reaction can be added by Telegram Premium subscribers
	AllowCustomEmoji bool
	// True, if the reactions will be tags and the message can be found by them
	AreTags bool
	// The reason why the current user can't add reactions to the message, despite some other
	// users can; may be null if none
	UnavailabilityReason ReactionUnavailabilityReasonClass
}

// AvailableReactionsTypeID is TL type id of AvailableReactions.
const AvailableReactionsTypeID = 0x3596f453

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

func (a *AvailableReactions) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.TopReactions == nil) {
		return false
	}
	if !(a.RecentReactions == nil) {
		return false
	}
	if !(a.PopularReactions == nil) {
		return false
	}
	if !(a.AllowCustomEmoji == false) {
		return false
	}
	if !(a.AreTags == false) {
		return false
	}
	if !(a.UnavailabilityReason == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AvailableReactions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "availableReactions",
		ID:   AvailableReactionsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TopReactions",
			SchemaName: "top_reactions",
		},
		{
			Name:       "RecentReactions",
			SchemaName: "recent_reactions",
		},
		{
			Name:       "PopularReactions",
			SchemaName: "popular_reactions",
		},
		{
			Name:       "AllowCustomEmoji",
			SchemaName: "allow_custom_emoji",
		},
		{
			Name:       "AreTags",
			SchemaName: "are_tags",
		},
		{
			Name:       "UnavailabilityReason",
			SchemaName: "unavailability_reason",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AvailableReactions) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode availableReactions#3596f453 as nil")
	}
	b.PutID(AvailableReactionsTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AvailableReactions) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode availableReactions#3596f453 as nil")
	}
	b.PutInt(len(a.TopReactions))
	for idx, v := range a.TopReactions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare availableReactions#3596f453: field top_reactions element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(a.RecentReactions))
	for idx, v := range a.RecentReactions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare availableReactions#3596f453: field recent_reactions element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(a.PopularReactions))
	for idx, v := range a.PopularReactions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare availableReactions#3596f453: field popular_reactions element with index %d: %w", idx, err)
		}
	}
	b.PutBool(a.AllowCustomEmoji)
	b.PutBool(a.AreTags)
	if a.UnavailabilityReason == nil {
		return fmt.Errorf("unable to encode availableReactions#3596f453: field unavailability_reason is nil")
	}
	if err := a.UnavailabilityReason.Encode(b); err != nil {
		return fmt.Errorf("unable to encode availableReactions#3596f453: field unavailability_reason: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AvailableReactions) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode availableReactions#3596f453 to nil")
	}
	if err := b.ConsumeID(AvailableReactionsTypeID); err != nil {
		return fmt.Errorf("unable to decode availableReactions#3596f453: %w", err)
	}
	return a.DecodeBare(b)
}

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

		if headerLen > 0 {
			a.TopReactions = make([]AvailableReaction, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AvailableReaction
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare availableReactions#3596f453: field top_reactions: %w", err)
			}
			a.TopReactions = append(a.TopReactions, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode availableReactions#3596f453: field recent_reactions: %w", err)
		}

		if headerLen > 0 {
			a.RecentReactions = make([]AvailableReaction, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AvailableReaction
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare availableReactions#3596f453: field recent_reactions: %w", err)
			}
			a.RecentReactions = append(a.RecentReactions, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode availableReactions#3596f453: field popular_reactions: %w", err)
		}

		if headerLen > 0 {
			a.PopularReactions = make([]AvailableReaction, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AvailableReaction
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare availableReactions#3596f453: field popular_reactions: %w", err)
			}
			a.PopularReactions = append(a.PopularReactions, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode availableReactions#3596f453: field allow_custom_emoji: %w", err)
		}
		a.AllowCustomEmoji = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode availableReactions#3596f453: field are_tags: %w", err)
		}
		a.AreTags = value
	}
	{
		value, err := DecodeReactionUnavailabilityReason(b)
		if err != nil {
			return fmt.Errorf("unable to decode availableReactions#3596f453: field unavailability_reason: %w", err)
		}
		a.UnavailabilityReason = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AvailableReactions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode availableReactions#3596f453 as nil")
	}
	b.ObjStart()
	b.PutID("availableReactions")
	b.Comma()
	b.FieldStart("top_reactions")
	b.ArrStart()
	for idx, v := range a.TopReactions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode availableReactions#3596f453: field top_reactions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("recent_reactions")
	b.ArrStart()
	for idx, v := range a.RecentReactions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode availableReactions#3596f453: field recent_reactions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("popular_reactions")
	b.ArrStart()
	for idx, v := range a.PopularReactions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode availableReactions#3596f453: field popular_reactions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("allow_custom_emoji")
	b.PutBool(a.AllowCustomEmoji)
	b.Comma()
	b.FieldStart("are_tags")
	b.PutBool(a.AreTags)
	b.Comma()
	b.FieldStart("unavailability_reason")
	if a.UnavailabilityReason == nil {
		return fmt.Errorf("unable to encode availableReactions#3596f453: field unavailability_reason is nil")
	}
	if err := a.UnavailabilityReason.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode availableReactions#3596f453: field unavailability_reason: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AvailableReactions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode availableReactions#3596f453 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("availableReactions"); err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: %w", err)
			}
		case "top_reactions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value AvailableReaction
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode availableReactions#3596f453: field top_reactions: %w", err)
				}
				a.TopReactions = append(a.TopReactions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: field top_reactions: %w", err)
			}
		case "recent_reactions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value AvailableReaction
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode availableReactions#3596f453: field recent_reactions: %w", err)
				}
				a.RecentReactions = append(a.RecentReactions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: field recent_reactions: %w", err)
			}
		case "popular_reactions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value AvailableReaction
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode availableReactions#3596f453: field popular_reactions: %w", err)
				}
				a.PopularReactions = append(a.PopularReactions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: field popular_reactions: %w", err)
			}
		case "allow_custom_emoji":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: field allow_custom_emoji: %w", err)
			}
			a.AllowCustomEmoji = value
		case "are_tags":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: field are_tags: %w", err)
			}
			a.AreTags = value
		case "unavailability_reason":
			value, err := DecodeTDLibJSONReactionUnavailabilityReason(b)
			if err != nil {
				return fmt.Errorf("unable to decode availableReactions#3596f453: field unavailability_reason: %w", err)
			}
			a.UnavailabilityReason = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTopReactions returns value of TopReactions field.
func (a *AvailableReactions) GetTopReactions() (value []AvailableReaction) {
	if a == nil {
		return
	}
	return a.TopReactions
}

// GetRecentReactions returns value of RecentReactions field.
func (a *AvailableReactions) GetRecentReactions() (value []AvailableReaction) {
	if a == nil {
		return
	}
	return a.RecentReactions
}

// GetPopularReactions returns value of PopularReactions field.
func (a *AvailableReactions) GetPopularReactions() (value []AvailableReaction) {
	if a == nil {
		return
	}
	return a.PopularReactions
}

// GetAllowCustomEmoji returns value of AllowCustomEmoji field.
func (a *AvailableReactions) GetAllowCustomEmoji() (value bool) {
	if a == nil {
		return
	}
	return a.AllowCustomEmoji
}

// GetAreTags returns value of AreTags field.
func (a *AvailableReactions) GetAreTags() (value bool) {
	if a == nil {
		return
	}
	return a.AreTags
}

// GetUnavailabilityReason returns value of UnavailabilityReason field.
func (a *AvailableReactions) GetUnavailabilityReason() (value ReactionUnavailabilityReasonClass) {
	if a == nil {
		return
	}
	return a.UnavailabilityReason
}
