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

// BotCommands represents TL type `botCommands#4f9aa2c5`.
type BotCommands struct {
	// Bot's user identifier
	BotUserID int64
	// List of bot commands
	Commands []BotCommand
}

// BotCommandsTypeID is TL type id of BotCommands.
const BotCommandsTypeID = 0x4f9aa2c5

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

func (b *BotCommands) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.BotUserID == 0) {
		return false
	}
	if !(b.Commands == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BotCommands) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botCommands",
		ID:   BotCommandsTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "Commands",
			SchemaName: "commands",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotCommands) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botCommands#4f9aa2c5 as nil")
	}
	buf.PutID(BotCommandsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotCommands) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botCommands#4f9aa2c5 as nil")
	}
	buf.PutInt53(b.BotUserID)
	buf.PutInt(len(b.Commands))
	for idx, v := range b.Commands {
		if err := v.EncodeBare(buf); err != nil {
			return fmt.Errorf("unable to encode bare botCommands#4f9aa2c5: field commands element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BotCommands) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botCommands#4f9aa2c5 to nil")
	}
	{
		value, err := buf.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode botCommands#4f9aa2c5: field bot_user_id: %w", err)
		}
		b.BotUserID = value
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botCommands#4f9aa2c5: field commands: %w", err)
		}

		if headerLen > 0 {
			b.Commands = make([]BotCommand, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BotCommand
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare botCommands#4f9aa2c5: field commands: %w", err)
			}
			b.Commands = append(b.Commands, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BotCommands) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode botCommands#4f9aa2c5 as nil")
	}
	buf.ObjStart()
	buf.PutID("botCommands")
	buf.Comma()
	buf.FieldStart("bot_user_id")
	buf.PutInt53(b.BotUserID)
	buf.Comma()
	buf.FieldStart("commands")
	buf.ArrStart()
	for idx, v := range b.Commands {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode botCommands#4f9aa2c5: field commands element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BotCommands) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode botCommands#4f9aa2c5 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("botCommands"); err != nil {
				return fmt.Errorf("unable to decode botCommands#4f9aa2c5: %w", err)
			}
		case "bot_user_id":
			value, err := buf.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode botCommands#4f9aa2c5: field bot_user_id: %w", err)
			}
			b.BotUserID = value
		case "commands":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value BotCommand
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode botCommands#4f9aa2c5: field commands: %w", err)
				}
				b.Commands = append(b.Commands, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode botCommands#4f9aa2c5: field commands: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

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

// GetCommands returns value of Commands field.
func (b *BotCommands) GetCommands() (value []BotCommand) {
	if b == nil {
		return
	}
	return b.Commands
}
