haj 0.3.6

the beautiful, cross-distro package manager and tui.
use super::theme::Theme;
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph, Wrap},
};

pub fn render_popup(f: &mut Frame, theme: &Theme) {
    let area = centered_rect(60, 80, f.area());

    f.render_widget(Clear, area);

    let block = Block::default()
        .title(" haj keybinds reference ")
        .title_alignment(Alignment::Center)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.accent));

    let help_text = vec![
        Line::from(Span::styled(
            "dashboard",
            Style::default()
                .fg(theme.secondary)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled("─────────", Style::default().fg(theme.muted))),
        format_bind(theme, "tab / enter", "switch widget / open browser or news"),
        format_bind(theme, "/ / f", "open package browser"),
        format_bind(theme, "u", "system upgrade (sync + upgrade)"),
        format_bind(theme, "n / t / h / g", "news / stats / history / groups"),
        format_bind(theme, "o / c", "sweep orphans / clean cache"),
        format_bind(theme, "b / r", "toggle blahaj / refresh data"),
        Line::from(""),
        Line::from(Span::styled(
            "package browser",
            Style::default()
                .fg(theme.secondary)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled(
            "───────────────",
            Style::default().fg(theme.muted),
        )),
        format_bind(theme, "j / k / g / G", "move cursor / top / bottom"),
        format_bind(theme, "/ / esc", "search repos & aur / exit search"),
        format_bind(theme, "tab / S", "cycle filter / cycle sort"),
        format_bind(
            theme,
            "space / a / A",
            "toggle select / all visible / clear",
        ),
        format_bind(theme, "i / r", "install / remove selected"),
        format_bind(theme, "1 - 4", "overview / deps / files / queue"),
        format_bind(theme, "ctrl+u / ctrl+d", "scroll details pane"),
        format_bind(theme, "R", "refresh package index"),
        Line::from(""),
        Line::from(Span::styled(
            "global",
            Style::default()
                .fg(theme.secondary)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled("──────", Style::default().fg(theme.muted))),
        format_bind(
            theme,
            "esc / ? / q",
            "back to dashboard / toggle help / quit",
        ),
    ];

    let paragraph = Paragraph::new(help_text)
        .block(block)
        .wrap(Wrap { trim: false });

    f.render_widget(paragraph, area);
}

fn format_bind(theme: &Theme, key: &str, desc: &str) -> Line<'static> {
    Line::from(vec![
        Span::styled(format!("{key:<14}"), Style::default().fg(theme.accent)),
        Span::styled(desc.to_string(), Style::default().fg(theme.text)),
    ])
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints(
            [
                Constraint::Percentage((100 - percent_y) / 2),
                Constraint::Percentage(percent_y),
                Constraint::Percentage((100 - percent_y) / 2),
            ]
            .as_ref(),
        )
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints(
            [
                Constraint::Percentage((100 - percent_x) / 2),
                Constraint::Percentage(percent_x),
                Constraint::Percentage((100 - percent_x) / 2),
            ]
            .as_ref(),
        )
        .split(popup_layout[1])[1]
}