package legacypacket

import (
	"encoding/base64"

	"github.com/akmalfairuz/legacy-version/legacyver/proto"
	"github.com/samber/lo"
	"github.com/sandertv/gophertunnel/minecraft/protocol"
	"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)

// BiomeDefinitionList is sent by the server to let the client know all biomes that are available and
// implemented on the server side. When enabled, it also includes information for the client to
// accurately recreate the server-side generation in vanilla worlds/servers for increased performance.
type BiomeDefinitionList struct {
	// BiomeDefinitions is a list of biomes that are available on the server.
	BiomeDefinitions []proto.BiomeDefinition
	// StringList is a makeshift dictionary implementation Mojang created to try and reduce the size of the
	// overall packet. It is a list of common strings that are used in the biome definitions, such as
	// biome names, float values or query expressions.
	StringList []string
}

// ID ...
func (*BiomeDefinitionList) ID() uint32 {
	return packet.IDBiomeDefinitionList
}

func (pk *BiomeDefinitionList) Marshal(io protocol.IO) {
	if proto.IsProtoGTE(io, proto.ID800) {
		protocol.Slice(io, &pk.BiomeDefinitions)
		protocol.FuncSlice(io, &pk.StringList, io.String)
	} else {
		io.Bytes(lo.ToPtr(append([]byte(nil), rawOldSerializedBiomeDefinitionList...)))
	}
}

var rawOldSerializedBiomeDefinitionList []byte

func init() {
	const s = `[encoded data, 4552 characters]`
	rawOldSerializedBiomeDefinitionList, _ = base64.StdEncoding.DecodeString(s)
}
