use servo::{Code, Key};

pub(crate) fn key_and_code_for_character(character: char) -> (Key, Code) {
    match character {
        '\n' | '\r' => (Key::Enter, Code::Enter),
        '\x08' => (Key::Backspace, Code::Backspace),
        '\t' => (Key::Tab, Code::Tab),
        '\x1b' => (Key::Escape, Code::Escape),
        '\x7f' => (Key::Delete, Code::Delete),
        '\u{F700}' => (Key::ArrowUp, Code::ArrowUp),
        '\u{F701}' => (Key::ArrowDown, Code::ArrowDown),
        '\u{F702}' => (Key::ArrowLeft, Code::ArrowLeft),
        '\u{F703}' => (Key::ArrowRight, Code::ArrowRight),
        '\u{F729}' => (Key::Home, Code::Home),
        '\u{F72B}' => (Key::End, Code::End),
        '\u{F72C}' => (Key::PageUp, Code::PageUp),
        '\u{F72D}' => (Key::PageDown, Code::PageDown),
        _ => (Key::Character(character.to_string()), printable_code(character)),
    }
}

fn printable_code(character: char) -> Code {
    match character {
        'a' | 'A' => Code::KeyA,
        'b' | 'B' => Code::KeyB,
        'c' | 'C' => Code::KeyC,
        'd' | 'D' => Code::KeyD,
        'e' | 'E' => Code::KeyE,
        'f' | 'F' => Code::KeyF,
        'g' | 'G' => Code::KeyG,
        'h' | 'H' => Code::KeyH,
        'i' | 'I' => Code::KeyI,
        'j' | 'J' => Code::KeyJ,
        'k' | 'K' => Code::KeyK,
        'l' | 'L' => Code::KeyL,
        'm' | 'M' => Code::KeyM,
        'n' | 'N' => Code::KeyN,
        'o' | 'O' => Code::KeyO,
        'p' | 'P' => Code::KeyP,
        'q' | 'Q' => Code::KeyQ,
        'r' | 'R' => Code::KeyR,
        's' | 'S' => Code::KeyS,
        't' | 'T' => Code::KeyT,
        'u' | 'U' => Code::KeyU,
        'v' | 'V' => Code::KeyV,
        'w' | 'W' => Code::KeyW,
        'x' | 'X' => Code::KeyX,
        'y' | 'Y' => Code::KeyY,
        'z' | 'Z' => Code::KeyZ,
        '0' => Code::Digit0,
        '1' => Code::Digit1,
        '2' => Code::Digit2,
        '3' => Code::Digit3,
        '4' => Code::Digit4,
        '5' => Code::Digit5,
        '6' => Code::Digit6,
        '7' => Code::Digit7,
        '8' => Code::Digit8,
        '9' => Code::Digit9,
        ' ' => Code::Space,
        '-' | '_' => Code::Minus,
        '=' | '+' => Code::Equal,
        ',' | '<' => Code::Comma,
        '.' | '>' => Code::Period,
        '/' | '?' => Code::Slash,
        ';' | ':' => Code::Semicolon,
        '\'' | '"' => Code::Quote,
        '[' | '{' => Code::BracketLeft,
        ']' | '}' => Code::BracketRight,
        '\\' | '|' => Code::Backslash,
        '`' | '~' => Code::Backquote,
        _ => Code::Unidentified,
    }
}
