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

// Game represents TL type `game#a2aedfc8`.
type Game struct {
	// Unique game identifier
	ID int64
	// Game short name
	ShortName string
	// Game title
	Title string
	// Game text, usually containing scoreboards for a game
	Text FormattedText
	// Describes a game. Use getInternalLink with internalLinkTypeGame to share the game
	Description string
	// Game photo
	Photo Photo
	// Game animation; may be null
	Animation Animation
}

// GameTypeID is TL type id of Game.
const GameTypeID = 0xa2aedfc8

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

func (g *Game) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ID == 0) {
		return false
	}
	if !(g.ShortName == "") {
		return false
	}
	if !(g.Title == "") {
		return false
	}
	if !(g.Text.Zero()) {
		return false
	}
	if !(g.Description == "") {
		return false
	}
	if !(g.Photo.Zero()) {
		return false
	}
	if !(g.Animation.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *Game) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "game",
		ID:   GameTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "ShortName",
			SchemaName: "short_name",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "Description",
			SchemaName: "description",
		},
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
		{
			Name:       "Animation",
			SchemaName: "animation",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *Game) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode game#a2aedfc8 as nil")
	}
	b.PutID(GameTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *Game) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode game#a2aedfc8 as nil")
	}
	b.PutLong(g.ID)
	b.PutString(g.ShortName)
	b.PutString(g.Title)
	if err := g.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode game#a2aedfc8: field text: %w", err)
	}
	b.PutString(g.Description)
	if err := g.Photo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode game#a2aedfc8: field photo: %w", err)
	}
	if err := g.Animation.Encode(b); err != nil {
		return fmt.Errorf("unable to encode game#a2aedfc8: field animation: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *Game) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode game#a2aedfc8 to nil")
	}
	if err := b.ConsumeID(GameTypeID); err != nil {
		return fmt.Errorf("unable to decode game#a2aedfc8: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *Game) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode game#a2aedfc8 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field id: %w", err)
		}
		g.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field short_name: %w", err)
		}
		g.ShortName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field title: %w", err)
		}
		g.Title = value
	}
	{
		if err := g.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field text: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field description: %w", err)
		}
		g.Description = value
	}
	{
		if err := g.Photo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field photo: %w", err)
		}
	}
	{
		if err := g.Animation.Decode(b); err != nil {
			return fmt.Errorf("unable to decode game#a2aedfc8: field animation: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *Game) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode game#a2aedfc8 as nil")
	}
	b.ObjStart()
	b.PutID("game")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(g.ID)
	b.Comma()
	b.FieldStart("short_name")
	b.PutString(g.ShortName)
	b.Comma()
	b.FieldStart("title")
	b.PutString(g.Title)
	b.Comma()
	b.FieldStart("text")
	if err := g.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode game#a2aedfc8: field text: %w", err)
	}
	b.Comma()
	b.FieldStart("description")
	b.PutString(g.Description)
	b.Comma()
	b.FieldStart("photo")
	if err := g.Photo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode game#a2aedfc8: field photo: %w", err)
	}
	b.Comma()
	b.FieldStart("animation")
	if err := g.Animation.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode game#a2aedfc8: field animation: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *Game) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode game#a2aedfc8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("game"); err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field id: %w", err)
			}
			g.ID = value
		case "short_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field short_name: %w", err)
			}
			g.ShortName = value
		case "title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field title: %w", err)
			}
			g.Title = value
		case "text":
			if err := g.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field text: %w", err)
			}
		case "description":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field description: %w", err)
			}
			g.Description = value
		case "photo":
			if err := g.Photo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field photo: %w", err)
			}
		case "animation":
			if err := g.Animation.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode game#a2aedfc8: field animation: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (g *Game) GetID() (value int64) {
	if g == nil {
		return
	}
	return g.ID
}

// GetShortName returns value of ShortName field.
func (g *Game) GetShortName() (value string) {
	if g == nil {
		return
	}
	return g.ShortName
}

// GetTitle returns value of Title field.
func (g *Game) GetTitle() (value string) {
	if g == nil {
		return
	}
	return g.Title
}

// GetText returns value of Text field.
func (g *Game) GetText() (value FormattedText) {
	if g == nil {
		return
	}
	return g.Text
}

// GetDescription returns value of Description field.
func (g *Game) GetDescription() (value string) {
	if g == nil {
		return
	}
	return g.Description
}

// GetPhoto returns value of Photo field.
func (g *Game) GetPhoto() (value Photo) {
	if g == nil {
		return
	}
	return g.Photo
}

// GetAnimation returns value of Animation field.
func (g *Game) GetAnimation() (value Animation) {
	if g == nil {
		return
	}
	return g.Animation
}
