// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// StarRevenueStatistics represents TL type `starRevenueStatistics#bd2d9257`.
type StarRevenueStatistics struct {
	// A graph containing amount of revenue in a given day
	RevenueByDayGraph StatisticalGraphClass
	// Telegram Star revenue status
	Status StarRevenueStatus
	// Current conversion rate of a Telegram Star to USD
	UsdRate float64
}

// StarRevenueStatisticsTypeID is TL type id of StarRevenueStatistics.
const StarRevenueStatisticsTypeID = 0xbd2d9257

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

func (s *StarRevenueStatistics) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.RevenueByDayGraph == nil) {
		return false
	}
	if !(s.Status.Zero()) {
		return false
	}
	if !(s.UsdRate == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarRevenueStatistics) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starRevenueStatistics",
		ID:   StarRevenueStatisticsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "RevenueByDayGraph",
			SchemaName: "revenue_by_day_graph",
		},
		{
			Name:       "Status",
			SchemaName: "status",
		},
		{
			Name:       "UsdRate",
			SchemaName: "usd_rate",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarRevenueStatistics) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starRevenueStatistics#bd2d9257 as nil")
	}
	b.PutID(StarRevenueStatisticsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarRevenueStatistics) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starRevenueStatistics#bd2d9257 as nil")
	}
	if s.RevenueByDayGraph == nil {
		return fmt.Errorf("unable to encode starRevenueStatistics#bd2d9257: field revenue_by_day_graph is nil")
	}
	if err := s.RevenueByDayGraph.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starRevenueStatistics#bd2d9257: field revenue_by_day_graph: %w", err)
	}
	if err := s.Status.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starRevenueStatistics#bd2d9257: field status: %w", err)
	}
	b.PutDouble(s.UsdRate)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarRevenueStatistics) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starRevenueStatistics#bd2d9257 to nil")
	}
	{
		value, err := DecodeStatisticalGraph(b)
		if err != nil {
			return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: field revenue_by_day_graph: %w", err)
		}
		s.RevenueByDayGraph = value
	}
	{
		if err := s.Status.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: field status: %w", err)
		}
	}
	{
		value, err := b.Double()
		if err != nil {
			return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: field usd_rate: %w", err)
		}
		s.UsdRate = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarRevenueStatistics) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starRevenueStatistics#bd2d9257 as nil")
	}
	b.ObjStart()
	b.PutID("starRevenueStatistics")
	b.Comma()
	b.FieldStart("revenue_by_day_graph")
	if s.RevenueByDayGraph == nil {
		return fmt.Errorf("unable to encode starRevenueStatistics#bd2d9257: field revenue_by_day_graph is nil")
	}
	if err := s.RevenueByDayGraph.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode starRevenueStatistics#bd2d9257: field revenue_by_day_graph: %w", err)
	}
	b.Comma()
	b.FieldStart("status")
	if err := s.Status.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode starRevenueStatistics#bd2d9257: field status: %w", err)
	}
	b.Comma()
	b.FieldStart("usd_rate")
	b.PutDouble(s.UsdRate)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StarRevenueStatistics) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode starRevenueStatistics#bd2d9257 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starRevenueStatistics"); err != nil {
				return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: %w", err)
			}
		case "revenue_by_day_graph":
			value, err := DecodeTDLibJSONStatisticalGraph(b)
			if err != nil {
				return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: field revenue_by_day_graph: %w", err)
			}
			s.RevenueByDayGraph = value
		case "status":
			if err := s.Status.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: field status: %w", err)
			}
		case "usd_rate":
			value, err := b.Double()
			if err != nil {
				return fmt.Errorf("unable to decode starRevenueStatistics#bd2d9257: field usd_rate: %w", err)
			}
			s.UsdRate = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetRevenueByDayGraph returns value of RevenueByDayGraph field.
func (s *StarRevenueStatistics) GetRevenueByDayGraph() (value StatisticalGraphClass) {
	if s == nil {
		return
	}
	return s.RevenueByDayGraph
}

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

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