
#ifndef BEATRAT_SETTINGS_H
#define BEATRAT_SETTINGS_H

#include <X11/Xlib.h>

#define SETTINGS_MAX_ALT_DIRS 6

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

#define SETTINGS_MAX_HIDDEN_DEVICES 32

typedef struct {
    char default_import_dir[512];
    char default_export_dir[512];
    int always_on_top;
    AltDir alt_dirs[SETTINGS_MAX_ALT_DIRS];
    int num_alt_dirs;
    char output_device[128]; /* empty = auto (PipeWire-preferred/ALSA-fallback) */
    char input_device[128];  /* empty = auto */
    int volume_percent;      /* software playback gain, 0-150; 100 = unity */
    char hidden_devices[SETTINGS_MAX_HIDDEN_DEVICES][128]; /* device names hidden from Prev/Next cycling */
    int num_hidden_devices;
    int auto_deselect_region; /* clear a region (time) selection after copy/cut/void -- on by default */
} Settings;

/* Shared layout constants so main.c's click hit-testing lines up
 * exactly with what settings_draw() actually draws. */
#define SETTINGS_BOX_W          520
#define SETTINGS_BOX_H          368
#define SETTINGS_IMPORT_FIELD_Y 46
#define SETTINGS_EXPORT_FIELD_Y 90
#define SETTINGS_FIELD_H        22
#define SETTINGS_CHECKBOX_X     8
#define SETTINGS_CHECKBOX_Y     126
#define SETTINGS_CHECKBOX_SIZE  16
/* Second checkbox row, directly below "Always on top" -- see
 * settings_draw()/Settings.auto_deselect_region. */
#define SETTINGS_CHECKBOX2_Y    148
/* Opens the separate Keybinds editor overlay (see keybinds_menu.h) --
 * sits in the gap between the checkboxes and the alternate-directory
 * section below, which is why that section shifted down 26px to make
 * room (190->216, 220->246). */
#define SETTINGS_KEYBINDS_BTN_Y  172
#define SETTINGS_KEYBINDS_BTN_W  100
#define SETTINGS_KEYBINDS_BTN_H  20
#define SETTINGS_ALTDIR_FIELD_Y 216
#define SETTINGS_ALTDIR_LIST_Y  246
#define SETTINGS_ALTDIR_ROW_H   20
#define SETTINGS_ALTDIR_REMOVE_W 18

void settings_set_defaults(Settings *s);

/* Loads from ~/.config/beatrat/settings.conf (or $XDG_CONFIG_HOME
 * equivalent) if present, falling back to defaults for anything
 * missing or if the file doesn't exist at all yet. */
void settings_load(Settings *s);

/* Writes the current settings to disk, creating ~/.config/beatrat if
 * needed. Returns 0 on success. */
int settings_save(const Settings *s);

/* Generic field editing -- pass whichever of Settings' path buffers is
 * currently focused (both are the same size, so one pair of functions
 * covers both fields rather than duplicating per-field). */
void settings_append_char_to(char *field, size_t field_size, char c);
void settings_backspace_from(char *field);

/* Alternate import directories the user has added, for the quick-swap
 * shortcut row in the Import file picker. Label is derived from the
 * path's last component. Returns the new entry's index, or -1 if the
 * list is full or the path is already present. */
int settings_add_alt_dir(Settings *s, const char *path);
void settings_remove_alt_dir(Settings *s, int idx);

/* Resolves (and creates if missing) ~/.local/share/beatrat/projects
 * (or $XDG_DATA_HOME equivalent) -- where native Save writes project
 * folders. This is app data, not configuration, so it deliberately
 * lives separately from settings.conf's ~/.config/beatrat. Not a
 * user-facing setting. */
void settings_projects_dir(char *buf, size_t buf_size);

/* Resolves (and creates if missing) ~/.local/share/beatrat/samples --
 * a personal sample library, separate from both saved projects and
 * the configured export directory. */
void settings_samples_dir(char *buf, size_t buf_size);

/* Devices hidden from the Audio panel's Prev/Next cycling (e.g. a
 * monitor's HDMI output the user never actually wants). Shared between
 * input and output -- a device name is either hidden everywhere or
 * nowhere, since the same physical device can appear in both lists. */
int settings_is_device_hidden(const Settings *s, const char *name);
/* Returns 0 on success, -1 if already hidden or the list is full. */
int settings_hide_device(Settings *s, const char *name);
void settings_unhide_device(Settings *s, const char *name);

/* active_field: 0 = import directory, 1 = export directory, 2 = the
 * "add alternate directory" entry field. altdir_input is that entry
 * field's live (unsaved-until-Enter) text, owned by the caller. */
void settings_draw(const Settings *s, Display *dpy, Window win, GC gc,
                    int win_w, int win_h, int active_field, const char *altdir_input);

#endif

