#pragma once

#include "base/base_core.h"
#include "base/base_math.h"
#include "clay.h"

typedef struct Renderer Renderer;

typedef struct RendererDesc {
    void    *window_handle;
    S32      width;
    S32      height;
    S32      frame_count;
} RendererDesc;

Renderer *renderer_create(RendererDesc *desc);

// creates a lightweight renderer that shares the GPU device, pipeline, shaders, font atlas, and icon textures from a
// parent renderer, but creates its own drawable surface (CAMetalLayer / swap chain), vertex/index buffers, and frame synchronization
// primitives. On each renderer_begin_frame(), shared renderers sync font atlas updates from the parent so Cmd+/- zoom works across all
// windows.
Renderer *renderer_create_shared(Renderer *parent, RendererDesc *desc);

void      renderer_destroy(Renderer *renderer);
B32       renderer_begin_frame(Renderer *renderer);
void      renderer_end_frame(Renderer *renderer, Clay_RenderCommandArray *render_commands);
void      renderer_resize(Renderer *renderer, S32 width, S32 height);
void      renderer_set_font_scale(Renderer *renderer, F32 scale);
void      renderer_sync_from_parent(Renderer *renderer);  // sync shared font atlas from parent
void      renderer_set_clear_color(Renderer *renderer, F32 r, F32 g, F32 b);

// Text measurement callback compatible with UI_MeasureTextFn
// Measures text of given length (NOT necessarily null-terminated) at font_size pixels.
// user_data should be the Renderer pointer.
Vec2F32 renderer_measure_text(const char *text, S32 length, F32 font_size, void *user_data);

// Upload an RGBA8 icon atlas texture for icon rendering (4 bytes per pixel)
void renderer_create_icon_atlas(Renderer *renderer, const U8 *data, S32 w, S32 h);
