package ecs

import (
	
	
	
	
)

// entityID is the type for entity IDs.
type entityID uint32

// runtime type of entities
var entityType = reflect.TypeFor[Entity]()

// memory size of an Entity
var entitySize = entityType.Size()

// memory size of an entityIndex
var entityIndexSize = reflect.TypeFor[entityIndex]().Size()

//var wildcard = Entity{1, 0}

// Entity is an identifier for entities.
//
// Can be stored safely in components, resources or elsewhere.
// For stored entities, it may be necessary to check their alive status with [World.Alive].
//
// ⚠️ Always store entities by value, never by pointer!
//
// In Ark, entities are returned to a pool when they are removed from the world.
// These entities can be recycled, with the same ID ([Entity.ID]), but an incremented generation ([Entity.Gen]).
// This allows to determine whether an entity hold by the user is still alive, despite it was potentially recycled.
type Entity struct {
	id  entityID // Entity ID
	gen uint32   // Entity generation
}

// newEntity creates a new entity with the given ID.
func newEntity( entityID) Entity {
	return Entity{, 0}
}

// ID returns the entity's ID, primarily for debugging purposes.
func ( Entity) () uint32 {
	return uint32(.id)
}

// Gen returns the entity's generation, primarily for debugging purposes.
func ( Entity) () uint32 {
	return .gen
}

// IsZero returns whether this entity is the reserved zero entity.
func ( Entity) () bool {
	return .id == 0
}

// isWildcard returns whether this entity is the reserved wildcard entity.
func ( Entity) () bool {
	return .id == 1
}

// MarshalJSON returns a JSON representation of the entity, for serialization purposes.
//
// The JSON representation of an entity is a two-element array of entity ID and generation.
func ( Entity) () ([]byte, error) {
	 := [2]uint32{uint32(.id), .gen}
	,  := json.Marshal() // Ignore the error, as we can be sure this works.
	return , nil
}

// UnmarshalJSON into an entity.
//
// For serialization purposes only. Do not use this to create entities!
func ( *Entity) ( []byte) error {
	 := [2]uint32{}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}
	.id = entityID([0])
	.gen = [1]

	return nil
}

// MarshalBinary returns a binary representation of the entity, for serialization and networking purposes.
func ( *Entity) () ( []byte,  error) {
	 := make([]byte, 8)
	binary.BigEndian.PutUint32([0:4], uint32(.id))
	binary.BigEndian.PutUint32([4:8], .gen)
	return , nil
}

// AppendBinary appends the binary representation of the entity to the given slice,
// for serialization and networking purposes.
func ( *Entity) ( []byte) ([]byte, error) {
	 = binary.BigEndian.AppendUint32(, uint32(.id))
	 = binary.BigEndian.AppendUint32(, .gen)
	return , nil
}

// UnmarshalBinary into an entity.
//
// For serialization and networking purposes only. Do not use this to create entities!
func ( *Entity) ( []byte) error {
	if len() != 8 {
		return fmt.Errorf("invalid data length: expected 8 bytes, got %d", len())
	}
	.id = entityID(binary.BigEndian.Uint32([0:4]))
	.gen = binary.BigEndian.Uint32([4:8])
	return nil
}

// entityIndex denotes an entity's location by table and row index.
type entityIndex struct {
	table tableID
	row   uint32
}