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

// GetBotNameRequest represents TL type `getBotName#9a3f722c`.
type GetBotNameRequest struct {
	// Identifier of the target bot
	BotUserID int64
	// A two-letter ISO 639-1 language code or an empty string
	LanguageCode string
}

// GetBotNameRequestTypeID is TL type id of GetBotNameRequest.
const GetBotNameRequestTypeID = 0x9a3f722c

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

func (g *GetBotNameRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BotUserID == 0) {
		return false
	}
	if !(g.LanguageCode == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBotNameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBotName",
		ID:   GetBotNameRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "LanguageCode",
			SchemaName: "language_code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBotNameRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotName#9a3f722c as nil")
	}
	b.PutID(GetBotNameRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBotNameRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotName#9a3f722c as nil")
	}
	b.PutInt53(g.BotUserID)
	b.PutString(g.LanguageCode)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetBotNameRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBotName#9a3f722c to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getBotName#9a3f722c: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getBotName#9a3f722c: field language_code: %w", err)
		}
		g.LanguageCode = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBotNameRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotName#9a3f722c as nil")
	}
	b.ObjStart()
	b.PutID("getBotName")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.FieldStart("language_code")
	b.PutString(g.LanguageCode)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getBotName"); err != nil {
				return fmt.Errorf("unable to decode getBotName#9a3f722c: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getBotName#9a3f722c: field bot_user_id: %w", err)
			}
			g.BotUserID = value
		case "language_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getBotName#9a3f722c: field language_code: %w", err)
			}
			g.LanguageCode = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBotUserID returns value of BotUserID field.
func (g *GetBotNameRequest) GetBotUserID() (value int64) {
	if g == nil {
		return
	}
	return g.BotUserID
}

// GetLanguageCode returns value of LanguageCode field.
func (g *GetBotNameRequest) GetLanguageCode() (value string) {
	if g == nil {
		return
	}
	return g.LanguageCode
}

// GetBotName invokes method getBotName#9a3f722c returning error if any.
func (c *Client) GetBotName(ctx context.Context, request *GetBotNameRequest) (*Text, error) {
	var result Text

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