#pragma once

#include <enkiTS/TaskScheduler.h>
#include <entt/entt.hpp>

#include "crEcsComponents.h"

// enkiTS task adapter: splits the CTransform pool into ranges for parallel prev-snapshot
class crEcsSnapshotTask : public enki::ITaskSet
{
public:
    entt::registry* registry = nullptr;

    void ExecuteRange( enki::TaskSetPartition range, uint32_t threadIndex ) override;
};

class crEcs
{
public:
    static constexpr size_t INIT_CAPA_TRANSFORM    = 10000;
    static constexpr size_t INIT_CAPA_PHYSICS_BODY = 10000;
    static constexpr size_t INIT_CAPA_VELOCITY     = 10000;
    static constexpr size_t INIT_CAPA_PRIMITIVE         = 10000;

    static constexpr uint32_t SNAPSHOT_MIN_RANGE = 1000;   // enki grain size (elements per partition, minimum)

    entt::registry   registry;
    entt::dispatcher dispatcher;

private:
    crEcsSnapshotTask _snapshotTask;

public:
    void Init();
    void Cleanup();
    void Reset();

    void SnapshotPrevTransforms();
    void SyncDynamicTransforms( float fdt );

    uint64_t HashTransforms() const;   // determinism fingerprint of all CTransform.current

private:
    void RecreateRegistry();
};
