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

// PaymentsCheckGiftCodeRequest represents TL type `payments.checkGiftCode#8e51b4c1`.
// Obtain information about a Telegram Premium giftcode »¹
//
// Links:
//  1. https://core.telegram.org/api/giveaways
//
// See https://core.telegram.org/method/payments.checkGiftCode for reference.
type PaymentsCheckGiftCodeRequest struct {
	// The giftcode to check
	Slug string
}

// PaymentsCheckGiftCodeRequestTypeID is TL type id of PaymentsCheckGiftCodeRequest.
const PaymentsCheckGiftCodeRequestTypeID = 0x8e51b4c1

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

func (c *PaymentsCheckGiftCodeRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Slug == "") {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsCheckGiftCodeRequest from given interface.
func (c *PaymentsCheckGiftCodeRequest) FillFrom(from interface {
	GetSlug() (value string)
}) {
	c.Slug = from.GetSlug()
}

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

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

// TypeInfo returns info about TL type.
func (c *PaymentsCheckGiftCodeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.checkGiftCode",
		ID:   PaymentsCheckGiftCodeRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Slug",
			SchemaName: "slug",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *PaymentsCheckGiftCodeRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.checkGiftCode#8e51b4c1 as nil")
	}
	b.PutID(PaymentsCheckGiftCodeRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PaymentsCheckGiftCodeRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.checkGiftCode#8e51b4c1 as nil")
	}
	b.PutString(c.Slug)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *PaymentsCheckGiftCodeRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.checkGiftCode#8e51b4c1 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkGiftCode#8e51b4c1: field slug: %w", err)
		}
		c.Slug = value
	}
	return nil
}

// GetSlug returns value of Slug field.
func (c *PaymentsCheckGiftCodeRequest) GetSlug() (value string) {
	if c == nil {
		return
	}
	return c.Slug
}

// PaymentsCheckGiftCode invokes method payments.checkGiftCode#8e51b4c1 returning error if any.
// Obtain information about a Telegram Premium giftcode »¹
//
// Links:
//  1. https://core.telegram.org/api/giveaways
//
// Possible errors:
//
//	400 GIFT_SLUG_EXPIRED: The specified gift slug has expired.
//	400 GIFT_SLUG_INVALID: The specified slug is invalid.
//
// See https://core.telegram.org/method/payments.checkGiftCode for reference.
func (c *Client) PaymentsCheckGiftCode(ctx context.Context, slug string) (*PaymentsCheckedGiftCode, error) {
	var result PaymentsCheckedGiftCode

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