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

// GameHighScores represents TL type `gameHighScores#6d4cd30d`.
type GameHighScores struct {
	// A list of game high scores
	Scores []GameHighScore
}

// GameHighScoresTypeID is TL type id of GameHighScores.
const GameHighScoresTypeID = 0x6d4cd30d

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

func (g *GameHighScores) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Scores == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GameHighScores) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "gameHighScores",
		ID:   GameHighScoresTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Scores",
			SchemaName: "scores",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GameHighScores) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode gameHighScores#6d4cd30d as nil")
	}
	b.PutID(GameHighScoresTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GameHighScores) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode gameHighScores#6d4cd30d as nil")
	}
	b.PutInt(len(g.Scores))
	for idx, v := range g.Scores {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare gameHighScores#6d4cd30d: field scores element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GameHighScores) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode gameHighScores#6d4cd30d to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode gameHighScores#6d4cd30d: field scores: %w", err)
		}

		if headerLen > 0 {
			g.Scores = make([]GameHighScore, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value GameHighScore
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare gameHighScores#6d4cd30d: field scores: %w", err)
			}
			g.Scores = append(g.Scores, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GameHighScores) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode gameHighScores#6d4cd30d as nil")
	}
	b.ObjStart()
	b.PutID("gameHighScores")
	b.Comma()
	b.FieldStart("scores")
	b.ArrStart()
	for idx, v := range g.Scores {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode gameHighScores#6d4cd30d: field scores element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("gameHighScores"); err != nil {
				return fmt.Errorf("unable to decode gameHighScores#6d4cd30d: %w", err)
			}
		case "scores":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value GameHighScore
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode gameHighScores#6d4cd30d: field scores: %w", err)
				}
				g.Scores = append(g.Scores, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode gameHighScores#6d4cd30d: field scores: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetScores returns value of Scores field.
func (g *GameHighScores) GetScores() (value []GameHighScore) {
	if g == nil {
		return
	}
	return g.Scores
}
