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

// PaymentsGetStarsRevenueStatsRequest represents TL type `payments.getStarsRevenueStats#d91ffad6`.
// Get Telegram Star revenue statistics »¹.
//
// Links:
//  1. https://core.telegram.org/api/stars
//
// See https://core.telegram.org/method/payments.getStarsRevenueStats for reference.
type PaymentsGetStarsRevenueStatsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to enable dark theme for graph colors
	Dark bool
	// If set, fetches channel/bot ad revenue statistics in TON.
	Ton bool
	// Get statistics for the specified bot, channel or ourselves (inputPeerSelf¹).
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputPeerSelf
	Peer InputPeerClass
}

// PaymentsGetStarsRevenueStatsRequestTypeID is TL type id of PaymentsGetStarsRevenueStatsRequest.
const PaymentsGetStarsRevenueStatsRequestTypeID = 0xd91ffad6

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

func (g *PaymentsGetStarsRevenueStatsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Dark == false) {
		return false
	}
	if !(g.Ton == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetStarsRevenueStatsRequest from given interface.
func (g *PaymentsGetStarsRevenueStatsRequest) FillFrom(from interface {
	GetDark() (value bool)
	GetTon() (value bool)
	GetPeer() (value InputPeerClass)
}) {
	g.Dark = from.GetDark()
	g.Ton = from.GetTon()
	g.Peer = from.GetPeer()
}

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

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

// TypeInfo returns info about TL type.
func (g *PaymentsGetStarsRevenueStatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getStarsRevenueStats",
		ID:   PaymentsGetStarsRevenueStatsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Dark",
			SchemaName: "dark",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "Ton",
			SchemaName: "ton",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *PaymentsGetStarsRevenueStatsRequest) SetFlags() {
	if !(g.Dark == false) {
		g.Flags.Set(0)
	}
	if !(g.Ton == false) {
		g.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (g *PaymentsGetStarsRevenueStatsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsRevenueStats#d91ffad6 as nil")
	}
	b.PutID(PaymentsGetStarsRevenueStatsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetStarsRevenueStatsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsRevenueStats#d91ffad6 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueStats#d91ffad6: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueStats#d91ffad6: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueStats#d91ffad6: field peer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *PaymentsGetStarsRevenueStatsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarsRevenueStats#d91ffad6 to nil")
	}
	if err := b.ConsumeID(PaymentsGetStarsRevenueStatsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.getStarsRevenueStats#d91ffad6: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetStarsRevenueStatsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarsRevenueStats#d91ffad6 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueStats#d91ffad6: field flags: %w", err)
		}
	}
	g.Dark = g.Flags.Has(0)
	g.Ton = g.Flags.Has(1)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueStats#d91ffad6: field peer: %w", err)
		}
		g.Peer = value
	}
	return nil
}

// SetDark sets value of Dark conditional field.
func (g *PaymentsGetStarsRevenueStatsRequest) SetDark(value bool) {
	if value {
		g.Flags.Set(0)
		g.Dark = true
	} else {
		g.Flags.Unset(0)
		g.Dark = false
	}
}

// GetDark returns value of Dark conditional field.
func (g *PaymentsGetStarsRevenueStatsRequest) GetDark() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

// SetTon sets value of Ton conditional field.
func (g *PaymentsGetStarsRevenueStatsRequest) SetTon(value bool) {
	if value {
		g.Flags.Set(1)
		g.Ton = true
	} else {
		g.Flags.Unset(1)
		g.Ton = false
	}
}

// GetTon returns value of Ton conditional field.
func (g *PaymentsGetStarsRevenueStatsRequest) GetTon() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(1)
}

// GetPeer returns value of Peer field.
func (g *PaymentsGetStarsRevenueStatsRequest) GetPeer() (value InputPeerClass) {
	if g == nil {
		return
	}
	return g.Peer
}

// PaymentsGetStarsRevenueStats invokes method payments.getStarsRevenueStats#d91ffad6 returning error if any.
// Get Telegram Star revenue statistics »¹.
//
// Links:
//  1. https://core.telegram.org/api/stars
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.getStarsRevenueStats for reference.
func (c *Client) PaymentsGetStarsRevenueStats(ctx context.Context, request *PaymentsGetStarsRevenueStatsRequest) (*PaymentsStarsRevenueStats, error) {
	var result PaymentsStarsRevenueStats

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
