// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesEmojiGameUnavailable represents TL type `messages.emojiGameUnavailable#59e65335`.
//
// See https://core.telegram.org/constructor/messages.emojiGameUnavailable for reference.
type MessagesEmojiGameUnavailable struct {
}

// MessagesEmojiGameUnavailableTypeID is TL type id of MessagesEmojiGameUnavailable.
const MessagesEmojiGameUnavailableTypeID = 0x59e65335

// construct implements constructor of MessagesEmojiGameInfoClass.
func (e MessagesEmojiGameUnavailable) construct() MessagesEmojiGameInfoClass { return &e }

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

	_ MessagesEmojiGameInfoClass = &MessagesEmojiGameUnavailable{}
)

func (e *MessagesEmojiGameUnavailable) Zero() bool {
	if e == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*MessagesEmojiGameUnavailable) TypeName() string {
	return "messages.emojiGameUnavailable"
}

// TypeInfo returns info about TL type.
func (e *MessagesEmojiGameUnavailable) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.emojiGameUnavailable",
		ID:   MessagesEmojiGameUnavailableTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (e *MessagesEmojiGameUnavailable) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode messages.emojiGameUnavailable#59e65335 as nil")
	}
	b.PutID(MessagesEmojiGameUnavailableTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *MessagesEmojiGameUnavailable) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode messages.emojiGameUnavailable#59e65335 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *MessagesEmojiGameUnavailable) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode messages.emojiGameUnavailable#59e65335 to nil")
	}
	if err := b.ConsumeID(MessagesEmojiGameUnavailableTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.emojiGameUnavailable#59e65335: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *MessagesEmojiGameUnavailable) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode messages.emojiGameUnavailable#59e65335 to nil")
	}
	return nil
}

// MessagesEmojiGameDiceInfo represents TL type `messages.emojiGameDiceInfo#44e56023`.
//
// See https://core.telegram.org/constructor/messages.emojiGameDiceInfo for reference.
type MessagesEmojiGameDiceInfo struct {
	// Flags field of MessagesEmojiGameDiceInfo.
	Flags bin.Fields
	// GameHash field of MessagesEmojiGameDiceInfo.
	GameHash string
	// PrevStake field of MessagesEmojiGameDiceInfo.
	PrevStake int64
	// CurrentStreak field of MessagesEmojiGameDiceInfo.
	CurrentStreak int
	// Params field of MessagesEmojiGameDiceInfo.
	Params []int
	// PlaysLeft field of MessagesEmojiGameDiceInfo.
	//
	// Use SetPlaysLeft and GetPlaysLeft helpers.
	PlaysLeft int
}

// MessagesEmojiGameDiceInfoTypeID is TL type id of MessagesEmojiGameDiceInfo.
const MessagesEmojiGameDiceInfoTypeID = 0x44e56023

// construct implements constructor of MessagesEmojiGameInfoClass.
func (e MessagesEmojiGameDiceInfo) construct() MessagesEmojiGameInfoClass { return &e }

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

	_ MessagesEmojiGameInfoClass = &MessagesEmojiGameDiceInfo{}
)

func (e *MessagesEmojiGameDiceInfo) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.Flags.Zero()) {
		return false
	}
	if !(e.GameHash == "") {
		return false
	}
	if !(e.PrevStake == 0) {
		return false
	}
	if !(e.CurrentStreak == 0) {
		return false
	}
	if !(e.Params == nil) {
		return false
	}
	if !(e.PlaysLeft == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesEmojiGameDiceInfo from given interface.
func (e *MessagesEmojiGameDiceInfo) FillFrom(from interface {
	GetGameHash() (value string)
	GetPrevStake() (value int64)
	GetCurrentStreak() (value int)
	GetParams() (value []int)
	GetPlaysLeft() (value int, ok bool)
}) {
	e.GameHash = from.GetGameHash()
	e.PrevStake = from.GetPrevStake()
	e.CurrentStreak = from.GetCurrentStreak()
	e.Params = from.GetParams()
	if val, ok := from.GetPlaysLeft(); ok {
		e.PlaysLeft = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*MessagesEmojiGameDiceInfo) TypeName() string {
	return "messages.emojiGameDiceInfo"
}

// TypeInfo returns info about TL type.
func (e *MessagesEmojiGameDiceInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.emojiGameDiceInfo",
		ID:   MessagesEmojiGameDiceInfoTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GameHash",
			SchemaName: "game_hash",
		},
		{
			Name:       "PrevStake",
			SchemaName: "prev_stake",
		},
		{
			Name:       "CurrentStreak",
			SchemaName: "current_streak",
		},
		{
			Name:       "Params",
			SchemaName: "params",
		},
		{
			Name:       "PlaysLeft",
			SchemaName: "plays_left",
			Null:       !e.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (e *MessagesEmojiGameDiceInfo) SetFlags() {
	if !(e.PlaysLeft == 0) {
		e.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (e *MessagesEmojiGameDiceInfo) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode messages.emojiGameDiceInfo#44e56023 as nil")
	}
	b.PutID(MessagesEmojiGameDiceInfoTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *MessagesEmojiGameDiceInfo) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode messages.emojiGameDiceInfo#44e56023 as nil")
	}
	e.SetFlags()
	if err := e.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.emojiGameDiceInfo#44e56023: field flags: %w", err)
	}
	b.PutString(e.GameHash)
	b.PutLong(e.PrevStake)
	b.PutInt(e.CurrentStreak)
	b.PutVectorHeader(len(e.Params))
	for _, v := range e.Params {
		b.PutInt(v)
	}
	if e.Flags.Has(0) {
		b.PutInt(e.PlaysLeft)
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *MessagesEmojiGameDiceInfo) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode messages.emojiGameDiceInfo#44e56023 to nil")
	}
	if err := b.ConsumeID(MessagesEmojiGameDiceInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *MessagesEmojiGameDiceInfo) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode messages.emojiGameDiceInfo#44e56023 to nil")
	}
	{
		if err := e.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field game_hash: %w", err)
		}
		e.GameHash = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field prev_stake: %w", err)
		}
		e.PrevStake = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field current_streak: %w", err)
		}
		e.CurrentStreak = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field params: %w", err)
		}

		if headerLen > 0 {
			e.Params = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field params: %w", err)
			}
			e.Params = append(e.Params, value)
		}
	}
	if e.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.emojiGameDiceInfo#44e56023: field plays_left: %w", err)
		}
		e.PlaysLeft = value
	}
	return nil
}

// GetGameHash returns value of GameHash field.
func (e *MessagesEmojiGameDiceInfo) GetGameHash() (value string) {
	if e == nil {
		return
	}
	return e.GameHash
}

// GetPrevStake returns value of PrevStake field.
func (e *MessagesEmojiGameDiceInfo) GetPrevStake() (value int64) {
	if e == nil {
		return
	}
	return e.PrevStake
}

// GetCurrentStreak returns value of CurrentStreak field.
func (e *MessagesEmojiGameDiceInfo) GetCurrentStreak() (value int) {
	if e == nil {
		return
	}
	return e.CurrentStreak
}

// GetParams returns value of Params field.
func (e *MessagesEmojiGameDiceInfo) GetParams() (value []int) {
	if e == nil {
		return
	}
	return e.Params
}

// SetPlaysLeft sets value of PlaysLeft conditional field.
func (e *MessagesEmojiGameDiceInfo) SetPlaysLeft(value int) {
	e.Flags.Set(0)
	e.PlaysLeft = value
}

// GetPlaysLeft returns value of PlaysLeft conditional field and
// boolean which is true if field was set.
func (e *MessagesEmojiGameDiceInfo) GetPlaysLeft() (value int, ok bool) {
	if e == nil {
		return
	}
	if !e.Flags.Has(0) {
		return value, false
	}
	return e.PlaysLeft, true
}

// MessagesEmojiGameInfoClassName is schema name of MessagesEmojiGameInfoClass.
const MessagesEmojiGameInfoClassName = "messages.EmojiGameInfo"

// MessagesEmojiGameInfoClass represents messages.EmojiGameInfo generic type.
//
// See https://core.telegram.org/type/messages.EmojiGameInfo for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesEmojiGameInfo(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesEmojiGameUnavailable: // messages.emojiGameUnavailable#59e65335
//	case *tg.MessagesEmojiGameDiceInfo: // messages.emojiGameDiceInfo#44e56023
//	default: panic(v)
//	}
type MessagesEmojiGameInfoClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesEmojiGameInfoClass

	// 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

	// AsAvailable tries to map MessagesEmojiGameInfoClass to MessagesEmojiGameDiceInfo.
	AsAvailable() (*MessagesEmojiGameDiceInfo, bool)
}

// AsAvailable tries to map MessagesEmojiGameUnavailable to MessagesEmojiGameDiceInfo.
func (e *MessagesEmojiGameUnavailable) AsAvailable() (*MessagesEmojiGameDiceInfo, bool) {
	return nil, false
}

// AsAvailable tries to map MessagesEmojiGameDiceInfo to MessagesEmojiGameDiceInfo.
func (e *MessagesEmojiGameDiceInfo) AsAvailable() (*MessagesEmojiGameDiceInfo, bool) {
	return e, true
}

// DecodeMessagesEmojiGameInfo implements binary de-serialization for MessagesEmojiGameInfoClass.
func DecodeMessagesEmojiGameInfo(buf *bin.Buffer) (MessagesEmojiGameInfoClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessagesEmojiGameUnavailableTypeID:
		// Decoding messages.emojiGameUnavailable#59e65335.
		v := MessagesEmojiGameUnavailable{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesEmojiGameInfoClass: %w", err)
		}
		return &v, nil
	case MessagesEmojiGameDiceInfoTypeID:
		// Decoding messages.emojiGameDiceInfo#44e56023.
		v := MessagesEmojiGameDiceInfo{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesEmojiGameInfoClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesEmojiGameInfoClass: %w", bin.NewUnexpectedID(id))
	}
}

// MessagesEmojiGameInfo boxes the MessagesEmojiGameInfoClass providing a helper.
type MessagesEmojiGameInfoBox struct {
	EmojiGameInfo MessagesEmojiGameInfoClass
}

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

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