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

// PaymentsGetStarsStatusRequest represents TL type `payments.getStarsStatus#4ea9b3bf`.
// Get the current Telegram Stars balance¹ of the current account (with
// peer=inputPeerSelf²), or the stars balance of the bot specified in peer.
//
// Links:
//  1. https://core.telegram.org/api/stars
//  2. https://core.telegram.org/constructor/inputPeerSelf
//
// See https://core.telegram.org/method/payments.getStarsStatus for reference.
type PaymentsGetStarsStatusRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, returns the channel/ad revenue balance in nanotons.
	Ton bool
	// Peer of which to get the balance.
	Peer InputPeerClass
}

// PaymentsGetStarsStatusRequestTypeID is TL type id of PaymentsGetStarsStatusRequest.
const PaymentsGetStarsStatusRequestTypeID = 0x4ea9b3bf

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

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

	return true
}

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

// FillFrom fills PaymentsGetStarsStatusRequest from given interface.
func (g *PaymentsGetStarsStatusRequest) FillFrom(from interface {
	GetTon() (value bool)
	GetPeer() (value InputPeerClass)
}) {
	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 (*PaymentsGetStarsStatusRequest) TypeID() uint32 {
	return PaymentsGetStarsStatusRequestTypeID
}

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

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

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

// Encode implements bin.Encoder.
func (g *PaymentsGetStarsStatusRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsStatus#4ea9b3bf as nil")
	}
	b.PutID(PaymentsGetStarsStatusRequestTypeID)
	return g.EncodeBare(b)
}

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

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

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

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

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

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

// PaymentsGetStarsStatus invokes method payments.getStarsStatus#4ea9b3bf returning error if any.
// Get the current Telegram Stars balance¹ of the current account (with
// peer=inputPeerSelf²), or the stars balance of the bot specified in peer.
//
// Links:
//  1. https://core.telegram.org/api/stars
//  2. https://core.telegram.org/constructor/inputPeerSelf
//
// Possible errors:
//
//	403 BOT_ACCESS_FORBIDDEN: The specified method can be used over a business connection for some operations, but the specified query attempted an operation that is not allowed over a business connection.
//	400 BUSINESS_CONNECTION_INVALID: The connection_id passed to the wrapping invokeWithBusinessConnection call is invalid.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.getStarsStatus for reference.
func (c *Client) PaymentsGetStarsStatus(ctx context.Context, request *PaymentsGetStarsStatusRequest) (*PaymentsStarsStatus, error) {
	var result PaymentsStarsStatus

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