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

// PremiumBoostsList represents TL type `premium.boostsList#86f8613c`.
// List of boosts¹ that were applied to a peer by multiple users.
//
// Links:
//  1. https://core.telegram.org/api/boost
//
// See https://core.telegram.org/constructor/premium.boostsList for reference.
type PremiumBoostsList struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Total number of results
	Count int
	// Boosts¹
	//
	// Links:
	//  1) https://core.telegram.org/api/boost
	Boosts []Boost
	// Offset that can be used for pagination¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
	// Mentioned users
	Users []UserClass
}

// PremiumBoostsListTypeID is TL type id of PremiumBoostsList.
const PremiumBoostsListTypeID = 0x86f8613c

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

func (b *PremiumBoostsList) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.Count == 0) {
		return false
	}
	if !(b.Boosts == nil) {
		return false
	}
	if !(b.NextOffset == "") {
		return false
	}
	if !(b.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PremiumBoostsList from given interface.
func (b *PremiumBoostsList) FillFrom(from interface {
	GetCount() (value int)
	GetBoosts() (value []Boost)
	GetNextOffset() (value string, ok bool)
	GetUsers() (value []UserClass)
}) {
	b.Count = from.GetCount()
	b.Boosts = from.GetBoosts()
	if val, ok := from.GetNextOffset(); ok {
		b.NextOffset = val
	}

	b.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PremiumBoostsList) TypeName() string {
	return "premium.boostsList"
}

// TypeInfo returns info about TL type.
func (b *PremiumBoostsList) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premium.boostsList",
		ID:   PremiumBoostsListTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Boosts",
			SchemaName: "boosts",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *PremiumBoostsList) SetFlags() {
	if !(b.NextOffset == "") {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *PremiumBoostsList) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode premium.boostsList#86f8613c as nil")
	}
	buf.PutID(PremiumBoostsListTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *PremiumBoostsList) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode premium.boostsList#86f8613c as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode premium.boostsList#86f8613c: field flags: %w", err)
	}
	buf.PutInt(b.Count)
	buf.PutVectorHeader(len(b.Boosts))
	for idx, v := range b.Boosts {
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode premium.boostsList#86f8613c: field boosts element with index %d: %w", idx, err)
		}
	}
	if b.Flags.Has(0) {
		buf.PutString(b.NextOffset)
	}
	buf.PutVectorHeader(len(b.Users))
	for idx, v := range b.Users {
		if v == nil {
			return fmt.Errorf("unable to encode premium.boostsList#86f8613c: field users element with index %d is nil", idx)
		}
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode premium.boostsList#86f8613c: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *PremiumBoostsList) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode premium.boostsList#86f8613c to nil")
	}
	if err := buf.ConsumeID(PremiumBoostsListTypeID); err != nil {
		return fmt.Errorf("unable to decode premium.boostsList#86f8613c: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *PremiumBoostsList) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode premium.boostsList#86f8613c to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field flags: %w", err)
		}
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field count: %w", err)
		}
		b.Count = value
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field boosts: %w", err)
		}

		if headerLen > 0 {
			b.Boosts = make([]Boost, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Boost
			if err := value.Decode(buf); err != nil {
				return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field boosts: %w", err)
			}
			b.Boosts = append(b.Boosts, value)
		}
	}
	if b.Flags.Has(0) {
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field next_offset: %w", err)
		}
		b.NextOffset = value
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field users: %w", err)
		}

		if headerLen > 0 {
			b.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(buf)
			if err != nil {
				return fmt.Errorf("unable to decode premium.boostsList#86f8613c: field users: %w", err)
			}
			b.Users = append(b.Users, value)
		}
	}
	return nil
}

// GetCount returns value of Count field.
func (b *PremiumBoostsList) GetCount() (value int) {
	if b == nil {
		return
	}
	return b.Count
}

// GetBoosts returns value of Boosts field.
func (b *PremiumBoostsList) GetBoosts() (value []Boost) {
	if b == nil {
		return
	}
	return b.Boosts
}

// SetNextOffset sets value of NextOffset conditional field.
func (b *PremiumBoostsList) SetNextOffset(value string) {
	b.Flags.Set(0)
	b.NextOffset = value
}

// GetNextOffset returns value of NextOffset conditional field and
// boolean which is true if field was set.
func (b *PremiumBoostsList) GetNextOffset() (value string, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(0) {
		return value, false
	}
	return b.NextOffset, true
}

// GetUsers returns value of Users field.
func (b *PremiumBoostsList) GetUsers() (value []UserClass) {
	if b == nil {
		return
	}
	return b.Users
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (b *PremiumBoostsList) MapUsers() (value UserClassArray) {
	return UserClassArray(b.Users)
}
