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

// GetCollectibleItemInfoRequest represents TL type `getCollectibleItemInfo#f304ad8a`.
type GetCollectibleItemInfoRequest struct {
	// Type of the collectible item. The item must be used by a user and must be visible to
	// the current user
	Type CollectibleItemTypeClass
}

// GetCollectibleItemInfoRequestTypeID is TL type id of GetCollectibleItemInfoRequest.
const GetCollectibleItemInfoRequestTypeID = 0xf304ad8a

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

func (g *GetCollectibleItemInfoRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Type == nil) {
		return false
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (g *GetCollectibleItemInfoRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getCollectibleItemInfo#f304ad8a as nil")
	}
	b.PutID(GetCollectibleItemInfoRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetCollectibleItemInfoRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getCollectibleItemInfo#f304ad8a as nil")
	}
	if g.Type == nil {
		return fmt.Errorf("unable to encode getCollectibleItemInfo#f304ad8a: field type is nil")
	}
	if err := g.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getCollectibleItemInfo#f304ad8a: field type: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetCollectibleItemInfoRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getCollectibleItemInfo#f304ad8a to nil")
	}
	{
		value, err := DecodeCollectibleItemType(b)
		if err != nil {
			return fmt.Errorf("unable to decode getCollectibleItemInfo#f304ad8a: field type: %w", err)
		}
		g.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetCollectibleItemInfoRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getCollectibleItemInfo#f304ad8a as nil")
	}
	b.ObjStart()
	b.PutID("getCollectibleItemInfo")
	b.Comma()
	b.FieldStart("type")
	if g.Type == nil {
		return fmt.Errorf("unable to encode getCollectibleItemInfo#f304ad8a: field type is nil")
	}
	if err := g.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getCollectibleItemInfo#f304ad8a: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetType returns value of Type field.
func (g *GetCollectibleItemInfoRequest) GetType() (value CollectibleItemTypeClass) {
	if g == nil {
		return
	}
	return g.Type
}

// GetCollectibleItemInfo invokes method getCollectibleItemInfo#f304ad8a returning error if any.
func (c *Client) GetCollectibleItemInfo(ctx context.Context, type_ CollectibleItemTypeClass) (*CollectibleItemInfo, error) {
	var result CollectibleItemInfo

	request := &GetCollectibleItemInfoRequest{
		Type: type_,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
