#include "ui.h"

#include <curses.h>

static const char *HELP_LINES[] = {
    "Sidebar (focus with Tab / \xe2\x86\x90):",
    "  \xe2\x86\x91/k \xe2\x86\x93/j       select filter category (All/Downloading/Uploading/Paused/Completed)",
    "  Enter / \xe2\x86\x92 / Tab  move focus to the list",
    "",
    "List view (focus with Tab / \xe2\x86\x92):",
    "  \xe2\x86\x91/k \xe2\x86\x93/j       move selection",
    "  PgUp/PgDn      page up/down",
    "  Tab / \xe2\x86\x90         move focus to the sidebar",
    "  Enter          show details for the selected torrent",
    "  space          select/deselect torrent (for batch actions)",
    "  s / S / p      start / stop / toggle start-stop",
    "  v              verify (recheck)",
    "  d / Delete     remove (asks whether to delete data)",
    "  l              set speed limit",
    "  a              add torrent (magnet/URL/file)",
    "  r              force refresh",
    "  o / O          next sort column / reverse direction",
    "  /              search by name",
    "  Esc            clear search / deselect",
    "  g              settings",
    "  q              quit",
    "",
    "Detail view:",
    "  Tab / 1-4      switch tab (General/Files/Peers/Trackers)",
    "  space          (Files) select/skip file",
    "  + / -          (Files) raise/lower priority",
    "  Esc/q          back to the list",
    "",
    "Settings (opened with g):",
    "  \xe2\x86\x91/k \xe2\x86\x93/j       navigate fields",
    "  Enter/space    change value / toggle on/off",
    "  Esc/q          close",
    "",
    "Press any key to go back.",
};

void ui_help_render(AppState *st)
{
    (void)st;
    int rows, cols;
    getmaxyx(stdscr, rows, cols);
    erase();

    int h = rows - 2;
    int w = cols - 2;
    WINDOW *win = derwin(stdscr, h, w, 1, 1);
    ui_box(win, h, w);
    ui_box_title(win, w, "Help");

    size_t n = sizeof(HELP_LINES) / sizeof(HELP_LINES[0]);
    for (size_t i = 0; i < n && (int)i + 2 < h; i++)
        mvwprintw(win, (int)i + 2, 2, "%.*s", w - 4, HELP_LINES[i]);

    wnoutrefresh(win);
    delwin(win);
    doupdate();
}

void ui_help_handle_key(AppState *st, int ch)
{
    (void)ch;
    st->view = VIEW_LIST;
}
