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

// PaymentsUpgradeStarGiftRequest represents TL type `payments.upgradeStarGift#aed6e4f5`.
// Upgrade a gift¹ to a collectible gift²: can only be used if the upgrade was already
// paid by the gift sender; see here »³ for more info on the full flow (including the
// different flow to use in case the upgrade was not paid by the gift sender).
//
// Links:
//  1. https://core.telegram.org/api/gifts
//  2. https://core.telegram.org/api/gifts#collectible-gifts
//  3. https://core.telegram.org/api/gifts#upgrade-a-gift-to-a-collectible-gift
//
// See https://core.telegram.org/method/payments.upgradeStarGift for reference.
type PaymentsUpgradeStarGiftRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Set this flag to keep the original gift text, sender and receiver in the upgraded gift
	// as a starGiftAttributeOriginalDetails¹ attribute.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/starGiftAttributeOriginalDetails
	KeepOriginalDetails bool
	// The gift to upgrade
	Stargift InputSavedStarGiftClass
}

// PaymentsUpgradeStarGiftRequestTypeID is TL type id of PaymentsUpgradeStarGiftRequest.
const PaymentsUpgradeStarGiftRequestTypeID = 0xaed6e4f5

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

func (u *PaymentsUpgradeStarGiftRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.KeepOriginalDetails == false) {
		return false
	}
	if !(u.Stargift == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsUpgradeStarGiftRequest from given interface.
func (u *PaymentsUpgradeStarGiftRequest) FillFrom(from interface {
	GetKeepOriginalDetails() (value bool)
	GetStargift() (value InputSavedStarGiftClass)
}) {
	u.KeepOriginalDetails = from.GetKeepOriginalDetails()
	u.Stargift = from.GetStargift()
}

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

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

// TypeInfo returns info about TL type.
func (u *PaymentsUpgradeStarGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.upgradeStarGift",
		ID:   PaymentsUpgradeStarGiftRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "KeepOriginalDetails",
			SchemaName: "keep_original_details",
			Null:       !u.Flags.Has(0),
		},
		{
			Name:       "Stargift",
			SchemaName: "stargift",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *PaymentsUpgradeStarGiftRequest) SetFlags() {
	if !(u.KeepOriginalDetails == false) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *PaymentsUpgradeStarGiftRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode payments.upgradeStarGift#aed6e4f5 as nil")
	}
	b.PutID(PaymentsUpgradeStarGiftRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *PaymentsUpgradeStarGiftRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode payments.upgradeStarGift#aed6e4f5 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.upgradeStarGift#aed6e4f5: field flags: %w", err)
	}
	if u.Stargift == nil {
		return fmt.Errorf("unable to encode payments.upgradeStarGift#aed6e4f5: field stargift is nil")
	}
	if err := u.Stargift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.upgradeStarGift#aed6e4f5: field stargift: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *PaymentsUpgradeStarGiftRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode payments.upgradeStarGift#aed6e4f5 to nil")
	}
	if err := b.ConsumeID(PaymentsUpgradeStarGiftRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.upgradeStarGift#aed6e4f5: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *PaymentsUpgradeStarGiftRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode payments.upgradeStarGift#aed6e4f5 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.upgradeStarGift#aed6e4f5: field flags: %w", err)
		}
	}
	u.KeepOriginalDetails = u.Flags.Has(0)
	{
		value, err := DecodeInputSavedStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.upgradeStarGift#aed6e4f5: field stargift: %w", err)
		}
		u.Stargift = value
	}
	return nil
}

// SetKeepOriginalDetails sets value of KeepOriginalDetails conditional field.
func (u *PaymentsUpgradeStarGiftRequest) SetKeepOriginalDetails(value bool) {
	if value {
		u.Flags.Set(0)
		u.KeepOriginalDetails = true
	} else {
		u.Flags.Unset(0)
		u.KeepOriginalDetails = false
	}
}

// GetKeepOriginalDetails returns value of KeepOriginalDetails conditional field.
func (u *PaymentsUpgradeStarGiftRequest) GetKeepOriginalDetails() (value bool) {
	if u == nil {
		return
	}
	return u.Flags.Has(0)
}

// GetStargift returns value of Stargift field.
func (u *PaymentsUpgradeStarGiftRequest) GetStargift() (value InputSavedStarGiftClass) {
	if u == nil {
		return
	}
	return u.Stargift
}

// PaymentsUpgradeStarGift invokes method payments.upgradeStarGift#aed6e4f5 returning error if any.
// Upgrade a gift¹ to a collectible gift²: can only be used if the upgrade was already
// paid by the gift sender; see here »³ for more info on the full flow (including the
// different flow to use in case the upgrade was not paid by the gift sender).
//
// Links:
//  1. https://core.telegram.org/api/gifts
//  2. https://core.telegram.org/api/gifts#collectible-gifts
//  3. https://core.telegram.org/api/gifts#upgrade-a-gift-to-a-collectible-gift
//
// Possible errors:
//
//	400 BUSINESS_CONNECTION_INVALID: The connection_id passed to the wrapping invokeWithBusinessConnection call is invalid.
//	400 MESSAGE_ID_INVALID: The provided message id is invalid.
//	400 PAYMENT_REQUIRED: Payment is required for this action, see here » for more info.
//	400 SAVED_ID_EMPTY: The passed inputSavedStarGiftChat.saved_id is empty.
//	400 STARGIFT_ALREADY_CONVERTED: The specified star gift was already converted to Stars.
//	400 STARGIFT_ALREADY_UPGRADED: The specified gift was already upgraded to a collectible gift.
//	400 STARGIFT_UPGRADE_UNAVAILABLE: A received gift can only be upgraded to a collectible gift if the messageActionStarGift/savedStarGift.can_upgrade flag is set.
//
// See https://core.telegram.org/method/payments.upgradeStarGift for reference.
func (c *Client) PaymentsUpgradeStarGift(ctx context.Context, request *PaymentsUpgradeStarGiftRequest) (UpdatesClass, error) {
	var result UpdatesBox

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