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

// GameHighScore represents TL type `gameHighScore#146fcf1e`.
type GameHighScore struct {
	// Position in the high score table
	Position int32
	// User identifier
	UserID int64
	// User score
	Score int32
}

// GameHighScoreTypeID is TL type id of GameHighScore.
const GameHighScoreTypeID = 0x146fcf1e

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

func (g *GameHighScore) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Position == 0) {
		return false
	}
	if !(g.UserID == 0) {
		return false
	}
	if !(g.Score == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GameHighScore) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "gameHighScore",
		ID:   GameHighScoreTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Position",
			SchemaName: "position",
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Score",
			SchemaName: "score",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GameHighScore) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode gameHighScore#146fcf1e as nil")
	}
	b.PutID(GameHighScoreTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GameHighScore) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode gameHighScore#146fcf1e as nil")
	}
	b.PutInt32(g.Position)
	b.PutInt53(g.UserID)
	b.PutInt32(g.Score)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GameHighScore) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode gameHighScore#146fcf1e to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode gameHighScore#146fcf1e: field position: %w", err)
		}
		g.Position = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode gameHighScore#146fcf1e: field user_id: %w", err)
		}
		g.UserID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode gameHighScore#146fcf1e: field score: %w", err)
		}
		g.Score = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GameHighScore) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode gameHighScore#146fcf1e as nil")
	}
	b.ObjStart()
	b.PutID("gameHighScore")
	b.Comma()
	b.FieldStart("position")
	b.PutInt32(g.Position)
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(g.UserID)
	b.Comma()
	b.FieldStart("score")
	b.PutInt32(g.Score)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("gameHighScore"); err != nil {
				return fmt.Errorf("unable to decode gameHighScore#146fcf1e: %w", err)
			}
		case "position":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode gameHighScore#146fcf1e: field position: %w", err)
			}
			g.Position = value
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode gameHighScore#146fcf1e: field user_id: %w", err)
			}
			g.UserID = value
		case "score":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode gameHighScore#146fcf1e: field score: %w", err)
			}
			g.Score = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPosition returns value of Position field.
func (g *GameHighScore) GetPosition() (value int32) {
	if g == nil {
		return
	}
	return g.Position
}

// GetUserID returns value of UserID field.
func (g *GameHighScore) GetUserID() (value int64) {
	if g == nil {
		return
	}
	return g.UserID
}

// GetScore returns value of Score field.
func (g *GameHighScore) GetScore() (value int32) {
	if g == nil {
		return
	}
	return g.Score
}
