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

// StatsBroadcastRevenueStats represents TL type `stats.broadcastRevenueStats#5407e297`.
// Channel revenue ad statistics, see here »¹ for more info.
// Note that all balances and currency amounts and graph values are in the smallest unit
// of the chosen cryptocurrency (currently nanotons for TONs, so to obtain a value in USD
// divide the chosen amount by 10^9, and then divide by usd_rate).
//
// Links:
//  1. https://core.telegram.org/api/revenue
//
// See https://core.telegram.org/constructor/stats.broadcastRevenueStats for reference.
type StatsBroadcastRevenueStats struct {
	// Ad impressions graph
	TopHoursGraph StatsGraphClass
	// Ad revenue graph (in the smallest unit of the cryptocurrency in which revenue is
	// calculated)
	RevenueGraph StatsGraphClass
	// Current balance, current withdrawable balance and overall revenue
	Balances BroadcastRevenueBalances
	// Current conversion rate of the cryptocurrency (not in the smallest unit) in which
	// revenue is calculated to USD
	UsdRate float64
}

// StatsBroadcastRevenueStatsTypeID is TL type id of StatsBroadcastRevenueStats.
const StatsBroadcastRevenueStatsTypeID = 0x5407e297

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

func (b *StatsBroadcastRevenueStats) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.TopHoursGraph == nil) {
		return false
	}
	if !(b.RevenueGraph == nil) {
		return false
	}
	if !(b.Balances.Zero()) {
		return false
	}
	if !(b.UsdRate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StatsBroadcastRevenueStats from given interface.
func (b *StatsBroadcastRevenueStats) FillFrom(from interface {
	GetTopHoursGraph() (value StatsGraphClass)
	GetRevenueGraph() (value StatsGraphClass)
	GetBalances() (value BroadcastRevenueBalances)
	GetUsdRate() (value float64)
}) {
	b.TopHoursGraph = from.GetTopHoursGraph()
	b.RevenueGraph = from.GetRevenueGraph()
	b.Balances = from.GetBalances()
	b.UsdRate = from.GetUsdRate()
}

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

// TypeName returns name of type in TL schema.
func (*StatsBroadcastRevenueStats) TypeName() string {
	return "stats.broadcastRevenueStats"
}

// TypeInfo returns info about TL type.
func (b *StatsBroadcastRevenueStats) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stats.broadcastRevenueStats",
		ID:   StatsBroadcastRevenueStatsTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TopHoursGraph",
			SchemaName: "top_hours_graph",
		},
		{
			Name:       "RevenueGraph",
			SchemaName: "revenue_graph",
		},
		{
			Name:       "Balances",
			SchemaName: "balances",
		},
		{
			Name:       "UsdRate",
			SchemaName: "usd_rate",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *StatsBroadcastRevenueStats) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode stats.broadcastRevenueStats#5407e297 as nil")
	}
	buf.PutID(StatsBroadcastRevenueStatsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *StatsBroadcastRevenueStats) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode stats.broadcastRevenueStats#5407e297 as nil")
	}
	if b.TopHoursGraph == nil {
		return fmt.Errorf("unable to encode stats.broadcastRevenueStats#5407e297: field top_hours_graph is nil")
	}
	if err := b.TopHoursGraph.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode stats.broadcastRevenueStats#5407e297: field top_hours_graph: %w", err)
	}
	if b.RevenueGraph == nil {
		return fmt.Errorf("unable to encode stats.broadcastRevenueStats#5407e297: field revenue_graph is nil")
	}
	if err := b.RevenueGraph.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode stats.broadcastRevenueStats#5407e297: field revenue_graph: %w", err)
	}
	if err := b.Balances.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode stats.broadcastRevenueStats#5407e297: field balances: %w", err)
	}
	buf.PutDouble(b.UsdRate)
	return nil
}

// Decode implements bin.Decoder.
func (b *StatsBroadcastRevenueStats) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode stats.broadcastRevenueStats#5407e297 to nil")
	}
	if err := buf.ConsumeID(StatsBroadcastRevenueStatsTypeID); err != nil {
		return fmt.Errorf("unable to decode stats.broadcastRevenueStats#5407e297: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *StatsBroadcastRevenueStats) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode stats.broadcastRevenueStats#5407e297 to nil")
	}
	{
		value, err := DecodeStatsGraph(buf)
		if err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueStats#5407e297: field top_hours_graph: %w", err)
		}
		b.TopHoursGraph = value
	}
	{
		value, err := DecodeStatsGraph(buf)
		if err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueStats#5407e297: field revenue_graph: %w", err)
		}
		b.RevenueGraph = value
	}
	{
		if err := b.Balances.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueStats#5407e297: field balances: %w", err)
		}
	}
	{
		value, err := buf.Double()
		if err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueStats#5407e297: field usd_rate: %w", err)
		}
		b.UsdRate = value
	}
	return nil
}

// GetTopHoursGraph returns value of TopHoursGraph field.
func (b *StatsBroadcastRevenueStats) GetTopHoursGraph() (value StatsGraphClass) {
	if b == nil {
		return
	}
	return b.TopHoursGraph
}

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

// GetBalances returns value of Balances field.
func (b *StatsBroadcastRevenueStats) GetBalances() (value BroadcastRevenueBalances) {
	if b == nil {
		return
	}
	return b.Balances
}

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