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

// ApplyPremiumGiftCodeRequest represents TL type `applyPremiumGiftCode#afb44c1e`.
type ApplyPremiumGiftCodeRequest struct {
	// The code to apply
	Code string
}

// ApplyPremiumGiftCodeRequestTypeID is TL type id of ApplyPremiumGiftCodeRequest.
const ApplyPremiumGiftCodeRequestTypeID = 0xafb44c1e

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (a *ApplyPremiumGiftCodeRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode applyPremiumGiftCode#afb44c1e as nil")
	}
	b.PutID(ApplyPremiumGiftCodeRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *ApplyPremiumGiftCodeRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode applyPremiumGiftCode#afb44c1e as nil")
	}
	b.PutString(a.Code)
	return nil
}

// Decode implements bin.Decoder.
func (a *ApplyPremiumGiftCodeRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode applyPremiumGiftCode#afb44c1e to nil")
	}
	if err := b.ConsumeID(ApplyPremiumGiftCodeRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode applyPremiumGiftCode#afb44c1e: %w", err)
	}
	return a.DecodeBare(b)
}

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

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *ApplyPremiumGiftCodeRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode applyPremiumGiftCode#afb44c1e to nil")
	}

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

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

// ApplyPremiumGiftCode invokes method applyPremiumGiftCode#afb44c1e returning error if any.
func (c *Client) ApplyPremiumGiftCode(ctx context.Context, code string) error {
	var ok Ok

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