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

// PaymentsStarsRevenueStats represents TL type `payments.starsRevenueStats#6c207376`.
// Star revenue statistics, see here »¹ for more info.
// Note that all balances and currency amounts and graph values are in Stars.
//
// Links:
//  1. https://core.telegram.org/api/stars
//
// See https://core.telegram.org/constructor/payments.starsRevenueStats for reference.
type PaymentsStarsRevenueStats struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// For ad revenue statistics, ad impressions graph
	//
	// Use SetTopHoursGraph and GetTopHoursGraph helpers.
	TopHoursGraph StatsGraphClass
	// Star revenue graph (number of earned stars)
	RevenueGraph StatsGraphClass
	// Current balance, current withdrawable balance and overall earned Telegram Stars
	Status StarsRevenueStatus
	// Current conversion rate of Telegram Stars to USD
	UsdRate float64
}

// PaymentsStarsRevenueStatsTypeID is TL type id of PaymentsStarsRevenueStats.
const PaymentsStarsRevenueStatsTypeID = 0x6c207376

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

func (s *PaymentsStarsRevenueStats) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.TopHoursGraph == nil) {
		return false
	}
	if !(s.RevenueGraph == nil) {
		return false
	}
	if !(s.Status.Zero()) {
		return false
	}
	if !(s.UsdRate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarsRevenueStats from given interface.
func (s *PaymentsStarsRevenueStats) FillFrom(from interface {
	GetTopHoursGraph() (value StatsGraphClass, ok bool)
	GetRevenueGraph() (value StatsGraphClass)
	GetStatus() (value StarsRevenueStatus)
	GetUsdRate() (value float64)
}) {
	if val, ok := from.GetTopHoursGraph(); ok {
		s.TopHoursGraph = val
	}

	s.RevenueGraph = from.GetRevenueGraph()
	s.Status = from.GetStatus()
	s.UsdRate = from.GetUsdRate()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsStarsRevenueStats) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starsRevenueStats",
		ID:   PaymentsStarsRevenueStatsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TopHoursGraph",
			SchemaName: "top_hours_graph",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "RevenueGraph",
			SchemaName: "revenue_graph",
		},
		{
			Name:       "Status",
			SchemaName: "status",
		},
		{
			Name:       "UsdRate",
			SchemaName: "usd_rate",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *PaymentsStarsRevenueStats) SetFlags() {
	if !(s.TopHoursGraph == nil) {
		s.Flags.Set(0)
	}
}

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

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarsRevenueStats) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starsRevenueStats#6c207376 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starsRevenueStats#6c207376: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		if s.TopHoursGraph == nil {
			return fmt.Errorf("unable to encode payments.starsRevenueStats#6c207376: field top_hours_graph is nil")
		}
		if err := s.TopHoursGraph.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starsRevenueStats#6c207376: field top_hours_graph: %w", err)
		}
	}
	if s.RevenueGraph == nil {
		return fmt.Errorf("unable to encode payments.starsRevenueStats#6c207376: field revenue_graph is nil")
	}
	if err := s.RevenueGraph.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starsRevenueStats#6c207376: field revenue_graph: %w", err)
	}
	if err := s.Status.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starsRevenueStats#6c207376: field status: %w", err)
	}
	b.PutDouble(s.UsdRate)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsStarsRevenueStats) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starsRevenueStats#6c207376 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.starsRevenueStats#6c207376: field flags: %w", err)
		}
	}
	if s.Flags.Has(0) {
		value, err := DecodeStatsGraph(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsRevenueStats#6c207376: field top_hours_graph: %w", err)
		}
		s.TopHoursGraph = value
	}
	{
		value, err := DecodeStatsGraph(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsRevenueStats#6c207376: field revenue_graph: %w", err)
		}
		s.RevenueGraph = value
	}
	{
		if err := s.Status.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.starsRevenueStats#6c207376: field status: %w", err)
		}
	}
	{
		value, err := b.Double()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starsRevenueStats#6c207376: field usd_rate: %w", err)
		}
		s.UsdRate = value
	}
	return nil
}

// SetTopHoursGraph sets value of TopHoursGraph conditional field.
func (s *PaymentsStarsRevenueStats) SetTopHoursGraph(value StatsGraphClass) {
	s.Flags.Set(0)
	s.TopHoursGraph = value
}

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

// GetRevenueGraph returns value of RevenueGraph field.
func (s *PaymentsStarsRevenueStats) GetRevenueGraph() (value StatsGraphClass) {
	if s == nil {
		return
	}
	return s.RevenueGraph
}

// GetStatus returns value of Status field.
func (s *PaymentsStarsRevenueStats) GetStatus() (value StarsRevenueStatus) {
	if s == nil {
		return
	}
	return s.Status
}

// GetUsdRate returns value of UsdRate field.
func (s *PaymentsStarsRevenueStats) GetUsdRate() (value float64) {
	if s == nil {
		return
	}
	return s.UsdRate
}
