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

// PaymentsResaleStarGifts represents TL type `payments.resaleStarGifts#947a12df`.
// List of gifts currently on resale »¹.
//
// Links:
//  1. https://core.telegram.org/api/gifts#reselling-collectible-gifts
//
// See https://core.telegram.org/constructor/payments.resaleStarGifts for reference.
type PaymentsResaleStarGifts 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
	// Collectible gifts on resale (may be less than count, in which case next_offset will be
	// set).
	Gifts []StarGiftClass
	// Offset for pagination, pass this to payments.getResaleStarGifts¹.offset to fetch the
	// next results.
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getResaleStarGifts
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
	// Possible gift attributes, only set if payments.getResaleStarGifts¹.attributes_hash is
	// set (on the first call, it must be equal to 0).
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getResaleStarGifts
	//
	// Use SetAttributes and GetAttributes helpers.
	Attributes []StarGiftAttributeClass
	// Hash of the attributes field, pass this to payments.getResaleStarGifts¹
	// attributes_hash to avoid returning any attributes (flag not set) if they haven't
	// changed.
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getResaleStarGifts
	//
	// Use SetAttributesHash and GetAttributesHash helpers.
	AttributesHash int64
	// Chats mentioned in the attributes.
	Chats []ChatClass
	// Indicates the total number of gifts that have a specific attribute, only set if
	// payments.getResaleStarGifts¹.offset is empty (since this field is not related to the
	// current result page but to all of them, it's only returned on the first page).
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getResaleStarGifts
	//
	// Use SetCounters and GetCounters helpers.
	Counters []StarGiftAttributeCounter
	// Users mentioned in the attributes.
	Users []UserClass
}

// PaymentsResaleStarGiftsTypeID is TL type id of PaymentsResaleStarGifts.
const PaymentsResaleStarGiftsTypeID = 0x947a12df

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

func (r *PaymentsResaleStarGifts) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Count == 0) {
		return false
	}
	if !(r.Gifts == nil) {
		return false
	}
	if !(r.NextOffset == "") {
		return false
	}
	if !(r.Attributes == nil) {
		return false
	}
	if !(r.AttributesHash == 0) {
		return false
	}
	if !(r.Chats == nil) {
		return false
	}
	if !(r.Counters == nil) {
		return false
	}
	if !(r.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsResaleStarGifts from given interface.
func (r *PaymentsResaleStarGifts) FillFrom(from interface {
	GetCount() (value int)
	GetGifts() (value []StarGiftClass)
	GetNextOffset() (value string, ok bool)
	GetAttributes() (value []StarGiftAttributeClass, ok bool)
	GetAttributesHash() (value int64, ok bool)
	GetChats() (value []ChatClass)
	GetCounters() (value []StarGiftAttributeCounter, ok bool)
	GetUsers() (value []UserClass)
}) {
	r.Count = from.GetCount()
	r.Gifts = from.GetGifts()
	if val, ok := from.GetNextOffset(); ok {
		r.NextOffset = val
	}

	if val, ok := from.GetAttributes(); ok {
		r.Attributes = val
	}

	if val, ok := from.GetAttributesHash(); ok {
		r.AttributesHash = val
	}

	r.Chats = from.GetChats()
	if val, ok := from.GetCounters(); ok {
		r.Counters = val
	}

	r.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsResaleStarGifts) TypeName() string {
	return "payments.resaleStarGifts"
}

// TypeInfo returns info about TL type.
func (r *PaymentsResaleStarGifts) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.resaleStarGifts",
		ID:   PaymentsResaleStarGiftsTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Gifts",
			SchemaName: "gifts",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "Attributes",
			SchemaName: "attributes",
			Null:       !r.Flags.Has(1),
		},
		{
			Name:       "AttributesHash",
			SchemaName: "attributes_hash",
			Null:       !r.Flags.Has(1),
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Counters",
			SchemaName: "counters",
			Null:       !r.Flags.Has(2),
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *PaymentsResaleStarGifts) SetFlags() {
	if !(r.NextOffset == "") {
		r.Flags.Set(0)
	}
	if !(r.Attributes == nil) {
		r.Flags.Set(1)
	}
	if !(r.AttributesHash == 0) {
		r.Flags.Set(1)
	}
	if !(r.Counters == nil) {
		r.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (r *PaymentsResaleStarGifts) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode payments.resaleStarGifts#947a12df as nil")
	}
	b.PutID(PaymentsResaleStarGiftsTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *PaymentsResaleStarGifts) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode payments.resaleStarGifts#947a12df as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field flags: %w", err)
	}
	b.PutInt(r.Count)
	b.PutVectorHeader(len(r.Gifts))
	for idx, v := range r.Gifts {
		if v == nil {
			return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field gifts element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field gifts element with index %d: %w", idx, err)
		}
	}
	if r.Flags.Has(0) {
		b.PutString(r.NextOffset)
	}
	if r.Flags.Has(1) {
		b.PutVectorHeader(len(r.Attributes))
		for idx, v := range r.Attributes {
			if v == nil {
				return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field attributes element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field attributes element with index %d: %w", idx, err)
			}
		}
	}
	if r.Flags.Has(1) {
		b.PutLong(r.AttributesHash)
	}
	b.PutVectorHeader(len(r.Chats))
	for idx, v := range r.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field chats element with index %d: %w", idx, err)
		}
	}
	if r.Flags.Has(2) {
		b.PutVectorHeader(len(r.Counters))
		for idx, v := range r.Counters {
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field counters element with index %d: %w", idx, err)
			}
		}
	}
	b.PutVectorHeader(len(r.Users))
	for idx, v := range r.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.resaleStarGifts#947a12df: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *PaymentsResaleStarGifts) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode payments.resaleStarGifts#947a12df to nil")
	}
	if err := b.ConsumeID(PaymentsResaleStarGiftsTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *PaymentsResaleStarGifts) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode payments.resaleStarGifts#947a12df to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field count: %w", err)
		}
		r.Count = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field gifts: %w", err)
		}

		if headerLen > 0 {
			r.Gifts = make([]StarGiftClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeStarGift(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field gifts: %w", err)
			}
			r.Gifts = append(r.Gifts, value)
		}
	}
	if r.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field next_offset: %w", err)
		}
		r.NextOffset = value
	}
	if r.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field attributes: %w", err)
		}

		if headerLen > 0 {
			r.Attributes = make([]StarGiftAttributeClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeStarGiftAttribute(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field attributes: %w", err)
			}
			r.Attributes = append(r.Attributes, value)
		}
	}
	if r.Flags.Has(1) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field attributes_hash: %w", err)
		}
		r.AttributesHash = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field chats: %w", err)
		}

		if headerLen > 0 {
			r.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 payments.resaleStarGifts#947a12df: field chats: %w", err)
			}
			r.Chats = append(r.Chats, value)
		}
	}
	if r.Flags.Has(2) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field counters: %w", err)
		}

		if headerLen > 0 {
			r.Counters = make([]StarGiftAttributeCounter, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarGiftAttributeCounter
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field counters: %w", err)
			}
			r.Counters = append(r.Counters, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resaleStarGifts#947a12df: field users: %w", err)
		}

		if headerLen > 0 {
			r.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 payments.resaleStarGifts#947a12df: field users: %w", err)
			}
			r.Users = append(r.Users, value)
		}
	}
	return nil
}

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

// GetGifts returns value of Gifts field.
func (r *PaymentsResaleStarGifts) GetGifts() (value []StarGiftClass) {
	if r == nil {
		return
	}
	return r.Gifts
}

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

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

// SetAttributes sets value of Attributes conditional field.
func (r *PaymentsResaleStarGifts) SetAttributes(value []StarGiftAttributeClass) {
	r.Flags.Set(1)
	r.Attributes = value
}

// GetAttributes returns value of Attributes conditional field and
// boolean which is true if field was set.
func (r *PaymentsResaleStarGifts) GetAttributes() (value []StarGiftAttributeClass, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.Attributes, true
}

// SetAttributesHash sets value of AttributesHash conditional field.
func (r *PaymentsResaleStarGifts) SetAttributesHash(value int64) {
	r.Flags.Set(1)
	r.AttributesHash = value
}

// GetAttributesHash returns value of AttributesHash conditional field and
// boolean which is true if field was set.
func (r *PaymentsResaleStarGifts) GetAttributesHash() (value int64, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.AttributesHash, true
}

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

// SetCounters sets value of Counters conditional field.
func (r *PaymentsResaleStarGifts) SetCounters(value []StarGiftAttributeCounter) {
	r.Flags.Set(2)
	r.Counters = value
}

// GetCounters returns value of Counters conditional field and
// boolean which is true if field was set.
func (r *PaymentsResaleStarGifts) GetCounters() (value []StarGiftAttributeCounter, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(2) {
		return value, false
	}
	return r.Counters, true
}

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

// MapGifts returns field Gifts wrapped in StarGiftClassArray helper.
func (r *PaymentsResaleStarGifts) MapGifts() (value StarGiftClassArray) {
	return StarGiftClassArray(r.Gifts)
}

// MapAttributes returns field Attributes wrapped in StarGiftAttributeClassArray helper.
func (r *PaymentsResaleStarGifts) MapAttributes() (value StarGiftAttributeClassArray, ok bool) {
	if !r.Flags.Has(1) {
		return value, false
	}
	return StarGiftAttributeClassArray(r.Attributes), true
}

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

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