/// Resolves the quick-toggle key from env, defaulting to Pause/Break.
fn quick_toggle_key_from_env() -> Option<KeyCode> {
    match std::env::var("LESAVKA_INPUT_TOGGLE_KEY") {
        Ok(raw) => parse_quick_toggle_key(&raw),
        Err(_) => Some(KeyCode::KEY_PAUSE),
    }
}

/// Parses a launcher/operator key alias into an evdev key code.
fn parse_quick_toggle_key(raw: &str) -> Option<KeyCode> {
    let normalized = raw.trim().to_ascii_lowercase();
    if matches!(normalized.as_str(), "" | "off" | "none" | "disabled") {
        return None;
    }

    if let Some(letter) = parse_quick_toggle_letter(&normalized) {
        return Some(letter);
    }

    if let Some(digit) = parse_quick_toggle_digit(&normalized) {
        return Some(digit);
    }

    if let Some(function) = parse_quick_toggle_function_key(&normalized) {
        return Some(function);
    }

    match normalized.as_str() {
        "scrolllock" | "scroll_lock" | "scroll-lock" => Some(KeyCode::KEY_SCROLLLOCK),
        "sysrq" | "sysreq" | "prtsc" | "printscreen" | "print_screen" | "print-screen" => {
            Some(KeyCode::KEY_SYSRQ)
        }
        "pause" | "pausebreak" | "pause_break" | "pause-break" => Some(KeyCode::KEY_PAUSE),
        "escape" | "esc" => Some(KeyCode::KEY_ESC),
        "tab" => Some(KeyCode::KEY_TAB),
        "capslock" | "caps_lock" | "caps-lock" => Some(KeyCode::KEY_CAPSLOCK),
        "backspace" | "back_space" | "back-space" => Some(KeyCode::KEY_BACKSPACE),
        "space" | "spacebar" => Some(KeyCode::KEY_SPACE),
        "enter" | "return" => Some(KeyCode::KEY_ENTER),
        "insert" => Some(KeyCode::KEY_INSERT),
        "delete" | "del" => Some(KeyCode::KEY_DELETE),
        "home" => Some(KeyCode::KEY_HOME),
        "end" => Some(KeyCode::KEY_END),
        "pageup" | "page_up" | "page-up" => Some(KeyCode::KEY_PAGEUP),
        "pagedown" | "page_down" | "page-down" => Some(KeyCode::KEY_PAGEDOWN),
        "left" => Some(KeyCode::KEY_LEFT),
        "right" => Some(KeyCode::KEY_RIGHT),
        "up" => Some(KeyCode::KEY_UP),
        "down" => Some(KeyCode::KEY_DOWN),
        _ => Some(KeyCode::KEY_PAUSE),
    }
}

fn parse_quick_toggle_letter(raw: &str) -> Option<KeyCode> {
    match raw {
        "a" => Some(KeyCode::KEY_A),
        "b" => Some(KeyCode::KEY_B),
        "c" => Some(KeyCode::KEY_C),
        "d" => Some(KeyCode::KEY_D),
        "e" => Some(KeyCode::KEY_E),
        "f" => Some(KeyCode::KEY_F),
        "g" => Some(KeyCode::KEY_G),
        "h" => Some(KeyCode::KEY_H),
        "i" => Some(KeyCode::KEY_I),
        "j" => Some(KeyCode::KEY_J),
        "k" => Some(KeyCode::KEY_K),
        "l" => Some(KeyCode::KEY_L),
        "m" => Some(KeyCode::KEY_M),
        "n" => Some(KeyCode::KEY_N),
        "o" => Some(KeyCode::KEY_O),
        "p" => Some(KeyCode::KEY_P),
        "q" => Some(KeyCode::KEY_Q),
        "r" => Some(KeyCode::KEY_R),
        "s" => Some(KeyCode::KEY_S),
        "t" => Some(KeyCode::KEY_T),
        "u" => Some(KeyCode::KEY_U),
        "v" => Some(KeyCode::KEY_V),
        "w" => Some(KeyCode::KEY_W),
        "x" => Some(KeyCode::KEY_X),
        "y" => Some(KeyCode::KEY_Y),
        "z" => Some(KeyCode::KEY_Z),
        _ => None,
    }
}

fn parse_quick_toggle_digit(raw: &str) -> Option<KeyCode> {
    match raw {
        "1" => Some(KeyCode::KEY_1),
        "2" => Some(KeyCode::KEY_2),
        "3" => Some(KeyCode::KEY_3),
        "4" => Some(KeyCode::KEY_4),
        "5" => Some(KeyCode::KEY_5),
        "6" => Some(KeyCode::KEY_6),
        "7" => Some(KeyCode::KEY_7),
        "8" => Some(KeyCode::KEY_8),
        "9" => Some(KeyCode::KEY_9),
        "0" => Some(KeyCode::KEY_0),
        _ => None,
    }
}

fn parse_quick_toggle_function_key(raw: &str) -> Option<KeyCode> {
    match raw {
        "f1" => Some(KeyCode::KEY_F1),
        "f2" => Some(KeyCode::KEY_F2),
        "f3" => Some(KeyCode::KEY_F3),
        "f4" => Some(KeyCode::KEY_F4),
        "f5" => Some(KeyCode::KEY_F5),
        "f6" => Some(KeyCode::KEY_F6),
        "f7" => Some(KeyCode::KEY_F7),
        "f8" => Some(KeyCode::KEY_F8),
        "f9" => Some(KeyCode::KEY_F9),
        "f10" => Some(KeyCode::KEY_F10),
        "f11" => Some(KeyCode::KEY_F11),
        "f12" => Some(KeyCode::KEY_F12),
        _ => None,
    }
}
