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

// PaymentsBotCancelStarsSubscriptionRequest represents TL type `payments.botCancelStarsSubscription#6dfa0622`.
// Cancel a bot subscription¹
//
// Links:
//  1. https://core.telegram.org/api/subscriptions#bot-subscriptions
//
// See https://core.telegram.org/method/payments.botCancelStarsSubscription for reference.
type PaymentsBotCancelStarsSubscriptionRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If not set, disables autorenewal of the subscriptions, and prevents the user from
	// reactivating the subscription once the current period expires: a subscription
	// cancelled by the bot will have the starsSubscription¹.bot_canceled flag set.  The bot
	// can can partially undo this operation by setting this flag: this will allow the user
	// to reactivate the subscription.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/starsSubscription
	Restore bool
	// The ID of the user whose subscription should be (un)cancelled
	UserID InputUserClass
	// The provider_charge_id from the messageActionPaymentSentMe¹ service message sent to
	// the bot for the first subscription payment.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/messageActionPaymentSentMe
	ChargeID string
}

// PaymentsBotCancelStarsSubscriptionRequestTypeID is TL type id of PaymentsBotCancelStarsSubscriptionRequest.
const PaymentsBotCancelStarsSubscriptionRequestTypeID = 0x6dfa0622

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

func (b *PaymentsBotCancelStarsSubscriptionRequest) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.Restore == false) {
		return false
	}
	if !(b.UserID == nil) {
		return false
	}
	if !(b.ChargeID == "") {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsBotCancelStarsSubscriptionRequest from given interface.
func (b *PaymentsBotCancelStarsSubscriptionRequest) FillFrom(from interface {
	GetRestore() (value bool)
	GetUserID() (value InputUserClass)
	GetChargeID() (value string)
}) {
	b.Restore = from.GetRestore()
	b.UserID = from.GetUserID()
	b.ChargeID = from.GetChargeID()
}

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

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

// TypeInfo returns info about TL type.
func (b *PaymentsBotCancelStarsSubscriptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.botCancelStarsSubscription",
		ID:   PaymentsBotCancelStarsSubscriptionRequestTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Restore",
			SchemaName: "restore",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "ChargeID",
			SchemaName: "charge_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *PaymentsBotCancelStarsSubscriptionRequest) SetFlags() {
	if !(b.Restore == false) {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *PaymentsBotCancelStarsSubscriptionRequest) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode payments.botCancelStarsSubscription#6dfa0622 as nil")
	}
	buf.PutID(PaymentsBotCancelStarsSubscriptionRequestTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *PaymentsBotCancelStarsSubscriptionRequest) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode payments.botCancelStarsSubscription#6dfa0622 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode payments.botCancelStarsSubscription#6dfa0622: field flags: %w", err)
	}
	if b.UserID == nil {
		return fmt.Errorf("unable to encode payments.botCancelStarsSubscription#6dfa0622: field user_id is nil")
	}
	if err := b.UserID.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode payments.botCancelStarsSubscription#6dfa0622: field user_id: %w", err)
	}
	buf.PutString(b.ChargeID)
	return nil
}

// Decode implements bin.Decoder.
func (b *PaymentsBotCancelStarsSubscriptionRequest) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode payments.botCancelStarsSubscription#6dfa0622 to nil")
	}
	if err := buf.ConsumeID(PaymentsBotCancelStarsSubscriptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.botCancelStarsSubscription#6dfa0622: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *PaymentsBotCancelStarsSubscriptionRequest) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode payments.botCancelStarsSubscription#6dfa0622 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode payments.botCancelStarsSubscription#6dfa0622: field flags: %w", err)
		}
	}
	b.Restore = b.Flags.Has(0)
	{
		value, err := DecodeInputUser(buf)
		if err != nil {
			return fmt.Errorf("unable to decode payments.botCancelStarsSubscription#6dfa0622: field user_id: %w", err)
		}
		b.UserID = value
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.botCancelStarsSubscription#6dfa0622: field charge_id: %w", err)
		}
		b.ChargeID = value
	}
	return nil
}

// SetRestore sets value of Restore conditional field.
func (b *PaymentsBotCancelStarsSubscriptionRequest) SetRestore(value bool) {
	if value {
		b.Flags.Set(0)
		b.Restore = true
	} else {
		b.Flags.Unset(0)
		b.Restore = false
	}
}

// GetRestore returns value of Restore conditional field.
func (b *PaymentsBotCancelStarsSubscriptionRequest) GetRestore() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(0)
}

// GetUserID returns value of UserID field.
func (b *PaymentsBotCancelStarsSubscriptionRequest) GetUserID() (value InputUserClass) {
	if b == nil {
		return
	}
	return b.UserID
}

// GetChargeID returns value of ChargeID field.
func (b *PaymentsBotCancelStarsSubscriptionRequest) GetChargeID() (value string) {
	if b == nil {
		return
	}
	return b.ChargeID
}

// PaymentsBotCancelStarsSubscription invokes method payments.botCancelStarsSubscription#6dfa0622 returning error if any.
// Cancel a bot subscription¹
//
// Links:
//  1. https://core.telegram.org/api/subscriptions#bot-subscriptions
//
// Possible errors:
//
//	400 CHARGE_ID_INVALID: The specified charge_id is invalid.
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/payments.botCancelStarsSubscription for reference.
func (c *Client) PaymentsBotCancelStarsSubscription(ctx context.Context, request *PaymentsBotCancelStarsSubscriptionRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
