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

// StarsRevenueStatus represents TL type `starsRevenueStatus#febe5491`.
// Describes Telegram Star revenue balances »¹.
//
// Links:
//  1. https://core.telegram.org/api/stars
//
// See https://core.telegram.org/constructor/starsRevenueStatus for reference.
type StarsRevenueStatus struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, the user may withdraw¹ up to available_balance stars.
	//
	// Links:
	//  1) https://core.telegram.org/api/stars#withdrawing-revenue
	WithdrawalEnabled bool
	// Amount of not-yet-withdrawn Telegram Stars.
	CurrentBalance StarsAmountClass
	// Amount of withdrawable Telegram Stars.
	AvailableBalance StarsAmountClass
	// Total amount of earned Telegram Stars.
	OverallRevenue StarsAmountClass
	// Unixtime indicating when will withdrawal be available to the user. If not set,
	// withdrawal can be started now.
	//
	// Use SetNextWithdrawalAt and GetNextWithdrawalAt helpers.
	NextWithdrawalAt int
}

// StarsRevenueStatusTypeID is TL type id of StarsRevenueStatus.
const StarsRevenueStatusTypeID = 0xfebe5491

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

func (s *StarsRevenueStatus) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.WithdrawalEnabled == false) {
		return false
	}
	if !(s.CurrentBalance == nil) {
		return false
	}
	if !(s.AvailableBalance == nil) {
		return false
	}
	if !(s.OverallRevenue == nil) {
		return false
	}
	if !(s.NextWithdrawalAt == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StarsRevenueStatus from given interface.
func (s *StarsRevenueStatus) FillFrom(from interface {
	GetWithdrawalEnabled() (value bool)
	GetCurrentBalance() (value StarsAmountClass)
	GetAvailableBalance() (value StarsAmountClass)
	GetOverallRevenue() (value StarsAmountClass)
	GetNextWithdrawalAt() (value int, ok bool)
}) {
	s.WithdrawalEnabled = from.GetWithdrawalEnabled()
	s.CurrentBalance = from.GetCurrentBalance()
	s.AvailableBalance = from.GetAvailableBalance()
	s.OverallRevenue = from.GetOverallRevenue()
	if val, ok := from.GetNextWithdrawalAt(); ok {
		s.NextWithdrawalAt = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*StarsRevenueStatus) TypeName() string {
	return "starsRevenueStatus"
}

// TypeInfo returns info about TL type.
func (s *StarsRevenueStatus) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starsRevenueStatus",
		ID:   StarsRevenueStatusTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "WithdrawalEnabled",
			SchemaName: "withdrawal_enabled",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "CurrentBalance",
			SchemaName: "current_balance",
		},
		{
			Name:       "AvailableBalance",
			SchemaName: "available_balance",
		},
		{
			Name:       "OverallRevenue",
			SchemaName: "overall_revenue",
		},
		{
			Name:       "NextWithdrawalAt",
			SchemaName: "next_withdrawal_at",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StarsRevenueStatus) SetFlags() {
	if !(s.WithdrawalEnabled == false) {
		s.Flags.Set(0)
	}
	if !(s.NextWithdrawalAt == 0) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *StarsRevenueStatus) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsRevenueStatus#febe5491 as nil")
	}
	b.PutID(StarsRevenueStatusTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarsRevenueStatus) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsRevenueStatus#febe5491 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field flags: %w", err)
	}
	if s.CurrentBalance == nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field current_balance is nil")
	}
	if err := s.CurrentBalance.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field current_balance: %w", err)
	}
	if s.AvailableBalance == nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field available_balance is nil")
	}
	if err := s.AvailableBalance.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field available_balance: %w", err)
	}
	if s.OverallRevenue == nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field overall_revenue is nil")
	}
	if err := s.OverallRevenue.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsRevenueStatus#febe5491: field overall_revenue: %w", err)
	}
	if s.Flags.Has(1) {
		b.PutInt(s.NextWithdrawalAt)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StarsRevenueStatus) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsRevenueStatus#febe5491 to nil")
	}
	if err := b.ConsumeID(StarsRevenueStatusTypeID); err != nil {
		return fmt.Errorf("unable to decode starsRevenueStatus#febe5491: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarsRevenueStatus) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsRevenueStatus#febe5491 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starsRevenueStatus#febe5491: field flags: %w", err)
		}
	}
	s.WithdrawalEnabled = s.Flags.Has(0)
	{
		value, err := DecodeStarsAmount(b)
		if err != nil {
			return fmt.Errorf("unable to decode starsRevenueStatus#febe5491: field current_balance: %w", err)
		}
		s.CurrentBalance = value
	}
	{
		value, err := DecodeStarsAmount(b)
		if err != nil {
			return fmt.Errorf("unable to decode starsRevenueStatus#febe5491: field available_balance: %w", err)
		}
		s.AvailableBalance = value
	}
	{
		value, err := DecodeStarsAmount(b)
		if err != nil {
			return fmt.Errorf("unable to decode starsRevenueStatus#febe5491: field overall_revenue: %w", err)
		}
		s.OverallRevenue = value
	}
	if s.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starsRevenueStatus#febe5491: field next_withdrawal_at: %w", err)
		}
		s.NextWithdrawalAt = value
	}
	return nil
}

// SetWithdrawalEnabled sets value of WithdrawalEnabled conditional field.
func (s *StarsRevenueStatus) SetWithdrawalEnabled(value bool) {
	if value {
		s.Flags.Set(0)
		s.WithdrawalEnabled = true
	} else {
		s.Flags.Unset(0)
		s.WithdrawalEnabled = false
	}
}

// GetWithdrawalEnabled returns value of WithdrawalEnabled conditional field.
func (s *StarsRevenueStatus) GetWithdrawalEnabled() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetCurrentBalance returns value of CurrentBalance field.
func (s *StarsRevenueStatus) GetCurrentBalance() (value StarsAmountClass) {
	if s == nil {
		return
	}
	return s.CurrentBalance
}

// GetAvailableBalance returns value of AvailableBalance field.
func (s *StarsRevenueStatus) GetAvailableBalance() (value StarsAmountClass) {
	if s == nil {
		return
	}
	return s.AvailableBalance
}

// GetOverallRevenue returns value of OverallRevenue field.
func (s *StarsRevenueStatus) GetOverallRevenue() (value StarsAmountClass) {
	if s == nil {
		return
	}
	return s.OverallRevenue
}

// SetNextWithdrawalAt sets value of NextWithdrawalAt conditional field.
func (s *StarsRevenueStatus) SetNextWithdrawalAt(value int) {
	s.Flags.Set(1)
	s.NextWithdrawalAt = value
}

// GetNextWithdrawalAt returns value of NextWithdrawalAt conditional field and
// boolean which is true if field was set.
func (s *StarsRevenueStatus) GetNextWithdrawalAt() (value int, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.NextWithdrawalAt, true
}
