// 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#2433dc92`.
// 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 {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, withdraws channel/ad revenue in TON.
	Ton bool
	// Channel or bot from which to withdraw funds.
	Peer InputPeerClass
	// The amount of stars or nanotons to withdraw.
	//
	// Use SetAmount and GetAmount helpers.
	Amount 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 = 0x2433dc92

// 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.Flags.Zero()) {
		return false
	}
	if !(g.Ton == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Amount == 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 {
	GetTon() (value bool)
	GetPeer() (value InputPeerClass)
	GetAmount() (value int64, ok bool)
	GetPassword() (value InputCheckPasswordSRPClass)
}) {
	g.Ton = from.GetTon()
	g.Peer = from.GetPeer()
	if val, ok := from.GetAmount(); ok {
		g.Amount = val
	}

	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:       "Ton",
			SchemaName: "ton",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsRevenueWithdrawalUrl#2433dc92 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#2433dc92 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#2433dc92: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#2433dc92: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#2433dc92: field peer: %w", err)
	}
	if g.Flags.Has(1) {
		b.PutLong(g.Amount)
	}
	if g.Password == nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#2433dc92: field password is nil")
	}
	if err := g.Password.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsRevenueWithdrawalUrl#2433dc92: 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#2433dc92 to nil")
	}
	if err := b.ConsumeID(PaymentsGetStarsRevenueWithdrawalURLRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#2433dc92: %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#2433dc92 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#2433dc92: field flags: %w", err)
		}
	}
	g.Ton = g.Flags.Has(0)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#2433dc92: field peer: %w", err)
		}
		g.Peer = value
	}
	if g.Flags.Has(1) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#2433dc92: field amount: %w", err)
		}
		g.Amount = value
	}
	{
		value, err := DecodeInputCheckPasswordSRP(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsRevenueWithdrawalUrl#2433dc92: field password: %w", err)
		}
		g.Password = value
	}
	return nil
}

// SetTon sets value of Ton conditional field.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) 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 *PaymentsGetStarsRevenueWithdrawalURLRequest) GetTon() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

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

// SetAmount sets value of Amount conditional field.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) SetAmount(value int64) {
	g.Flags.Set(1)
	g.Amount = value
}

// GetAmount returns value of Amount conditional field and
// boolean which is true if field was set.
func (g *PaymentsGetStarsRevenueWithdrawalURLRequest) GetAmount() (value int64, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(1) {
		return value, false
	}
	return g.Amount, true
}

// 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#2433dc92 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.
//	400 SESSION_TOO_FRESH_%d: This session was created 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
}
