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

// InlineQueryResultsButton represents TL type `inlineQueryResultsButton#d0df08ae`.
type InlineQueryResultsButton struct {
	// The text of the button
	Text string
	// Type of the button
	Type InlineQueryResultsButtonTypeClass
}

// InlineQueryResultsButtonTypeID is TL type id of InlineQueryResultsButton.
const InlineQueryResultsButtonTypeID = 0xd0df08ae

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InlineQueryResultsButton) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inlineQueryResultsButton",
		ID:   InlineQueryResultsButtonTypeID,
	}
	if i == 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 (i *InlineQueryResultsButton) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inlineQueryResultsButton#d0df08ae as nil")
	}
	b.PutID(InlineQueryResultsButtonTypeID)
	return i.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (i *InlineQueryResultsButton) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inlineQueryResultsButton#d0df08ae to nil")
	}
	if err := b.ConsumeID(InlineQueryResultsButtonTypeID); err != nil {
		return fmt.Errorf("unable to decode inlineQueryResultsButton#d0df08ae: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InlineQueryResultsButton) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inlineQueryResultsButton#d0df08ae to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inlineQueryResultsButton#d0df08ae: field text: %w", err)
		}
		i.Text = value
	}
	{
		value, err := DecodeInlineQueryResultsButtonType(b)
		if err != nil {
			return fmt.Errorf("unable to decode inlineQueryResultsButton#d0df08ae: field type: %w", err)
		}
		i.Type = value
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InlineQueryResultsButton) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inlineQueryResultsButton#d0df08ae to nil")
	}

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

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

// GetType returns value of Type field.
func (i *InlineQueryResultsButton) GetType() (value InlineQueryResultsButtonTypeClass) {
	if i == nil {
		return
	}
	return i.Type
}
