// render.h - UI scaling, colormaps, spectrogram texture generation, drawing
#ifndef RENDER_H
#define RENDER_H

#include "spectrogram_types.h"

// --- UI scaling & scaled text ---
float GetUIScale(void);
void  DrawTextScaled(const char* text, float x, float y, float baseSize, Color color);
float MeasureTextScaled(const char* text, float baseSize);

// --- DPI-aware UI font ---
// InitUIFont remembers the TTF path and bakes the first atlas; EnsureUIFont
// rebuilds it (cheap no-op when unchanged) whenever the effective pixel density
// (window UI scale * monitor DPI scale) shifts, keeping text crisp on any
// display instead of thinning out on standard-DPI screens. Call EnsureUIFont
// once per frame before drawing text.
void InitUIFont(const char* path);
void EnsureUIFont(void);

// --- Colormaps ---
void GenerateColormapTexture(void);
const char* ColormapName(ColormapType type);

// --- Spectrogram texture ---
// GenerateSpectrogramTexture recomputes the synchrosqueezed reassignment (use
// when the STFT changes). ColorizeSpectrogram only re-maps the cached
// reassignment to colors (use for dB-floor / colormap changes).
void GenerateSpectrogramTexture(StftResult* stft, Image* image, Texture2D* texture);
void ColorizeSpectrogram(Image* image, Texture2D* texture);
// Colormap lookup (0-1 -> colour); used by the sidebar's colormap swatch.
Color GetColormapColor(float t, ColormapType type);

// --- Headless (no-GL) spectrogram + annotation rendering ---
// BuildSpectrogramImageCPU fills `image` with the colorized spectrogram with no
// texture upload, so it runs with no window / no X server. LoadFontCPU returns
// a Font usable by ImageDrawTextEx without a GL context (glyphCount==0 on
// failure). DrawAnnotationsToImage paints the mLnL overlay onto a full-
// resolution spectrogram Image, honoring the kind filter and resting opacity.
void BuildSpectrogramImageCPU(StftResult* stft, Image* image);
Font LoadFontCPU(const char* path, int baseSize);
void DrawAnnotationsToImage(Image* img, Font font);

// --- On-screen drawing (operate on the global app state) ---
void DrawSpectrogramGrid(Rectangle bounds, int numCellsX, int numCellsY, Color color);
void DrawLabels(Rectangle bounds);
void DrawSelection(Rectangle bounds);

// --- Selection transport bar ---
// Play controls attached to the selection box. Geometry and input are split
// from drawing so a click can be claimed during the input phase, before the
// pan/select handlers run.
Rectangle SelectionTransportRect(Rectangle bounds);
bool TransportCapturesMouse(Rectangle bounds);
void UpdateSelectionTransport(Rectangle bounds);
void DrawSelectionTransport(Rectangle bounds);
void DrawSelectionDrag(Rectangle bounds);
void DrawCursorReadout(Rectangle bounds);
void DrawMarkers(Rectangle bounds);
void DrawSpectrumPanel(Rectangle bounds);
void DrawPlayhead(Rectangle bounds);
// Playhead X within bounds, or <0 when there's nothing to draw. Used by the
// scrub hit-test so the marker and its grab region agree.
float PlayheadScreenX(Rectangle bounds);
void DrawAnnotations(Rectangle bounds);

// --- Minimap ---
// Whole-file thumbnail in the top-right corner with a viewport rectangle.
// Returns the rect it occupies (zero-sized when hidden) so the caller can
// route clicks to it. The texture is cached; InvalidateMinimap() forces a
// rebuild on the next draw.
Rectangle DrawMinimap(void);
// Minimap rect without drawing, plus a hit test — needed during input handling,
// which runs long before the draw pass.
Rectangle MinimapBounds(void);
bool MinimapCapturesMouse(void);
Rectangle MinimapHandleRect(void);   // corner size-toggle square
void InvalidateMinimap(void);
// Recompute which annotations overlap in time+frequency. Call after the
// annotation set changes; result is cached in app.collisionFlags/Regions.
void ComputeCollisions(void);
// Annotation timeline lane. Updates app.hoveredTimelineEvent and
// app.selectedAnnotation in response to mouse interaction in `lane`.
void DrawTimeline(Rectangle lane);
// Time-only annotation overlay rendered on top of the waveform scope.
// Shares opacity/selection state with the spectrogram overlay.
void DrawAnnotationsOnScope(Rectangle scopeBounds);

#endif // RENDER_H
