
#ifndef BEATRAT_UI_H
#define BEATRAT_UI_H

#include <X11/Xlib.h>
#include "track.h"

/* Must be called once, after the display/colormap exist, before any
 * other ui_draw_* function. Allocates all the palette colors. */
void ui_init_colors(Display *dpy, Colormap cmap);

/* Draws a beveled rectangle -- raised normally, sunken when pressed is
 * true (used for buttons, and for anything else that wants that look). */
void ui_draw_bevel(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h, int pressed);

/* A bevel box with a centered text label. */
void ui_draw_button(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h,
                     const char *label, int pressed);

/* Named, pre-allocated colors for the mode/context indicator badges
 * (main.c's redraw() -- interaction mode and track/region nav context
 * are two SEPARATE badges, deliberately not folded into one string,
 * so each is readable at a glance without parsing text). Pastel rather
 * than saturated, so plain black text (ui_draw_button's usual look)
 * still reads clearly against every one of them. */
typedef enum {
    UI_BADGE_NEUTRAL = 0, /* NORMAL mode -- looks like the regular UI, nothing special */
    UI_BADGE_GREEN,       /* INSERT mode */
    UI_BADGE_PURPLE,      /* FX mode */
    UI_BADGE_ORANGE,      /* BEAT mode */
    UI_BADGE_BLUE,        /* TRACKS nav context */
    UI_BADGE_PINK,        /* REGION nav context */
    UI_BADGE_COUNT
} UiBadgeColor;

/* A small colored, beveled box with a centered label -- same shape as
 * ui_draw_button but with a caller-chosen background color instead of
 * the fixed XP gray, for the mode/context indicators. */
void ui_draw_badge(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h,
                    const char *label, UiBadgeColor color);

/* Transport/track-button glyphs, drawn as plain vector shapes instead
 * of text -- frees up horizontal space (no need for a button wide
 * enough to spell "Restart") and reads at a glance the way a media
 * player's buttons do. Record/Stop use red, matching the universal
 * "recording" convention; everything else is plain black, same as
 * ui_draw_button's text. */
typedef enum {
    UI_ICON_NONE = 0,
    UI_ICON_RECORD,   /* filled red circle */
    UI_ICON_STOP,     /* filled red square */
    UI_ICON_PLAY,     /* black right-pointing triangle */
    UI_ICON_PAUSE,    /* two black vertical bars */
    UI_ICON_LOOP,     /* circular arrow */
    UI_ICON_RESTART,  /* |<< -- bar plus a double left-pointing arrow */
    UI_ICON_DELETE,   /* X */
    UI_ICON_UP,       /* upward-pointing triangle */
    UI_ICON_DOWN,     /* downward-pointing triangle */
    UI_ICON_PENCIL,   /* rename -- a diagonal pencil body with a small nib */
    UI_ICON_SPEAKER,       /* mute toggle, unmuted state -- speaker + sound lines */
    UI_ICON_SPEAKER_MUTED, /* mute toggle, muted state -- speaker + an X over it */
} UiIcon;

/* Same bevel/pressed shape as ui_draw_button, but with a centered
 * vector glyph instead of a text label. */
void ui_draw_icon_button(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h,
                          UiIcon icon, int pressed);

/* Number of colors in the track palette (see ui.c) -- tracks cycle
 * through these by index so each one is visually distinct, the way
 * Audacity assigns a color per track. Same palette snippysnappy uses
 * for its drawing tools. */
#define UI_PALETTE_SIZE 5

/* Track row layout, shared with main.c so click hit-testing lines up
 * exactly with what's drawn. A row splits into a fixed-width label
 * strip (name, and -- when selected -- Delete/Re-record buttons) and
 * the waveform filling the rest. */
#define UI_TRACK_LABEL_W    120
#define UI_TRACK_BTN_Y_OFF  32
#define UI_TRACK_BTN_H      18
#define UI_TRACK_DEL_BTN_X  4
#define UI_TRACK_DEL_BTN_W  50
#define UI_TRACK_REC_BTN_X  62
#define UI_TRACK_REC_BTN_W  50
#define UI_TRACK_BTN2_Y_OFF 52
#define UI_TRACK_UP_BTN_X   4
#define UI_TRACK_UP_BTN_W   50
#define UI_TRACK_DN_BTN_X   62
#define UI_TRACK_DN_BTN_W   50
/* Rename -- a small pencil-icon button in the top-right corner of the
 * label strip, shown under the same selected-or-highlighted condition
 * as Del/Rec/Up/Dn (see ui_draw_track_row()'s comment), as an explicit
 * alternative to the existing double-click-to-rename gesture. */
#define UI_TRACK_RENAME_BTN_X (UI_TRACK_LABEL_W - 18)
#define UI_TRACK_RENAME_BTN_Y 2
#define UI_TRACK_RENAME_BTN_W 16
#define UI_TRACK_RENAME_BTN_H 14
/* Mute -- top-left corner of the label strip (mirroring rename's
 * top-right spot), ALWAYS visible rather than gated on selected/
 * highlighted like Del/Rec/Up/Dn/rename -- see ui_draw_track_row()'s
 * comment. The track name's own XDrawString x-offset is nudged right
 * by this button's width so the two never overlap. */
#define UI_TRACK_MUTE_BTN_X 2
#define UI_TRACK_MUTE_BTN_Y 2
#define UI_TRACK_MUTE_BTN_W 16
#define UI_TRACK_MUTE_BTN_H 14

/* Peak-based waveform, rendered against a VIEWPORT into the shared
 * project timeline: column 0 shows the sample at view_start_frame,
 * and each pixel column covers samples_per_pixel samples going right.
 * This replaces the old "stretch the whole timeline into the row"
 * approach -- it's what makes zoom and horizontal scroll possible.
 * A track shorter than the current view position just stops drawing
 * (shows background) rather than stretching. Standard "zoomed"
 * min/max peak rendering, the same idea Audacity and others use. */
void ui_draw_waveform(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h,
                       const int16_t *samples, size_t num_samples,
                       size_t view_start_frame, double samples_per_pixel, unsigned long color);

/* Draws one track's row: a fixed-width label strip (name, and -- when
 * selected -- Delete/Re-record/Up/Down buttons) plus the waveform
 * filling the rest, against the current viewport. `index` picks this
 * track's color from the palette (index % UI_PALETTE_SIZE). `selected`
 * draws a highlighted border and the inline per-track buttons.
 * `highlighted` (keyboard nav cursor, distinct from `selected` until
 * confirmed) draws a lighter blue border when true and `selected` is
 * false -- `selected` wins if both are true, since it's the stronger,
 * locked-in state. */
/* hl_x0/hl_x1 (absolute x pixels, same space as x/w) mark a time-region
 * selection to tint within this row's waveform area -- hl_x1 <= hl_x0
 * means no highlight for this row. The tint sits under the waveform
 * (drawn right after the row's own background, before the border,
 * divider, label, or waveform peaks) and is clipped to the waveform
 * area, never the label strip. Pass hl_x1<=hl_x0 for a track that
 * isn't part of the current selection -- its ordinary opaque
 * background then covers whatever selection band main.c drew under
 * the whole track area, which is what makes that band read as
 * "selected tracks only" despite spanning every row. */
void ui_draw_track_row(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h,
                        const Track *t, int index, size_t view_start_frame,
                        double samples_per_pixel, int selected, int highlighted,
                        int hl_x0, int hl_x1);

/* Draws a time ruler (tick marks + mm:ss.t labels) for the given
 * viewport, choosing a "nice" interval (0.1s/0.5s/1s/5s/10s/30s/60s/...)
 * so ticks land at least ~50px apart regardless of zoom level. */
void ui_draw_time_ruler(Display *dpy, Drawable d, GC gc, int x, int y, int w, int h,
                         size_t view_start_frame, double samples_per_pixel, uint32_t sample_rate);

#endif

