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

// PaymentsUniqueStarGift represents TL type `payments.uniqueStarGift#416c56e8`.
// Represents a collectible gift »¹.
//
// Links:
//  1. https://core.telegram.org/api/gifts#collectible-gifts
//
// See https://core.telegram.org/constructor/payments.uniqueStarGift for reference.
type PaymentsUniqueStarGift struct {
	// The starGiftUnique¹ constructor.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/starGiftUnique
	Gift StarGiftClass
	// Chats mentioned in the gift field.
	Chats []ChatClass
	// Users mentioned in the gift field.
	Users []UserClass
}

// PaymentsUniqueStarGiftTypeID is TL type id of PaymentsUniqueStarGift.
const PaymentsUniqueStarGiftTypeID = 0x416c56e8

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

func (u *PaymentsUniqueStarGift) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Gift == nil) {
		return false
	}
	if !(u.Chats == nil) {
		return false
	}
	if !(u.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsUniqueStarGift from given interface.
func (u *PaymentsUniqueStarGift) FillFrom(from interface {
	GetGift() (value StarGiftClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	u.Gift = from.GetGift()
	u.Chats = from.GetChats()
	u.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (u *PaymentsUniqueStarGift) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.uniqueStarGift",
		ID:   PaymentsUniqueStarGiftTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *PaymentsUniqueStarGift) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode payments.uniqueStarGift#416c56e8 as nil")
	}
	b.PutID(PaymentsUniqueStarGiftTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *PaymentsUniqueStarGift) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode payments.uniqueStarGift#416c56e8 as nil")
	}
	if u.Gift == nil {
		return fmt.Errorf("unable to encode payments.uniqueStarGift#416c56e8: field gift is nil")
	}
	if err := u.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.uniqueStarGift#416c56e8: field gift: %w", err)
	}
	b.PutVectorHeader(len(u.Chats))
	for idx, v := range u.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode payments.uniqueStarGift#416c56e8: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.uniqueStarGift#416c56e8: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(u.Users))
	for idx, v := range u.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.uniqueStarGift#416c56e8: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.uniqueStarGift#416c56e8: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *PaymentsUniqueStarGift) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode payments.uniqueStarGift#416c56e8 to nil")
	}
	if err := b.ConsumeID(PaymentsUniqueStarGiftTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.uniqueStarGift#416c56e8: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *PaymentsUniqueStarGift) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode payments.uniqueStarGift#416c56e8 to nil")
	}
	{
		value, err := DecodeStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.uniqueStarGift#416c56e8: field gift: %w", err)
		}
		u.Gift = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.uniqueStarGift#416c56e8: field chats: %w", err)
		}

		if headerLen > 0 {
			u.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.uniqueStarGift#416c56e8: field chats: %w", err)
			}
			u.Chats = append(u.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.uniqueStarGift#416c56e8: field users: %w", err)
		}

		if headerLen > 0 {
			u.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.uniqueStarGift#416c56e8: field users: %w", err)
			}
			u.Users = append(u.Users, value)
		}
	}
	return nil
}

// GetGift returns value of Gift field.
func (u *PaymentsUniqueStarGift) GetGift() (value StarGiftClass) {
	if u == nil {
		return
	}
	return u.Gift
}

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

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

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

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