// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// CheckPremiumGiftCodeRequest represents TL type `checkPremiumGiftCode#958ad664`.
type CheckPremiumGiftCodeRequest struct {
	// The code to check
	Code string
}

// CheckPremiumGiftCodeRequestTypeID is TL type id of CheckPremiumGiftCodeRequest.
const CheckPremiumGiftCodeRequestTypeID = 0x958ad664

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

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

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*CheckPremiumGiftCodeRequest) TypeName() string {
	return "checkPremiumGiftCode"
}

// TypeInfo returns info about TL type.
func (c *CheckPremiumGiftCodeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "checkPremiumGiftCode",
		ID:   CheckPremiumGiftCodeRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Code",
			SchemaName: "code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CheckPremiumGiftCodeRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkPremiumGiftCode#958ad664 as nil")
	}
	b.PutID(CheckPremiumGiftCodeRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CheckPremiumGiftCodeRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkPremiumGiftCode#958ad664 as nil")
	}
	b.PutString(c.Code)
	return nil
}

// Decode implements bin.Decoder.
func (c *CheckPremiumGiftCodeRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkPremiumGiftCode#958ad664 to nil")
	}
	if err := b.ConsumeID(CheckPremiumGiftCodeRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode checkPremiumGiftCode#958ad664: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CheckPremiumGiftCodeRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkPremiumGiftCode#958ad664 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode checkPremiumGiftCode#958ad664: field code: %w", err)
		}
		c.Code = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CheckPremiumGiftCodeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode checkPremiumGiftCode#958ad664 as nil")
	}
	b.ObjStart()
	b.PutID("checkPremiumGiftCode")
	b.Comma()
	b.FieldStart("code")
	b.PutString(c.Code)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CheckPremiumGiftCodeRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode checkPremiumGiftCode#958ad664 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("checkPremiumGiftCode"); err != nil {
				return fmt.Errorf("unable to decode checkPremiumGiftCode#958ad664: %w", err)
			}
		case "code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode checkPremiumGiftCode#958ad664: field code: %w", err)
			}
			c.Code = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCode returns value of Code field.
func (c *CheckPremiumGiftCodeRequest) GetCode() (value string) {
	if c == nil {
		return
	}
	return c.Code
}

// CheckPremiumGiftCode invokes method checkPremiumGiftCode#958ad664 returning error if any.
func (c *Client) CheckPremiumGiftCode(ctx context.Context, code string) (*PremiumGiftCodeInfo, error) {
	var result PremiumGiftCodeInfo

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