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

// PaymentsGetStarsSubscriptionsRequest represents TL type `payments.getStarsSubscriptions#32512c5`.
// Obtain a list of active, expired or cancelled Telegram Star subscriptions »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// See https://core.telegram.org/method/payments.getStarsSubscriptions for reference.
type PaymentsGetStarsSubscriptionsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to return only subscriptions expired due to an excessively low Telegram Star
	// balance.
	MissingBalance bool
	// Always pass inputPeerSelf¹.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputPeerSelf
	Peer InputPeerClass
	// Offset for pagination, taken from payments.starsStatus¹.subscriptions_next_offset.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/payments.starsStatus
	Offset string
}

// PaymentsGetStarsSubscriptionsRequestTypeID is TL type id of PaymentsGetStarsSubscriptionsRequest.
const PaymentsGetStarsSubscriptionsRequestTypeID = 0x32512c5

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

func (g *PaymentsGetStarsSubscriptionsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.MissingBalance == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetStarsSubscriptionsRequest from given interface.
func (g *PaymentsGetStarsSubscriptionsRequest) FillFrom(from interface {
	GetMissingBalance() (value bool)
	GetPeer() (value InputPeerClass)
	GetOffset() (value string)
}) {
	g.MissingBalance = from.GetMissingBalance()
	g.Peer = from.GetPeer()
	g.Offset = from.GetOffset()
}

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

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

// TypeInfo returns info about TL type.
func (g *PaymentsGetStarsSubscriptionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getStarsSubscriptions",
		ID:   PaymentsGetStarsSubscriptionsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MissingBalance",
			SchemaName: "missing_balance",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (g *PaymentsGetStarsSubscriptionsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsSubscriptions#32512c5 as nil")
	}
	b.PutID(PaymentsGetStarsSubscriptionsRequestTypeID)
	return g.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetStarsSubscriptionsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarsSubscriptions#32512c5 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getStarsSubscriptions#32512c5: field flags: %w", err)
		}
	}
	g.MissingBalance = g.Flags.Has(0)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsSubscriptions#32512c5: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsSubscriptions#32512c5: field offset: %w", err)
		}
		g.Offset = value
	}
	return nil
}

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

// GetMissingBalance returns value of MissingBalance conditional field.
func (g *PaymentsGetStarsSubscriptionsRequest) GetMissingBalance() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

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

// GetOffset returns value of Offset field.
func (g *PaymentsGetStarsSubscriptionsRequest) GetOffset() (value string) {
	if g == nil {
		return
	}
	return g.Offset
}

// PaymentsGetStarsSubscriptions invokes method payments.getStarsSubscriptions#32512c5 returning error if any.
// Obtain a list of active, expired or cancelled Telegram Star subscriptions »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.getStarsSubscriptions for reference.
func (c *Client) PaymentsGetStarsSubscriptions(ctx context.Context, request *PaymentsGetStarsSubscriptionsRequest) (*PaymentsStarsStatus, error) {
	var result PaymentsStarsStatus

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