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

// KeyboardButton represents TL type `keyboardButton#84a0ce74`.
type KeyboardButton struct {
	// Text of the button
	Text string
	// Type of the button
	Type KeyboardButtonTypeClass
}

// KeyboardButtonTypeID is TL type id of KeyboardButton.
const KeyboardButtonTypeID = 0x84a0ce74

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

func (k *KeyboardButton) Zero() bool {
	if k == nil {
		return true
	}
	if !(k.Text == "") {
		return false
	}
	if !(k.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (k *KeyboardButton) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "keyboardButton",
		ID:   KeyboardButtonTypeID,
	}
	if k == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (k *KeyboardButton) Encode(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButton#84a0ce74 as nil")
	}
	b.PutID(KeyboardButtonTypeID)
	return k.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (k *KeyboardButton) EncodeBare(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButton#84a0ce74 as nil")
	}
	b.PutString(k.Text)
	if k.Type == nil {
		return fmt.Errorf("unable to encode keyboardButton#84a0ce74: field type is nil")
	}
	if err := k.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode keyboardButton#84a0ce74: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (k *KeyboardButton) Decode(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButton#84a0ce74 to nil")
	}
	if err := b.ConsumeID(KeyboardButtonTypeID); err != nil {
		return fmt.Errorf("unable to decode keyboardButton#84a0ce74: %w", err)
	}
	return k.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (k *KeyboardButton) DecodeBare(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButton#84a0ce74 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode keyboardButton#84a0ce74: field text: %w", err)
		}
		k.Text = value
	}
	{
		value, err := DecodeKeyboardButtonType(b)
		if err != nil {
			return fmt.Errorf("unable to decode keyboardButton#84a0ce74: field type: %w", err)
		}
		k.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (k *KeyboardButton) EncodeTDLibJSON(b tdjson.Encoder) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButton#84a0ce74 as nil")
	}
	b.ObjStart()
	b.PutID("keyboardButton")
	b.Comma()
	b.FieldStart("text")
	b.PutString(k.Text)
	b.Comma()
	b.FieldStart("type")
	if k.Type == nil {
		return fmt.Errorf("unable to encode keyboardButton#84a0ce74: field type is nil")
	}
	if err := k.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode keyboardButton#84a0ce74: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (k *KeyboardButton) DecodeTDLibJSON(b tdjson.Decoder) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButton#84a0ce74 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("keyboardButton"); err != nil {
				return fmt.Errorf("unable to decode keyboardButton#84a0ce74: %w", err)
			}
		case "text":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode keyboardButton#84a0ce74: field text: %w", err)
			}
			k.Text = value
		case "type":
			value, err := DecodeTDLibJSONKeyboardButtonType(b)
			if err != nil {
				return fmt.Errorf("unable to decode keyboardButton#84a0ce74: field type: %w", err)
			}
			k.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetText returns value of Text field.
func (k *KeyboardButton) GetText() (value string) {
	if k == nil {
		return
	}
	return k.Text
}

// GetType returns value of Type field.
func (k *KeyboardButton) GetType() (value KeyboardButtonTypeClass) {
	if k == nil {
		return
	}
	return k.Type
}
