#pragma once

#include <box3d/box3d.h>

#include "crStruct.h"

class crApp;
class crBatchedLines3D;

// box3d debug draw -> 3D wireframe lines (crBatchedLines3D), projected with the orbit camera.
// Shapes are drawn via world-def debug-shape handles: box3d calls CreateDebugShape once per shape
// (handing us the geometry), then DrawShape every frame with that handle + the body transform.
class crDevPhysics
{
private:
    // per-shape draw handle built in CreateDebugShape (geometry snapshot in local space)
    struct DebugShape
    {
        b3ShapeType type;
        b3Sphere    sphere;          // b3_sphereShape
        b3Capsule   capsule;         // b3_capsuleShape
        float3*     hullEdges;       // b3_hullShape: edge endpoint pairs, SDL_malloc'd
        int32_t     hullEdgeVerts;   // entries in hullEdges (2 per edge)
    };

    bool _debugDrawEnabled = false;

    crBatchedLines3D* _lineBatch = nullptr;
    crApp*            _app = nullptr;

    b3DebugDraw _debugDraw;

public:
    void Init( crApp* app );
    void Cleanup();
    void TryRenderDebugDraw();

    bool GetDebugDraw() const;
    void SetDebugDraw( bool enable );

    static void* CreateDebugShape( const b3DebugShape* debugShape, void* ctx );
    static void  DestroyDebugShape( void* userShape, void* ctx );

private:
    static bool DrawShape( void* userShape, b3WorldTransform transform, b3HexColor color, void* ctx );
    static void DrawSegment( b3Pos p1, b3Pos p2, b3HexColor color, void* ctx );
    static void DrawTransform( b3WorldTransform transform, void* ctx );
    static void DrawPoint( b3Pos p, float size, b3HexColor color, void* ctx );
    static void DrawSphere( b3Pos p, float radius, b3HexColor color, float alpha, void* ctx );
    static void DrawCapsule( b3Pos p1, b3Pos p2, float radius, b3HexColor color, float alpha, void* ctx );
    static void DrawBounds( b3AABB aabb, b3HexColor color, void* ctx );
    static void DrawBox( b3Vec3 extents, b3WorldTransform transform, b3HexColor color, void* ctx );
    static void DrawString( b3Pos p, const char* s, b3HexColor color, void* ctx );
};
