#ifndef BEATRAT_FILE_PICKER_H
#define BEATRAT_FILE_PICKER_H

#include <X11/Xlib.h>
#include <stddef.h>

#define FILEPICKER_MAX_MATCHES 32
#define FILEPICKER_MAX_QUERY   128
#define FILEPICKER_MAX_NAME    256
#define FILEPICKER_MAX_SHORTCUTS 6

#define FILEPICKER_BOX_W 500
#define FILEPICKER_BOX_H 380
#define FILEPICKER_SHORTCUT_ROW_Y 34
#define FILEPICKER_SHORTCUT_ROW_H 20
#define FILEPICKER_LIST_Y 60

typedef enum {
    FILEPICKER_MODE_WAV_FILES,  /* used for Import: lists *.wav files */
    FILEPICKER_MODE_PROJECT_DIRS /* used for Open Project: lists subdirectories */
} FilePickerMode;

typedef struct {
    char label[64];
    char path[512];
} FilePickerShortcut;

/* A typed, incrementally-filtered picker -- same idea as snippysnappy's
 * OpenPromptApp fuzzy finder, ported to plain X11/C. Deliberately not a
 * native GTK/Qt file dialog: that would be a real new dependency,
 * whereas this needs nothing beyond what we already link, and it fits
 * the keyboard-first philosophy better anyway.
 *
 * Shortcuts (a row of quick-swap directory tabs, e.g. "Default" /
 * "Projects" / any alternate directories from Settings) persist across
 * filepicker_open() calls -- only set them again if the list actually
 * changed. This is what lets clicking a shortcut re-scan a different
 * directory without closing the picker. */
typedef struct {
    char base_dir[512];
    FilePickerMode mode;
    char query[FILEPICKER_MAX_QUERY];
    int query_len;
    char matches[FILEPICKER_MAX_MATCHES][FILEPICKER_MAX_NAME];
    int num_matches;
    int selected;
    FilePickerShortcut shortcuts[FILEPICKER_MAX_SHORTCUTS];
    int num_shortcuts;
} FilePicker;

/* Resets the query and scans base_dir, filtering by mode. Does NOT
 * touch the shortcuts list -- call filepicker_set_shortcuts separately
 * (typically once, right after this, the first time the picker opens). */
void filepicker_open(FilePicker *fp, const char *base_dir, FilePickerMode mode);

void filepicker_set_shortcuts(FilePicker *fp, const FilePickerShortcut *shortcuts, int num_shortcuts);

void filepicker_append_char(FilePicker *fp, char c);
void filepicker_backspace(FilePicker *fp);
void filepicker_move_selection(FilePicker *fp, int delta);

/* Writes the full path of the currently highlighted match into out.
 * Returns 0 on success, -1 if there are no matches to select. */
int filepicker_selected_path(const FilePicker *fp, char *out, size_t out_size);

/* Hit-tests the shortcut tab row against a click at (click_x, click_y)
 * within a window of size win_w x win_h (same box-centering math as
 * filepicker_draw). Returns the shortcut index, or -1 if the click
 * wasn't on a shortcut tab. */
int filepicker_shortcut_at(const FilePicker *fp, int win_w, int win_h, int click_x, int click_y);

/* Draws the picker as a centered modal-looking overlay box, including
 * the shortcut tab row if any shortcuts are set. */
void filepicker_draw(const FilePicker *fp, Display *dpy, Window win, GC gc,
                      int win_w, int win_h);

#endif
