// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// BotMenuButtonDefault represents TL type `botMenuButtonDefault#7533a588`.
// Placeholder bot menu button¹ never returned to users: see the docs for more info².
//
// Links:
//  1. https://core.telegram.org/api/bots/menu
//  2. https://core.telegram.org/api/bots/menu
//
// See https://core.telegram.org/constructor/botMenuButtonDefault for reference.
type BotMenuButtonDefault struct {
}

// BotMenuButtonDefaultTypeID is TL type id of BotMenuButtonDefault.
const BotMenuButtonDefaultTypeID = 0x7533a588

// construct implements constructor of BotMenuButtonClass.
func (b BotMenuButtonDefault) construct() BotMenuButtonClass { return &b }

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

	_ BotMenuButtonClass = &BotMenuButtonDefault{}
)

func (b *BotMenuButtonDefault) Zero() bool {
	if b == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BotMenuButtonDefault) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botMenuButtonDefault",
		ID:   BotMenuButtonDefaultTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotMenuButtonDefault) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMenuButtonDefault#7533a588 as nil")
	}
	buf.PutID(BotMenuButtonDefaultTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotMenuButtonDefault) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMenuButtonDefault#7533a588 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotMenuButtonDefault) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMenuButtonDefault#7533a588 to nil")
	}
	if err := buf.ConsumeID(BotMenuButtonDefaultTypeID); err != nil {
		return fmt.Errorf("unable to decode botMenuButtonDefault#7533a588: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotMenuButtonDefault) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMenuButtonDefault#7533a588 to nil")
	}
	return nil
}

// BotMenuButtonCommands represents TL type `botMenuButtonCommands#4258c205`.
// Bot menu button¹ that opens the bot command list when clicked.
//
// Links:
//  1. https://core.telegram.org/api/bots/menu
//
// See https://core.telegram.org/constructor/botMenuButtonCommands for reference.
type BotMenuButtonCommands struct {
}

// BotMenuButtonCommandsTypeID is TL type id of BotMenuButtonCommands.
const BotMenuButtonCommandsTypeID = 0x4258c205

// construct implements constructor of BotMenuButtonClass.
func (b BotMenuButtonCommands) construct() BotMenuButtonClass { return &b }

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

	_ BotMenuButtonClass = &BotMenuButtonCommands{}
)

func (b *BotMenuButtonCommands) Zero() bool {
	if b == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BotMenuButtonCommands) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botMenuButtonCommands",
		ID:   BotMenuButtonCommandsTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotMenuButtonCommands) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMenuButtonCommands#4258c205 as nil")
	}
	buf.PutID(BotMenuButtonCommandsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotMenuButtonCommands) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMenuButtonCommands#4258c205 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotMenuButtonCommands) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMenuButtonCommands#4258c205 to nil")
	}
	if err := buf.ConsumeID(BotMenuButtonCommandsTypeID); err != nil {
		return fmt.Errorf("unable to decode botMenuButtonCommands#4258c205: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotMenuButtonCommands) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMenuButtonCommands#4258c205 to nil")
	}
	return nil
}

// BotMenuButton represents TL type `botMenuButton#c7b57ce6`.
// Bot menu button¹ that opens a web app² when clicked.
//
// Links:
//  1. https://core.telegram.org/api/bots/menu
//  2. https://core.telegram.org/api/bots/webapps
//
// See https://core.telegram.org/constructor/botMenuButton for reference.
type BotMenuButton struct {
	// Title to be displayed on the menu button instead of 'Menu'
	Text string
	// URL of a web app¹ to open when the user clicks on the button
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/webapps
	URL string
}

// BotMenuButtonTypeID is TL type id of BotMenuButton.
const BotMenuButtonTypeID = 0xc7b57ce6

// construct implements constructor of BotMenuButtonClass.
func (b BotMenuButton) construct() BotMenuButtonClass { return &b }

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

	_ BotMenuButtonClass = &BotMenuButton{}
)

func (b *BotMenuButton) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Text == "") {
		return false
	}
	if !(b.URL == "") {
		return false
	}

	return true
}

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

// FillFrom fills BotMenuButton from given interface.
func (b *BotMenuButton) FillFrom(from interface {
	GetText() (value string)
	GetURL() (value string)
}) {
	b.Text = from.GetText()
	b.URL = from.GetURL()
}

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

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

// TypeInfo returns info about TL type.
func (b *BotMenuButton) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botMenuButton",
		ID:   BotMenuButtonTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotMenuButton) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMenuButton#c7b57ce6 as nil")
	}
	buf.PutID(BotMenuButtonTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotMenuButton) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMenuButton#c7b57ce6 as nil")
	}
	buf.PutString(b.Text)
	buf.PutString(b.URL)
	return nil
}

// Decode implements bin.Decoder.
func (b *BotMenuButton) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMenuButton#c7b57ce6 to nil")
	}
	if err := buf.ConsumeID(BotMenuButtonTypeID); err != nil {
		return fmt.Errorf("unable to decode botMenuButton#c7b57ce6: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotMenuButton) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMenuButton#c7b57ce6 to nil")
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode botMenuButton#c7b57ce6: field text: %w", err)
		}
		b.Text = value
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode botMenuButton#c7b57ce6: field url: %w", err)
		}
		b.URL = value
	}
	return nil
}

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

// GetURL returns value of URL field.
func (b *BotMenuButton) GetURL() (value string) {
	if b == nil {
		return
	}
	return b.URL
}

// BotMenuButtonClassName is schema name of BotMenuButtonClass.
const BotMenuButtonClassName = "BotMenuButton"

// BotMenuButtonClass represents BotMenuButton generic type.
//
// See https://core.telegram.org/type/BotMenuButton for reference.
//
// Example:
//
//	g, err := tg.DecodeBotMenuButton(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.BotMenuButtonDefault: // botMenuButtonDefault#7533a588
//	case *tg.BotMenuButtonCommands: // botMenuButtonCommands#4258c205
//	case *tg.BotMenuButton: // botMenuButton#c7b57ce6
//	default: panic(v)
//	}
type BotMenuButtonClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() BotMenuButtonClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool
}

// DecodeBotMenuButton implements binary de-serialization for BotMenuButtonClass.
func DecodeBotMenuButton(buf *bin.Buffer) (BotMenuButtonClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case BotMenuButtonDefaultTypeID:
		// Decoding botMenuButtonDefault#7533a588.
		v := BotMenuButtonDefault{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode BotMenuButtonClass: %w", err)
		}
		return &v, nil
	case BotMenuButtonCommandsTypeID:
		// Decoding botMenuButtonCommands#4258c205.
		v := BotMenuButtonCommands{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode BotMenuButtonClass: %w", err)
		}
		return &v, nil
	case BotMenuButtonTypeID:
		// Decoding botMenuButton#c7b57ce6.
		v := BotMenuButton{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode BotMenuButtonClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode BotMenuButtonClass: %w", bin.NewUnexpectedID(id))
	}
}

// BotMenuButton boxes the BotMenuButtonClass providing a helper.
type BotMenuButtonBox struct {
	BotMenuButton BotMenuButtonClass
}

// Decode implements bin.Decoder for BotMenuButtonBox.
func (b *BotMenuButtonBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode BotMenuButtonBox to nil")
	}
	v, err := DecodeBotMenuButton(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.BotMenuButton = v
	return nil
}

// Encode implements bin.Encode for BotMenuButtonBox.
func (b *BotMenuButtonBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.BotMenuButton == nil {
		return fmt.Errorf("unable to encode BotMenuButtonClass as nil")
	}
	return b.BotMenuButton.Encode(buf)
}
