use iced_core::SmolStr;

use iced_core::keyboard::Key as IcedKey;

// TODO: modifier
pub fn key_from_u32(value: u32) -> IcedKey {
    use iced_core::keyboard::key::Named;
    match value {
        0 => IcedKey::Unidentified,
        1 => IcedKey::Named(Named::Escape),
        code @ 2..=10 => IcedKey::Character(SmolStr::new((code - 1).to_string())),
        11 => IcedKey::Character(SmolStr::new("0")),
        12 => IcedKey::Character(SmolStr::new("-")),
        13 => IcedKey::Character(SmolStr::new("=")),
        14 => IcedKey::Named(Named::Backspace),
        15 => IcedKey::Named(Named::Tab),
        16 => IcedKey::Character(SmolStr::new("q")),
        17 => IcedKey::Character(SmolStr::new("w")),
        18 => IcedKey::Character(SmolStr::new("e")),
        19 => IcedKey::Character(SmolStr::new("r")),
        20 => IcedKey::Character(SmolStr::new("t")),
        21 => IcedKey::Character(SmolStr::new("y")),
        22 => IcedKey::Character(SmolStr::new("u")),
        23 => IcedKey::Character(SmolStr::new("i")),
        24 => IcedKey::Character(SmolStr::new("o")),
        25 => IcedKey::Character(SmolStr::new("p")),
        26 => IcedKey::Character(SmolStr::new("[")),
        27 => IcedKey::Character(SmolStr::new("]")),
        28 => IcedKey::Named(Named::Enter),
        29 | 97 => IcedKey::Named(Named::Control),
        30 => IcedKey::Character(SmolStr::new("a")),
        31 => IcedKey::Character(SmolStr::new("s")),
        32 => IcedKey::Character(SmolStr::new("d")),
        33 => IcedKey::Character(SmolStr::new("f")),
        34 => IcedKey::Character(SmolStr::new("g")),
        35 => IcedKey::Character(SmolStr::new("h")),
        36 => IcedKey::Character(SmolStr::new("j")),
        37 => IcedKey::Character(SmolStr::new("k")),
        38 => IcedKey::Character(SmolStr::new("l")),
        39 => IcedKey::Character(SmolStr::new(";")),
        40 => IcedKey::Character(SmolStr::new("'")),
        41 => IcedKey::Character(SmolStr::new("'")),
        42 | 54 => IcedKey::Named(Named::Shift),
        43 => IcedKey::Character(SmolStr::new("\\")),
        44 => IcedKey::Character(SmolStr::new("z")),
        45 => IcedKey::Character(SmolStr::new("x")),
        46 => IcedKey::Character(SmolStr::new("c")),
        47 => IcedKey::Character(SmolStr::new("v")),
        48 => IcedKey::Character(SmolStr::new("b")),
        49 => IcedKey::Character(SmolStr::new("n")),
        50 => IcedKey::Character(SmolStr::new("m")),
        51 => IcedKey::Character(SmolStr::new(",")),
        52 => IcedKey::Character(SmolStr::new(".")),
        53 => IcedKey::Character(SmolStr::new("/")),
        // TODO: 55
        56 | 100 => IcedKey::Named(Named::Alt),
        57 => IcedKey::Named(Named::Space),
        58 => IcedKey::Named(Named::CapsLock),
        59 => IcedKey::Named(Named::F1),
        60 => IcedKey::Named(Named::F2),
        61 => IcedKey::Named(Named::F3),
        62 => IcedKey::Named(Named::F4),
        63 => IcedKey::Named(Named::F5),
        64 => IcedKey::Named(Named::F6),
        65 => IcedKey::Named(Named::F7),
        66 => IcedKey::Named(Named::F8),
        67 => IcedKey::Named(Named::F9),
        68 => IcedKey::Named(Named::F10),
        69 => IcedKey::Named(Named::NumLock),
        70 => IcedKey::Named(Named::ScrollLock),
        71 => IcedKey::Character(SmolStr::new("7")),
        72 => IcedKey::Character(SmolStr::new("8")),
        73 => IcedKey::Character(SmolStr::new("9")),
        74 => IcedKey::Character(SmolStr::new("-")),
        75 => IcedKey::Character(SmolStr::new("4")),
        76 => IcedKey::Character(SmolStr::new("5")),
        77 => IcedKey::Character(SmolStr::new("6")),
        78 => IcedKey::Character(SmolStr::new("+")),
        79 => IcedKey::Character(SmolStr::new("1")),
        80 => IcedKey::Character(SmolStr::new("2")),
        81 => IcedKey::Character(SmolStr::new("3")),
        82 => IcedKey::Character(SmolStr::new("0")),
        103 => IcedKey::Named(Named::ArrowUp),
        104 => IcedKey::Named(Named::PageUp),
        105 => IcedKey::Named(Named::ArrowLeft),
        106 => IcedKey::Named(Named::ArrowRight),
        107 => IcedKey::Named(Named::End),
        108 => IcedKey::Named(Named::ArrowDown),
        _ => IcedKey::Unidentified,
    }
}

pub fn text_from_key(key: &IcedKey) -> Option<SmolStr> {
    use iced_core::keyboard::key::Named;
    match key {
        IcedKey::Character(c) => Some(c.clone()),
        IcedKey::Named(Named::Space) => Some(SmolStr::new(" ")),
        _ => None,
    }
}
