use crate::page::Page;

pub fn bindings_for(page: Page, plugin_mode: bool) -> Vec<(&'static str, &'static str, &'static str)> {
    let mut bindings = vec![
        ("F1–F7", "Go to view", "Dict/Patterns/Options/Help/Sequencer/Engine/Script"),
        ("Ctrl+←→↑↓", "Navigate", "Switch between adjacent views"),
    ];
    if !plugin_mode {
        bindings.push(("q", "Quit", "Quit application"));
    }
    bindings.extend([
        ("s", "Save", "Save project"),
        ("l", "Load", "Load project"),
        ("?", "Keybindings", "Show this help"),
        ("F12", "Restart", "Full restart from step 0"),
    ]);

    // Page-specific bindings
    match page {
        Page::Main => {
            if !plugin_mode {
                bindings.push(("Space", "Play/Stop", "Toggle playback"));
            }
            bindings.push(("Alt+↑↓", "Pattern", "Previous/next pattern"));
            bindings.push(("Alt+←→", "Bank", "Previous/next bank"));
            bindings.push(("←→↑↓", "Navigate", "Move cursor between steps"));
            bindings.push(("Shift+←→↑↓", "Select", "Extend selection"));
            bindings.push(("Esc", "Clear", "Clear selection"));
            bindings.push(("Enter", "Edit", "Open step editor"));
            bindings.push(("t", "Toggle", "Toggle selected steps"));
            bindings.push(("p", "Prelude", "Edit prelude script"));
            bindings.push(("Tab", "Samples", "Toggle sample browser"));
            bindings.push(("Ctrl+C", "Copy", "Copy selected steps"));
            bindings.push(("Ctrl+V", "Paste", "Paste steps"));
            bindings.push(("Ctrl+B", "Link", "Paste as linked steps"));
            bindings.push(("Ctrl+D", "Duplicate", "Duplicate selection"));
            bindings.push(("Ctrl+H", "Harden", "Convert links to copies"));
            bindings.push(("Del", "Delete", "Delete step(s)"));
            bindings.push(("< >", "Length", "Decrease/increase pattern length"));
            bindings.push(("[ ]", "Speed", "Decrease/increase pattern speed"));
            if !plugin_mode {
                bindings.push(("+ -", "Tempo", "Increase/decrease tempo"));
                bindings.push(("T", "Set tempo", "Open tempo input"));
            }
            bindings.push(("L", "Set length", "Open length input"));
            bindings.push(("S", "Set speed", "Open speed input"));
            bindings.push(("f", "Fill", "Toggle fill mode (hold)"));
            bindings.push(("r", "Rename", "Rename current step"));
            bindings.push(("Ctrl+R", "Run", "Run step script immediately"));
            bindings.push((":", "Jump", "Jump to step number"));
            bindings.push(("e", "Euclidean", "Distribute linked steps using Euclidean rhythm"));
            bindings.push(("m", "Mute", "Stage mute for current pattern"));
            bindings.push(("x", "Solo", "Stage solo for current pattern"));
            bindings.push(("M", "Clear mutes", "Clear all mutes"));
            bindings.push(("X", "Clear solos", "Clear all solos"));
            bindings.push(("d", "Eval prelude", "Re-evaluate prelude without editing"));
            bindings.push(("g", "Share", "Export pattern to clipboard"));
            bindings.push(("G", "Import", "Import pattern from clipboard"));
        }
        Page::Patterns => {
            bindings.push(("←→↑↓", "Navigate", "Move between banks/patterns"));
            bindings.push(("Shift+↑↓", "Select", "Extend selection"));
            bindings.push(("Alt+↑↓", "Shift", "Move patterns up/down"));
            bindings.push(("Enter", "Select", "Select pattern for editing"));
            if !plugin_mode {
                bindings.push(("Space", "Play", "Toggle pattern playback"));
            }
            bindings.push(("Esc", "Back", "Clear staged or go back"));
            bindings.push(("c", "Commit", "Commit staged changes"));
            bindings.push(("p", "Stage play", "Stage pattern play toggle"));
            bindings.push(("r", "Rename", "Rename bank/pattern"));
            bindings.push(("d", "Describe", "Add description to pattern"));
            bindings.push(("e", "Properties", "Edit pattern properties"));
            bindings.push(("m", "Mute", "Stage mute for pattern"));
            bindings.push(("x", "Solo", "Stage solo for pattern"));
            bindings.push(("M", "Clear mutes", "Clear all mutes"));
            bindings.push(("X", "Clear solos", "Clear all solos"));
            bindings.push(("g", "Share", "Export bank or pattern to clipboard"));
            bindings.push(("G", "Import", "Import bank or pattern from clipboard"));
            bindings.push(("Ctrl+C", "Copy", "Copy bank/pattern"));
            bindings.push(("Ctrl+V", "Paste", "Paste bank/pattern"));
            bindings.push(("Del", "Reset", "Reset bank/pattern"));
            bindings.push(("Ctrl+Z", "Undo", "Undo last action"));
            bindings.push(("Ctrl+Shift+Z", "Redo", "Redo last action"));
        }
        Page::Engine => {
            bindings.push(("Tab", "Section", "Next section"));
            bindings.push(("Shift+Tab", "Section", "Previous section"));
            bindings.push(("←→", "Switch", "Switch device type or adjust setting"));
            bindings.push(("↑↓", "Navigate", "Navigate list items"));
            bindings.push(("PgUp/Dn", "Page", "Page through device list"));
            bindings.push(("Enter", "Select", "Select device"));
            if !plugin_mode {
                bindings.push(("R", "Restart", "Restart audio engine"));
            }
            bindings.push(("A", "Add path", "Add sample path"));
            bindings.push(("D", "Refresh/Del", "Refresh devices or delete path"));
            bindings.push(("h", "Hush", "Stop all sounds gracefully"));
            bindings.push(("p", "Panic", "Stop all sounds immediately"));
            bindings.push(("r", "Reset", "Reset peak voice counter"));
            if !plugin_mode {
                bindings.push(("t", "Test", "Play test tone"));
            }
        }
        Page::Options => {
            bindings.push(("Tab", "Next", "Move to next option"));
            bindings.push(("Shift+Tab", "Previous", "Move to previous option"));
            bindings.push(("↑↓", "Navigate", "Navigate options"));
            bindings.push(("←→", "Toggle", "Toggle or adjust option"));
            if !plugin_mode {
                bindings.push(("Space", "Play/Stop", "Toggle playback"));
            }
        }
        Page::Help => {
            bindings.push(("↑↓ j/k", "Scroll", "Scroll content"));
            bindings.push(("Tab", "Topic", "Next topic"));
            bindings.push(("Shift+Tab", "Topic", "Previous topic"));
            bindings.push(("PgUp/Dn", "Page", "Page scroll"));
            bindings.push(("n", "Next code", "Jump to next code block"));
            bindings.push(("p", "Prev code", "Jump to previous code block"));
            bindings.push(("Enter", "Run code", "Execute focused code block"));
            bindings.push(("/", "Search", "Activate search"));
            bindings.push(("Esc", "Clear", "Clear search / deselect block"));
        }
        Page::Dict => {
            bindings.push(("Tab", "Focus", "Toggle category/words focus"));
            bindings.push(("↑↓ j/k", "Navigate", "Navigate items"));
            bindings.push(("PgUp/Dn", "Page", "Page scroll"));
            bindings.push(("/", "Search", "Activate search"));
            bindings.push(("Ctrl+F", "Search", "Activate search"));
            bindings.push(("Esc", "Clear", "Clear search"));
        }
        Page::Script => {
            bindings.push(("Enter", "Focus", "Focus editor for typing"));
            bindings.push(("Esc", "Unfocus", "Unfocus editor to use page keybindings"));
            bindings.push(("Ctrl+E", "Evaluate", "Compile and check for errors (focused)"));
            bindings.push(("S", "Set Speed", "Set script speed via text input (unfocused)"));
            bindings.push(("L", "Set Length", "Set script length via text input (unfocused)"));
            bindings.push(("Ctrl+S", "Stack", "Toggle stack preview (focused)"));
        }
    }

    bindings
}
