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

// PaymentsConvertStarGiftRequest represents TL type `payments.convertStarGift#74bf076b`.
// Convert a received gift »¹ into Telegram Stars: this will permanently destroy the
// gift, converting it into starGift².convert_stars Telegram Stars³, added to the
// user's balance.
// Note that starGift¹.convert_stars will be less than the buying price (starGift²
// stars) of the gift if it was originally bought using Telegram Stars bought a long time
// ago.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//  2. https://core.telegram.org/constructor/starGift
//  3. https://core.telegram.org/api/stars
//  4. https://core.telegram.org/constructor/starGift
//  5. https://core.telegram.org/constructor/starGift
//
// See https://core.telegram.org/method/payments.convertStarGift for reference.
type PaymentsConvertStarGiftRequest struct {
	// The gift to convert.
	Stargift InputSavedStarGiftClass
}

// PaymentsConvertStarGiftRequestTypeID is TL type id of PaymentsConvertStarGiftRequest.
const PaymentsConvertStarGiftRequestTypeID = 0x74bf076b

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

func (c *PaymentsConvertStarGiftRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Stargift == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsConvertStarGiftRequest from given interface.
func (c *PaymentsConvertStarGiftRequest) FillFrom(from interface {
	GetStargift() (value InputSavedStarGiftClass)
}) {
	c.Stargift = from.GetStargift()
}

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

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

// TypeInfo returns info about TL type.
func (c *PaymentsConvertStarGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.convertStarGift",
		ID:   PaymentsConvertStarGiftRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Stargift",
			SchemaName: "stargift",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *PaymentsConvertStarGiftRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.convertStarGift#74bf076b as nil")
	}
	b.PutID(PaymentsConvertStarGiftRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PaymentsConvertStarGiftRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.convertStarGift#74bf076b as nil")
	}
	if c.Stargift == nil {
		return fmt.Errorf("unable to encode payments.convertStarGift#74bf076b: field stargift is nil")
	}
	if err := c.Stargift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.convertStarGift#74bf076b: field stargift: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *PaymentsConvertStarGiftRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.convertStarGift#74bf076b to nil")
	}
	if err := b.ConsumeID(PaymentsConvertStarGiftRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.convertStarGift#74bf076b: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *PaymentsConvertStarGiftRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.convertStarGift#74bf076b to nil")
	}
	{
		value, err := DecodeInputSavedStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.convertStarGift#74bf076b: field stargift: %w", err)
		}
		c.Stargift = value
	}
	return nil
}

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

// PaymentsConvertStarGift invokes method payments.convertStarGift#74bf076b returning error if any.
// Convert a received gift »¹ into Telegram Stars: this will permanently destroy the
// gift, converting it into starGift².convert_stars Telegram Stars³, added to the
// user's balance.
// Note that starGift¹.convert_stars will be less than the buying price (starGift²
// stars) of the gift if it was originally bought using Telegram Stars bought a long time
// ago.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//  2. https://core.telegram.org/constructor/starGift
//  3. https://core.telegram.org/api/stars
//  4. https://core.telegram.org/constructor/starGift
//  5. https://core.telegram.org/constructor/starGift
//
// 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 SAVED_ID_EMPTY: The passed inputSavedStarGiftChat.saved_id is empty.
//	400 STARGIFT_PEER_INVALID: The specified inputSavedStarGiftChat.peer is invalid.
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/payments.convertStarGift for reference.
func (c *Client) PaymentsConvertStarGift(ctx context.Context, stargift InputSavedStarGiftClass) (bool, error) {
	var result BoolBox

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