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

// PaymentsGetStarsRevenueWithdrawalURLRequest represents TL type `payments.getStarsRevenueWithdrawalUrl#13bbe8b3`.
// Withdraw funds from a channel or bot's star balance »¹.
//
// Links:
//  1. https://core.telegram.org/api/stars#withdrawing-revenue
//
// See https://core.telegram.org/method/payments.getStarsRevenueWithdrawalUrl for reference.
type PaymentsGetStarsRevenueWithdrawalURLRequest struct {
	// Channel or bot from which to withdraw funds.
	Peer InputPeerClass
	// Amount of stars to withdraw.
	Stars int64
	// 2FA password, see here »¹ for more info.
	//
	// Links:
	//  1) https://core.telegram.org/api/srp#using-the-2fa-password
	Password InputCheckPasswordSRPClass
}

// PaymentsGetStarsRevenueWithdrawalURLRequestTypeID is TL type id of PaymentsGetStarsRevenueWithdrawalURLRequest.
const PaymentsGetStarsRevenueWithdrawalURLRequestTypeID = 0x13bbe8b3

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

func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Stars == 0) {
		return false
	}
	if !(g.Password == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetStarsRevenueWithdrawalURLRequest from given interface.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetStars() (value int64)
	GetPassword() (value InputCheckPasswordSRPClass)
}) {
	g.Peer = from.GetPeer()
	g.Stars = from.GetStars()
	g.Password = from.GetPassword()
}

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

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

// TypeInfo returns info about TL type.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getStarsRevenueWithdrawalUrl",
		ID:   PaymentsGetStarsRevenueWithdrawalURLRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Stars",
			SchemaName: "stars",
		},
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsRevenueWithdrawalUrl#13bbe8b3 as nil")
	}
	b.PutID(PaymentsGetStarsRevenueWithdrawalURLRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsRevenueWithdrawalUrl#13bbe8b3 as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field peer: %w", err)
	}
	b.PutLong(g.Stars)
	if g.Password == nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field password is nil")
	}
	if err := g.Password.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field password: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarsRevenueWithdrawalUrl#13bbe8b3 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field stars: %w", err)
		}
		g.Stars = value
	}
	{
		value, err := DecodeInputCheckPasswordSRP(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#13bbe8b3: field password: %w", err)
		}
		g.Password = value
	}
	return nil
}

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

// GetStars returns value of Stars field.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) GetStars() (value int64) {
	if g == nil {
		return
	}
	return g.Stars
}

// GetPassword returns value of Password field.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) GetPassword() (value InputCheckPasswordSRPClass) {
	if g == nil {
		return
	}
	return g.Password
}

// GetPasswordAsNotEmpty returns mapped value of Password field.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) GetPasswordAsNotEmpty() (*InputCheckPasswordSRP, bool) {
	return g.Password.AsNotEmpty()
}

// PaymentsGetStarsRevenueWithdrawalURL invokes method payments.getStarsRevenueWithdrawalUrl#13bbe8b3 returning error if any.
// Withdraw funds from a channel or bot's star balance »¹.
//
// Links:
//  1. https://core.telegram.org/api/stars#withdrawing-revenue
//
// Possible errors:
//
//	400 PASSWORD_HASH_INVALID: The provided password hash is invalid.
//	400 PASSWORD_MISSING: You must enable 2FA before executing this operation.
//	400 PASSWORD_TOO_FRESH_%d: The password was modified less than 24 hours ago, try again in %d seconds.
//
// See https://core.telegram.org/method/payments.getStarsRevenueWithdrawalUrl for reference.
func (c *Client) PaymentsGetStarsRevenueWithdrawalURL(ctx context.Context, request *PaymentsGetStarsRevenueWithdrawalURLRequest) (*PaymentsStarsRevenueWithdrawalURL, error) {
	var result PaymentsStarsRevenueWithdrawalURL

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