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

// AddedReactions represents TL type `addedReactions#77bf7590`.
type AddedReactions struct {
	// The total number of found reactions
	TotalCount int32
	// The list of added reactions
	Reactions []AddedReaction
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// AddedReactionsTypeID is TL type id of AddedReactions.
const AddedReactionsTypeID = 0x77bf7590

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

func (a *AddedReactions) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.TotalCount == 0) {
		return false
	}
	if !(a.Reactions == nil) {
		return false
	}
	if !(a.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AddedReactions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "addedReactions",
		ID:   AddedReactionsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Reactions",
			SchemaName: "reactions",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AddedReactions) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addedReactions#77bf7590 as nil")
	}
	b.PutID(AddedReactionsTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AddedReactions) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addedReactions#77bf7590 as nil")
	}
	b.PutInt32(a.TotalCount)
	b.PutInt(len(a.Reactions))
	for idx, v := range a.Reactions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare addedReactions#77bf7590: field reactions element with index %d: %w", idx, err)
		}
	}
	b.PutString(a.NextOffset)
	return nil
}

// Decode implements bin.Decoder.
func (a *AddedReactions) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addedReactions#77bf7590 to nil")
	}
	if err := b.ConsumeID(AddedReactionsTypeID); err != nil {
		return fmt.Errorf("unable to decode addedReactions#77bf7590: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AddedReactions) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addedReactions#77bf7590 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode addedReactions#77bf7590: field total_count: %w", err)
		}
		a.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode addedReactions#77bf7590: field reactions: %w", err)
		}

		if headerLen > 0 {
			a.Reactions = make([]AddedReaction, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AddedReaction
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare addedReactions#77bf7590: field reactions: %w", err)
			}
			a.Reactions = append(a.Reactions, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode addedReactions#77bf7590: field next_offset: %w", err)
		}
		a.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AddedReactions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode addedReactions#77bf7590 as nil")
	}
	b.ObjStart()
	b.PutID("addedReactions")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(a.TotalCount)
	b.Comma()
	b.FieldStart("reactions")
	b.ArrStart()
	for idx, v := range a.Reactions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode addedReactions#77bf7590: field reactions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(a.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AddedReactions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode addedReactions#77bf7590 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("addedReactions"); err != nil {
				return fmt.Errorf("unable to decode addedReactions#77bf7590: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode addedReactions#77bf7590: field total_count: %w", err)
			}
			a.TotalCount = value
		case "reactions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value AddedReaction
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode addedReactions#77bf7590: field reactions: %w", err)
				}
				a.Reactions = append(a.Reactions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode addedReactions#77bf7590: field reactions: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode addedReactions#77bf7590: field next_offset: %w", err)
			}
			a.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (a *AddedReactions) GetTotalCount() (value int32) {
	if a == nil {
		return
	}
	return a.TotalCount
}

// GetReactions returns value of Reactions field.
func (a *AddedReactions) GetReactions() (value []AddedReaction) {
	if a == nil {
		return
	}
	return a.Reactions
}

// GetNextOffset returns value of NextOffset field.
func (a *AddedReactions) GetNextOffset() (value string) {
	if a == nil {
		return
	}
	return a.NextOffset
}
