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

// StatsGetBroadcastRevenueWithdrawalURLRequest represents TL type `stats.getBroadcastRevenueWithdrawalUrl#9df4faad`.
// Withdraw funds from a channel's ad revenue balance »¹.
//
// Links:
//  1. https://core.telegram.org/api/revenue
//
// See https://core.telegram.org/method/stats.getBroadcastRevenueWithdrawalUrl for reference.
type StatsGetBroadcastRevenueWithdrawalURLRequest struct {
	// Get ad revenue withdrawal URL for the specified channel or bot
	Peer InputPeerClass
	// 2FA password, see here »¹ for more info.
	//
	// Links:
	//  1) https://core.telegram.org/api/srp#using-the-2fa-password
	Password InputCheckPasswordSRPClass
}

// StatsGetBroadcastRevenueWithdrawalURLRequestTypeID is TL type id of StatsGetBroadcastRevenueWithdrawalURLRequest.
const StatsGetBroadcastRevenueWithdrawalURLRequestTypeID = 0x9df4faad

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

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

	return true
}

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

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

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

// TypeName returns name of type in TL schema.
func (*StatsGetBroadcastRevenueWithdrawalURLRequest) TypeName() string {
	return "stats.getBroadcastRevenueWithdrawalUrl"
}

// TypeInfo returns info about TL type.
func (g *StatsGetBroadcastRevenueWithdrawalURLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stats.getBroadcastRevenueWithdrawalUrl",
		ID:   StatsGetBroadcastRevenueWithdrawalURLRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *StatsGetBroadcastRevenueWithdrawalURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stats.getBroadcastRevenueWithdrawalUrl#9df4faad as nil")
	}
	b.PutID(StatsGetBroadcastRevenueWithdrawalURLRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StatsGetBroadcastRevenueWithdrawalURLRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stats.getBroadcastRevenueWithdrawalUrl#9df4faad as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: field peer: %w", err)
	}
	if g.Password == nil {
		return fmt.Errorf("unable to encode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: field password is nil")
	}
	if err := g.Password.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: field password: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *StatsGetBroadcastRevenueWithdrawalURLRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stats.getBroadcastRevenueWithdrawalUrl#9df4faad to nil")
	}
	if err := b.ConsumeID(StatsGetBroadcastRevenueWithdrawalURLRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *StatsGetBroadcastRevenueWithdrawalURLRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stats.getBroadcastRevenueWithdrawalUrl#9df4faad to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := DecodeInputCheckPasswordSRP(b)
		if err != nil {
			return fmt.Errorf("unable to decode stats.getBroadcastRevenueWithdrawalUrl#9df4faad: field password: %w", err)
		}
		g.Password = value
	}
	return nil
}

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

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

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

// StatsGetBroadcastRevenueWithdrawalURL invokes method stats.getBroadcastRevenueWithdrawalUrl#9df4faad returning error if any.
// Withdraw funds from a channel's ad revenue balance »¹.
//
// Links:
//  1. https://core.telegram.org/api/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/stats.getBroadcastRevenueWithdrawalUrl for reference.
func (c *Client) StatsGetBroadcastRevenueWithdrawalURL(ctx context.Context, request *StatsGetBroadcastRevenueWithdrawalURLRequest) (*StatsBroadcastRevenueWithdrawalURL, error) {
	var result StatsBroadcastRevenueWithdrawalURL

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