use caw_computer_keyboard::{Key, Keyboard};
use sdl2::keyboard::Scancode;

pub fn sdl2_scancode_to_key(scancode: Scancode) -> Option<Key> {
    let key = match scancode {
        Scancode::A => Key::A,
        Scancode::B => Key::B,
        Scancode::C => Key::C,
        Scancode::D => Key::D,
        Scancode::E => Key::E,
        Scancode::F => Key::F,
        Scancode::G => Key::G,
        Scancode::H => Key::H,
        Scancode::I => Key::I,
        Scancode::J => Key::J,
        Scancode::K => Key::K,
        Scancode::L => Key::L,
        Scancode::M => Key::M,
        Scancode::N => Key::N,
        Scancode::O => Key::O,
        Scancode::P => Key::P,
        Scancode::Q => Key::Q,
        Scancode::R => Key::R,
        Scancode::S => Key::S,
        Scancode::T => Key::T,
        Scancode::U => Key::U,
        Scancode::V => Key::V,
        Scancode::W => Key::W,
        Scancode::X => Key::X,
        Scancode::Y => Key::Y,
        Scancode::Z => Key::Z,
        Scancode::Num0 => Key::N0,
        Scancode::Num1 => Key::N1,
        Scancode::Num2 => Key::N2,
        Scancode::Num3 => Key::N3,
        Scancode::Num4 => Key::N4,
        Scancode::Num5 => Key::N5,
        Scancode::Num6 => Key::N6,
        Scancode::Num7 => Key::N7,
        Scancode::Num8 => Key::N8,
        Scancode::Num9 => Key::N9,
        Scancode::LeftBracket => Key::LeftBracket,
        Scancode::RightBracket => Key::RightBracket,
        Scancode::Semicolon => Key::Semicolon,
        Scancode::Apostrophe => Key::Apostrophe,
        Scancode::Comma => Key::Comma,
        Scancode::Period => Key::Period,
        Scancode::Minus => Key::Minus,
        Scancode::Equals => Key::Equals,
        Scancode::Slash => Key::Slash,
        Scancode::Space => Key::Space,
        Scancode::Backspace => Key::Backspace,
        Scancode::Backslash => Key::Backslash,
        _ => return None,
    };
    Some(key)
}

pub fn sdl2_scancode_get<T>(
    keyboard: &Keyboard<T>,
    scancode: Scancode,
) -> Option<&T> {
    let key = match scancode {
        Scancode::A => &keyboard.a,
        Scancode::B => &keyboard.b,
        Scancode::C => &keyboard.c,
        Scancode::D => &keyboard.d,
        Scancode::E => &keyboard.e,
        Scancode::F => &keyboard.f,
        Scancode::G => &keyboard.g,
        Scancode::H => &keyboard.h,
        Scancode::I => &keyboard.i,
        Scancode::J => &keyboard.j,
        Scancode::K => &keyboard.k,
        Scancode::L => &keyboard.l,
        Scancode::M => &keyboard.m,
        Scancode::N => &keyboard.n,
        Scancode::O => &keyboard.o,
        Scancode::P => &keyboard.p,
        Scancode::Q => &keyboard.q,
        Scancode::R => &keyboard.r,
        Scancode::S => &keyboard.s,
        Scancode::T => &keyboard.t,
        Scancode::U => &keyboard.u,
        Scancode::V => &keyboard.v,
        Scancode::W => &keyboard.w,
        Scancode::X => &keyboard.x,
        Scancode::Y => &keyboard.y,
        Scancode::Z => &keyboard.z,
        Scancode::Num0 => &keyboard.n0,
        Scancode::Num1 => &keyboard.n1,
        Scancode::Num2 => &keyboard.n2,
        Scancode::Num3 => &keyboard.n3,
        Scancode::Num4 => &keyboard.n4,
        Scancode::Num5 => &keyboard.n5,
        Scancode::Num6 => &keyboard.n6,
        Scancode::Num7 => &keyboard.n7,
        Scancode::Num8 => &keyboard.n8,
        Scancode::Num9 => &keyboard.n9,
        Scancode::LeftBracket => &keyboard.left_bracket,
        Scancode::RightBracket => &keyboard.right_bracket,
        Scancode::Semicolon => &keyboard.semicolon,
        Scancode::Apostrophe => &keyboard.apostrophe,
        Scancode::Comma => &keyboard.comma,
        Scancode::Period => &keyboard.period,
        Scancode::Minus => &keyboard.minus,
        Scancode::Equals => &keyboard.equals,
        Scancode::Slash => &keyboard.slash,
        Scancode::Space => &keyboard.space,
        Scancode::Backspace => &keyboard.backspace,
        Scancode::Backslash => &keyboard.backslash,
        _ => return None,
    };
    Some(key)
}

pub fn sdl2_scancode_get_mut<T>(
    keyboard: &mut Keyboard<T>,
    scancode: Scancode,
) -> Option<&mut T> {
    let key = match scancode {
        Scancode::A => &mut keyboard.a,
        Scancode::B => &mut keyboard.b,
        Scancode::C => &mut keyboard.c,
        Scancode::D => &mut keyboard.d,
        Scancode::E => &mut keyboard.e,
        Scancode::F => &mut keyboard.f,
        Scancode::G => &mut keyboard.g,
        Scancode::H => &mut keyboard.h,
        Scancode::I => &mut keyboard.i,
        Scancode::J => &mut keyboard.j,
        Scancode::K => &mut keyboard.k,
        Scancode::L => &mut keyboard.l,
        Scancode::M => &mut keyboard.m,
        Scancode::N => &mut keyboard.n,
        Scancode::O => &mut keyboard.o,
        Scancode::P => &mut keyboard.p,
        Scancode::Q => &mut keyboard.q,
        Scancode::R => &mut keyboard.r,
        Scancode::S => &mut keyboard.s,
        Scancode::T => &mut keyboard.t,
        Scancode::U => &mut keyboard.u,
        Scancode::V => &mut keyboard.v,
        Scancode::W => &mut keyboard.w,
        Scancode::X => &mut keyboard.x,
        Scancode::Y => &mut keyboard.y,
        Scancode::Z => &mut keyboard.z,
        Scancode::Num0 => &mut keyboard.n0,
        Scancode::Num1 => &mut keyboard.n1,
        Scancode::Num2 => &mut keyboard.n2,
        Scancode::Num3 => &mut keyboard.n3,
        Scancode::Num4 => &mut keyboard.n4,
        Scancode::Num5 => &mut keyboard.n5,
        Scancode::Num6 => &mut keyboard.n6,
        Scancode::Num7 => &mut keyboard.n7,
        Scancode::Num8 => &mut keyboard.n8,
        Scancode::Num9 => &mut keyboard.n9,
        Scancode::LeftBracket => &mut keyboard.left_bracket,
        Scancode::RightBracket => &mut keyboard.right_bracket,
        Scancode::Semicolon => &mut keyboard.semicolon,
        Scancode::Apostrophe => &mut keyboard.apostrophe,
        Scancode::Comma => &mut keyboard.comma,
        Scancode::Period => &mut keyboard.period,
        Scancode::Minus => &mut keyboard.minus,
        Scancode::Equals => &mut keyboard.equals,
        Scancode::Slash => &mut keyboard.slash,
        Scancode::Space => &mut keyboard.space,
        Scancode::Backspace => &mut keyboard.backspace,
        Scancode::Backslash => &mut keyboard.backslash,
        _ => return None,
    };
    Some(key)
}
