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

// PaymentsStarsStatus represents TL type `payments.starsStatus#6c9ce8ed`.
// Info about the current Telegram Star subscriptions, balance and transaction history
// »¹.
//
// Links:
//  1. https://core.telegram.org/api/stars#balance-and-transaction-history
//
// See https://core.telegram.org/constructor/payments.starsStatus for reference.
type PaymentsStarsStatus struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Current Telegram Star balance.
	Balance StarsAmountClass
	// Info about current Telegram Star subscriptions, only returned when invoking payments
	// getStarsTransactions¹ and payments.getStarsSubscriptions².
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getStarsTransactions
	//  2) https://core.telegram.org/method/payments.getStarsSubscriptions
	//
	// Use SetSubscriptions and GetSubscriptions helpers.
	Subscriptions []StarsSubscription
	// Offset for pagination of subscriptions: only usable and returned when invoking
	// payments.getStarsSubscriptions¹.
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getStarsSubscriptions
	//
	// Use SetSubscriptionsNextOffset and GetSubscriptionsNextOffset helpers.
	SubscriptionsNextOffset string
	// The number of Telegram Stars the user should buy to be able to extend expired
	// subscriptions soon (i.e. the current balance is not enough to extend all expired
	// subscriptions).
	//
	// Use SetSubscriptionsMissingBalance and GetSubscriptionsMissingBalance helpers.
	SubscriptionsMissingBalance int64
	// List of Telegram Star transactions (partial if next_offset is set).
	//
	// Use SetHistory and GetHistory helpers.
	History []StarsTransaction
	// Offset to use to fetch more transactions from the transaction history using payments
	// getStarsTransactions¹.
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getStarsTransactions
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
	// Chats mentioned in history.
	Chats []ChatClass
	// Users mentioned in history.
	Users []UserClass
}

// PaymentsStarsStatusTypeID is TL type id of PaymentsStarsStatus.
const PaymentsStarsStatusTypeID = 0x6c9ce8ed

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

func (s *PaymentsStarsStatus) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Balance == nil) {
		return false
	}
	if !(s.Subscriptions == nil) {
		return false
	}
	if !(s.SubscriptionsNextOffset == "") {
		return false
	}
	if !(s.SubscriptionsMissingBalance == 0) {
		return false
	}
	if !(s.History == nil) {
		return false
	}
	if !(s.NextOffset == "") {
		return false
	}
	if !(s.Chats == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarsStatus from given interface.
func (s *PaymentsStarsStatus) FillFrom(from interface {
	GetBalance() (value StarsAmountClass)
	GetSubscriptions() (value []StarsSubscription, ok bool)
	GetSubscriptionsNextOffset() (value string, ok bool)
	GetSubscriptionsMissingBalance() (value int64, ok bool)
	GetHistory() (value []StarsTransaction, ok bool)
	GetNextOffset() (value string, ok bool)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	s.Balance = from.GetBalance()
	if val, ok := from.GetSubscriptions(); ok {
		s.Subscriptions = val
	}

	if val, ok := from.GetSubscriptionsNextOffset(); ok {
		s.SubscriptionsNextOffset = val
	}

	if val, ok := from.GetSubscriptionsMissingBalance(); ok {
		s.SubscriptionsMissingBalance = val
	}

	if val, ok := from.GetHistory(); ok {
		s.History = val
	}

	if val, ok := from.GetNextOffset(); ok {
		s.NextOffset = val
	}

	s.Chats = from.GetChats()
	s.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsStarsStatus) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starsStatus",
		ID:   PaymentsStarsStatusTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Balance",
			SchemaName: "balance",
		},
		{
			Name:       "Subscriptions",
			SchemaName: "subscriptions",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "SubscriptionsNextOffset",
			SchemaName: "subscriptions_next_offset",
			Null:       !s.Flags.Has(2),
		},
		{
			Name:       "SubscriptionsMissingBalance",
			SchemaName: "subscriptions_missing_balance",
			Null:       !s.Flags.Has(4),
		},
		{
			Name:       "History",
			SchemaName: "history",
			Null:       !s.Flags.Has(3),
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *PaymentsStarsStatus) SetFlags() {
	if !(s.Subscriptions == nil) {
		s.Flags.Set(1)
	}
	if !(s.SubscriptionsNextOffset == "") {
		s.Flags.Set(2)
	}
	if !(s.SubscriptionsMissingBalance == 0) {
		s.Flags.Set(4)
	}
	if !(s.History == nil) {
		s.Flags.Set(3)
	}
	if !(s.NextOffset == "") {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *PaymentsStarsStatus) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starsStatus#6c9ce8ed as nil")
	}
	b.PutID(PaymentsStarsStatusTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarsStatus) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starsStatus#6c9ce8ed as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field flags: %w", err)
	}
	if s.Balance == nil {
		return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field balance is nil")
	}
	if err := s.Balance.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field balance: %w", err)
	}
	if s.Flags.Has(1) {
		b.PutVectorHeader(len(s.Subscriptions))
		for idx, v := range s.Subscriptions {
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field subscriptions element with index %d: %w", idx, err)
			}
		}
	}
	if s.Flags.Has(2) {
		b.PutString(s.SubscriptionsNextOffset)
	}
	if s.Flags.Has(4) {
		b.PutLong(s.SubscriptionsMissingBalance)
	}
	if s.Flags.Has(3) {
		b.PutVectorHeader(len(s.History))
		for idx, v := range s.History {
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field history element with index %d: %w", idx, err)
			}
		}
	}
	if s.Flags.Has(0) {
		b.PutString(s.NextOffset)
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starsStatus#6c9ce8ed: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PaymentsStarsStatus) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starsStatus#6c9ce8ed to nil")
	}
	if err := b.ConsumeID(PaymentsStarsStatusTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsStarsStatus) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starsStatus#6c9ce8ed to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field flags: %w", err)
		}
	}
	{
		value, err := DecodeStarsAmount(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field balance: %w", err)
		}
		s.Balance = value
	}
	if s.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field subscriptions: %w", err)
		}

		if headerLen > 0 {
			s.Subscriptions = make([]StarsSubscription, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarsSubscription
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field subscriptions: %w", err)
			}
			s.Subscriptions = append(s.Subscriptions, value)
		}
	}
	if s.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field subscriptions_next_offset: %w", err)
		}
		s.SubscriptionsNextOffset = value
	}
	if s.Flags.Has(4) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field subscriptions_missing_balance: %w", err)
		}
		s.SubscriptionsMissingBalance = value
	}
	if s.Flags.Has(3) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field history: %w", err)
		}

		if headerLen > 0 {
			s.History = make([]StarsTransaction, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarsTransaction
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field history: %w", err)
			}
			s.History = append(s.History, value)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field next_offset: %w", err)
		}
		s.NextOffset = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsStatus#6c9ce8ed: field chats: %w", err)
		}

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

		if headerLen > 0 {
			s.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.starsStatus#6c9ce8ed: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

// GetBalance returns value of Balance field.
func (s *PaymentsStarsStatus) GetBalance() (value StarsAmountClass) {
	if s == nil {
		return
	}
	return s.Balance
}

// SetSubscriptions sets value of Subscriptions conditional field.
func (s *PaymentsStarsStatus) SetSubscriptions(value []StarsSubscription) {
	s.Flags.Set(1)
	s.Subscriptions = value
}

// GetSubscriptions returns value of Subscriptions conditional field and
// boolean which is true if field was set.
func (s *PaymentsStarsStatus) GetSubscriptions() (value []StarsSubscription, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.Subscriptions, true
}

// SetSubscriptionsNextOffset sets value of SubscriptionsNextOffset conditional field.
func (s *PaymentsStarsStatus) SetSubscriptionsNextOffset(value string) {
	s.Flags.Set(2)
	s.SubscriptionsNextOffset = value
}

// GetSubscriptionsNextOffset returns value of SubscriptionsNextOffset conditional field and
// boolean which is true if field was set.
func (s *PaymentsStarsStatus) GetSubscriptionsNextOffset() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.SubscriptionsNextOffset, true
}

// SetSubscriptionsMissingBalance sets value of SubscriptionsMissingBalance conditional field.
func (s *PaymentsStarsStatus) SetSubscriptionsMissingBalance(value int64) {
	s.Flags.Set(4)
	s.SubscriptionsMissingBalance = value
}

// GetSubscriptionsMissingBalance returns value of SubscriptionsMissingBalance conditional field and
// boolean which is true if field was set.
func (s *PaymentsStarsStatus) GetSubscriptionsMissingBalance() (value int64, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(4) {
		return value, false
	}
	return s.SubscriptionsMissingBalance, true
}

// SetHistory sets value of History conditional field.
func (s *PaymentsStarsStatus) SetHistory(value []StarsTransaction) {
	s.Flags.Set(3)
	s.History = value
}

// GetHistory returns value of History conditional field and
// boolean which is true if field was set.
func (s *PaymentsStarsStatus) GetHistory() (value []StarsTransaction, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(3) {
		return value, false
	}
	return s.History, true
}

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

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

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

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

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

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