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

// GetUpgradedGiftRequest represents TL type `getUpgradedGift#b09e05b1`.
type GetUpgradedGiftRequest struct {
	// Unique name of the upgraded gift
	Name string
}

// GetUpgradedGiftRequestTypeID is TL type id of GetUpgradedGiftRequest.
const GetUpgradedGiftRequestTypeID = 0xb09e05b1

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

func (g *GetUpgradedGiftRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Name == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetUpgradedGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getUpgradedGift",
		ID:   GetUpgradedGiftRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetUpgradedGiftRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getUpgradedGift#b09e05b1 as nil")
	}
	b.PutID(GetUpgradedGiftRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetUpgradedGiftRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getUpgradedGift#b09e05b1 as nil")
	}
	b.PutString(g.Name)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetUpgradedGiftRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getUpgradedGift#b09e05b1 to nil")
	}
	if err := b.ConsumeID(GetUpgradedGiftRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getUpgradedGift#b09e05b1: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetUpgradedGiftRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getUpgradedGift#b09e05b1 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getUpgradedGift#b09e05b1: field name: %w", err)
		}
		g.Name = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetUpgradedGiftRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getUpgradedGift#b09e05b1 as nil")
	}
	b.ObjStart()
	b.PutID("getUpgradedGift")
	b.Comma()
	b.FieldStart("name")
	b.PutString(g.Name)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetUpgradedGiftRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getUpgradedGift#b09e05b1 to nil")
	}

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

// GetName returns value of Name field.
func (g *GetUpgradedGiftRequest) GetName() (value string) {
	if g == nil {
		return
	}
	return g.Name
}

// GetUpgradedGift invokes method getUpgradedGift#b09e05b1 returning error if any.
func (c *Client) GetUpgradedGift(ctx context.Context, name string) (*UpgradedGift, error) {
	var result UpgradedGift

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