#pragma once

#if defined( __EMSCRIPTEN__ ) || defined( __ANDROID__ )
#include <GLES3/gl3.h>
#else
#include <glad/glad.h>
#endif

#include "crEcsComponents.h"

class crApp;
class crBatchedPrimitives;

class crRenderSystem
{
private:
    crBatchedPrimitives* _primitives = nullptr;

public:

    void Init();
    void Cleanup();

    // scene pass bracket — Begin: PP scene target + batches + primitive pass / End: PP execute + text draw.
    // text queued between the two calls is drawn by End; dev overlays follow after in crApp::Render
    void BeginScenePass( const crApp* app, float alpha );
    void EndScenePass( const crApp* app );

    // world-anchored, fixed pixel size (font = crFontAtlas::LoadFont handle); queued, drawn in EndScenePass
    // pivot: normalized point of the text block placed at the anchor — (0,0)=top-left, (0.5,0.5)=center, (1,1)=bottom-right
    void RenderTextWorld( const crApp* app, const char* utf8, float3 worldPos, int32_t font, color4 color, float scale = 1.0f, float2 pivot = { 0.0f, 0.0f } );
    void RenderTextScreen( const crApp* app, const char* utf8, float2 screenPosPx, int32_t font, color4 color, float scale = 1.0f, float2 pivot = { 0.0f, 0.0f } );   // pixel-ortho anchor (bottom-left, Y-up); no dev overlay hooks

    void ClearUnusedMemory();

    uint8_t RegisterPrimitiveMaterial( GLuint program );   // forwards to crBatchedPrimitives::RegisterMaterial — the contract lives there

    const crBatchedPrimitives* Primitives() const   // dev stats
    {
        return _primitives;
    }

private:
    void FillPrimitives( const crApp* app, float alpha, const crFrustum& cameraFrustum, const crFrustum* cascadeFrustums );   // ECS -> instance arrays (frustum-culled), CTransform previous->current interpolated at alpha
};
