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

// FoundChatBoosts represents TL type `foundChatBoosts#dc80d52e`.
type FoundChatBoosts struct {
	// Total number of boosts applied to the chat
	TotalCount int32
	// List of boosts
	Boosts []ChatBoost
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// FoundChatBoostsTypeID is TL type id of FoundChatBoosts.
const FoundChatBoostsTypeID = 0xdc80d52e

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

func (f *FoundChatBoosts) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.TotalCount == 0) {
		return false
	}
	if !(f.Boosts == nil) {
		return false
	}
	if !(f.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundChatBoosts) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundChatBoosts",
		ID:   FoundChatBoostsTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Boosts",
			SchemaName: "boosts",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundChatBoosts) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundChatBoosts#dc80d52e as nil")
	}
	b.PutID(FoundChatBoostsTypeID)
	return f.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (f *FoundChatBoosts) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundChatBoosts#dc80d52e to nil")
	}
	if err := b.ConsumeID(FoundChatBoostsTypeID); err != nil {
		return fmt.Errorf("unable to decode foundChatBoosts#dc80d52e: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FoundChatBoosts) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundChatBoosts#dc80d52e to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode foundChatBoosts#dc80d52e: field total_count: %w", err)
		}
		f.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode foundChatBoosts#dc80d52e: field boosts: %w", err)
		}

		if headerLen > 0 {
			f.Boosts = make([]ChatBoost, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ChatBoost
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare foundChatBoosts#dc80d52e: field boosts: %w", err)
			}
			f.Boosts = append(f.Boosts, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode foundChatBoosts#dc80d52e: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FoundChatBoosts) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode foundChatBoosts#dc80d52e to nil")
	}

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

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

// GetBoosts returns value of Boosts field.
func (f *FoundChatBoosts) GetBoosts() (value []ChatBoost) {
	if f == nil {
		return
	}
	return f.Boosts
}

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