#include "pce-sdl/scancodes_sdl.h"

struct SDLScanCodeMapping
{
  SDL_Scancode sdl_scancode;
  GenScanCode gen_scancode;
};

static const SDLScanCodeMapping sdl_mapping[] = {{SDL_SCANCODE_ESCAPE, GenScanCode_Escape},
                                                 {SDL_SCANCODE_F1, GenScanCode_F1},
                                                 {SDL_SCANCODE_F2, GenScanCode_F2},
                                                 {SDL_SCANCODE_F3, GenScanCode_F3},
                                                 {SDL_SCANCODE_F4, GenScanCode_F4},
                                                 {SDL_SCANCODE_F5, GenScanCode_F5},
                                                 {SDL_SCANCODE_F6, GenScanCode_F6},
                                                 {SDL_SCANCODE_F7, GenScanCode_F7},
                                                 {SDL_SCANCODE_F8, GenScanCode_F8},
                                                 {SDL_SCANCODE_F9, GenScanCode_F9},
                                                 {SDL_SCANCODE_F10, GenScanCode_F10},
                                                 {SDL_SCANCODE_F11, GenScanCode_F11},
                                                 {SDL_SCANCODE_F12, GenScanCode_F12},
                                                 {SDL_SCANCODE_1, GenScanCode_1},
                                                 {SDL_SCANCODE_2, GenScanCode_2},
                                                 {SDL_SCANCODE_3, GenScanCode_3},
                                                 {SDL_SCANCODE_4, GenScanCode_4},
                                                 {SDL_SCANCODE_5, GenScanCode_5},
                                                 {SDL_SCANCODE_6, GenScanCode_6},
                                                 {SDL_SCANCODE_7, GenScanCode_7},
                                                 {SDL_SCANCODE_8, GenScanCode_8},
                                                 {SDL_SCANCODE_9, GenScanCode_9},
                                                 {SDL_SCANCODE_0, GenScanCode_0},
                                                 {SDL_SCANCODE_MINUS, GenScanCode_Minus},
                                                 {SDL_SCANCODE_EQUALS, GenScanCode_Equals},
                                                 {SDL_SCANCODE_BACKSPACE, GenScanCode_Backspace},
                                                 {SDL_SCANCODE_TAB, GenScanCode_Tab},
                                                 {SDL_SCANCODE_Q, GenScanCode_Q},
                                                 {SDL_SCANCODE_W, GenScanCode_W},
                                                 {SDL_SCANCODE_E, GenScanCode_E},
                                                 {SDL_SCANCODE_R, GenScanCode_R},
                                                 {SDL_SCANCODE_T, GenScanCode_T},
                                                 {SDL_SCANCODE_Y, GenScanCode_Y},
                                                 {SDL_SCANCODE_U, GenScanCode_U},
                                                 {SDL_SCANCODE_I, GenScanCode_I},
                                                 {SDL_SCANCODE_O, GenScanCode_O},
                                                 {SDL_SCANCODE_P, GenScanCode_P},
                                                 {SDL_SCANCODE_LEFTBRACKET, GenScanCode_LeftBracket},
                                                 {SDL_SCANCODE_RIGHTBRACKET, GenScanCode_RightBracket},
                                                 {SDL_SCANCODE_BACKSLASH, GenScanCode_Backslash},
                                                 {SDL_SCANCODE_CAPSLOCK, GenScanCode_CapsLock},
                                                 {SDL_SCANCODE_A, GenScanCode_A},
                                                 {SDL_SCANCODE_S, GenScanCode_S},
                                                 {SDL_SCANCODE_D, GenScanCode_D},
                                                 {SDL_SCANCODE_F, GenScanCode_F},
                                                 {SDL_SCANCODE_G, GenScanCode_G},
                                                 {SDL_SCANCODE_H, GenScanCode_H},
                                                 {SDL_SCANCODE_J, GenScanCode_J},
                                                 {SDL_SCANCODE_K, GenScanCode_K},
                                                 {SDL_SCANCODE_L, GenScanCode_L},
                                                 {SDL_SCANCODE_SEMICOLON, GenScanCode_Semicolon},
                                                 {SDL_SCANCODE_APOSTROPHE, GenScanCode_Quote},
                                                 {SDL_SCANCODE_RETURN, GenScanCode_Return},
                                                 {SDL_SCANCODE_LSHIFT, GenScanCode_LeftShift},
                                                 {SDL_SCANCODE_Z, GenScanCode_Z},
                                                 {SDL_SCANCODE_X, GenScanCode_X},
                                                 {SDL_SCANCODE_C, GenScanCode_C},
                                                 {SDL_SCANCODE_V, GenScanCode_V},
                                                 {SDL_SCANCODE_B, GenScanCode_B},
                                                 {SDL_SCANCODE_N, GenScanCode_N},
                                                 {SDL_SCANCODE_M, GenScanCode_M},
                                                 {SDL_SCANCODE_COMMA, GenScanCode_Comma},
                                                 {SDL_SCANCODE_PERIOD, GenScanCode_Period},
                                                 {SDL_SCANCODE_SLASH, GenScanCode_Slash},
                                                 {SDL_SCANCODE_RSHIFT, GenScanCode_RightShift},
                                                 {SDL_SCANCODE_LCTRL, GenScanCode_LeftControl},
                                                 {SDL_SCANCODE_LGUI, GenScanCode_LeftSuper},
                                                 {SDL_SCANCODE_LALT, GenScanCode_LeftAlt},
                                                 {SDL_SCANCODE_SPACE, GenScanCode_Space},
                                                 {SDL_SCANCODE_RALT, GenScanCode_RightAlt},
                                                 {SDL_SCANCODE_RGUI, GenScanCode_RightSuper},
                                                 {SDL_SCANCODE_APPLICATION, GenScanCode_RightMenu},
                                                 {SDL_SCANCODE_RCTRL, GenScanCode_RightControl},
                                                 {SDL_SCANCODE_PRINTSCREEN, GenScanCode_PrintScreen},
                                                 {SDL_SCANCODE_SCROLLLOCK, GenScanCode_ScrollLock},
                                                 {SDL_SCANCODE_PAUSE, GenScanCode_PauseBreak},
                                                 {SDL_SCANCODE_GRAVE, GenScanCode_Backtick},
                                                 {SDL_SCANCODE_INSERT, GenScanCode_Insert},
                                                 {SDL_SCANCODE_HOME, GenScanCode_Home},
                                                 {SDL_SCANCODE_PAGEUP, GenScanCode_PageUp},
                                                 {SDL_SCANCODE_DELETE, GenScanCode_Delete},
                                                 {SDL_SCANCODE_END, GenScanCode_End},
                                                 {SDL_SCANCODE_PAGEDOWN, GenScanCode_PageDown},
                                                 {SDL_SCANCODE_UP, GenScanCode_Up},
                                                 {SDL_SCANCODE_LEFT, GenScanCode_Left},
                                                 {SDL_SCANCODE_DOWN, GenScanCode_Down},
                                                 {SDL_SCANCODE_RIGHT, GenScanCode_Right},
                                                 {SDL_SCANCODE_NUMLOCKCLEAR, GenScanCode_Numpad_NumLock},
                                                 {SDL_SCANCODE_KP_DIVIDE, GenScanCode_Numpad_Divide},
                                                 {SDL_SCANCODE_KP_MULTIPLY, GenScanCode_Numpad_Multiply},
                                                 {SDL_SCANCODE_KP_MINUS, GenScanCode_Numpad_Minus},
                                                 {SDL_SCANCODE_KP_PLUS, GenScanCode_Numpad_Plus},
                                                 {SDL_SCANCODE_KP_ENTER, GenScanCode_Numpad_Enter},
                                                 {SDL_SCANCODE_KP_1, GenScanCode_Numpad_1},
                                                 {SDL_SCANCODE_KP_2, GenScanCode_Numpad_2},
                                                 {SDL_SCANCODE_KP_3, GenScanCode_Numpad_3},
                                                 {SDL_SCANCODE_KP_4, GenScanCode_Numpad_4},
                                                 {SDL_SCANCODE_KP_5, GenScanCode_Numpad_5},
                                                 {SDL_SCANCODE_KP_6, GenScanCode_Numpad_6},
                                                 {SDL_SCANCODE_KP_7, GenScanCode_Numpad_7},
                                                 {SDL_SCANCODE_KP_8, GenScanCode_Numpad_8},
                                                 {SDL_SCANCODE_KP_9, GenScanCode_Numpad_9},
                                                 {SDL_SCANCODE_KP_0, GenScanCode_Numpad_0},
                                                 {SDL_SCANCODE_KP_DECIMAL, GenScanCode_Numpad_Decimal}};

bool MapSDLScanCode(GenScanCode* gen_scancode, SDL_Scancode sdl_scancode)
{
  for (size_t i = 0; i < countof(sdl_mapping); i++)
  {
    if (sdl_mapping[i].sdl_scancode == sdl_scancode)
    {
      *gen_scancode = sdl_mapping[i].gen_scancode;
      return true;
    }
  }

  return false;
}
