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

// PhoneGroupCallStars represents TL type `phone.groupCallStars#9d1dbd26`.
//
// See https://core.telegram.org/constructor/phone.groupCallStars for reference.
type PhoneGroupCallStars struct {
	// TotalStars field of PhoneGroupCallStars.
	TotalStars int64
	// TopDonors field of PhoneGroupCallStars.
	TopDonors []GroupCallDonor
	// Chats field of PhoneGroupCallStars.
	Chats []ChatClass
	// Users field of PhoneGroupCallStars.
	Users []UserClass
}

// PhoneGroupCallStarsTypeID is TL type id of PhoneGroupCallStars.
const PhoneGroupCallStarsTypeID = 0x9d1dbd26

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

func (g *PhoneGroupCallStars) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.TotalStars == 0) {
		return false
	}
	if !(g.TopDonors == nil) {
		return false
	}
	if !(g.Chats == nil) {
		return false
	}
	if !(g.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneGroupCallStars from given interface.
func (g *PhoneGroupCallStars) FillFrom(from interface {
	GetTotalStars() (value int64)
	GetTopDonors() (value []GroupCallDonor)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	g.TotalStars = from.GetTotalStars()
	g.TopDonors = from.GetTopDonors()
	g.Chats = from.GetChats()
	g.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PhoneGroupCallStars) TypeName() string {
	return "phone.groupCallStars"
}

// TypeInfo returns info about TL type.
func (g *PhoneGroupCallStars) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.groupCallStars",
		ID:   PhoneGroupCallStarsTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalStars",
			SchemaName: "total_stars",
		},
		{
			Name:       "TopDonors",
			SchemaName: "top_donors",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *PhoneGroupCallStars) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode phone.groupCallStars#9d1dbd26 as nil")
	}
	b.PutID(PhoneGroupCallStarsTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PhoneGroupCallStars) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode phone.groupCallStars#9d1dbd26 as nil")
	}
	b.PutLong(g.TotalStars)
	b.PutVectorHeader(len(g.TopDonors))
	for idx, v := range g.TopDonors {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.groupCallStars#9d1dbd26: field top_donors element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(g.Chats))
	for idx, v := range g.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode phone.groupCallStars#9d1dbd26: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.groupCallStars#9d1dbd26: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(g.Users))
	for idx, v := range g.Users {
		if v == nil {
			return fmt.Errorf("unable to encode phone.groupCallStars#9d1dbd26: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.groupCallStars#9d1dbd26: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *PhoneGroupCallStars) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode phone.groupCallStars#9d1dbd26 to nil")
	}
	if err := b.ConsumeID(PhoneGroupCallStarsTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *PhoneGroupCallStars) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode phone.groupCallStars#9d1dbd26 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: field total_stars: %w", err)
		}
		g.TotalStars = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: field top_donors: %w", err)
		}

		if headerLen > 0 {
			g.TopDonors = make([]GroupCallDonor, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value GroupCallDonor
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: field top_donors: %w", err)
			}
			g.TopDonors = append(g.TopDonors, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: field chats: %w", err)
		}

		if headerLen > 0 {
			g.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: field chats: %w", err)
			}
			g.Chats = append(g.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.groupCallStars#9d1dbd26: field users: %w", err)
		}

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

// GetTotalStars returns value of TotalStars field.
func (g *PhoneGroupCallStars) GetTotalStars() (value int64) {
	if g == nil {
		return
	}
	return g.TotalStars
}

// GetTopDonors returns value of TopDonors field.
func (g *PhoneGroupCallStars) GetTopDonors() (value []GroupCallDonor) {
	if g == nil {
		return
	}
	return g.TopDonors
}

// GetChats returns value of Chats field.
func (g *PhoneGroupCallStars) GetChats() (value []ChatClass) {
	if g == nil {
		return
	}
	return g.Chats
}

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

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (g *PhoneGroupCallStars) MapChats() (value ChatClassArray) {
	return ChatClassArray(g.Chats)
}

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