//
// Created by m on 12/6/21.
//

#include "sdlinputprocessor.hpp"

InputResult SdlInputProcessor::process_input() {
    SDL_Event event;
    InputResult result;
    while(SDL_PollEvent(&event)) {
        switch(event.type) {
            case SDL_QUIT: exit(EXIT_SUCCESS); // immediately exit here, eventually maybe make this na input
            case SDL_KEYDOWN: result.keycode = get_keycode(event.key.keysym.sym); break;
        }
    }
    return result;
}

SdlInputProcessor::SdlInputProcessor() {
    this->keycode_map[SDLK_UNKNOWN] = KEY_UNKNOWN;

    this->keycode_map[SDLK_RETURN] = KEY_RETURN;
    this->keycode_map[SDLK_ESCAPE] = KEY_ESCAPE;
    this->keycode_map[SDLK_BACKSPACE] = KEY_BACKSPACE;
    this->keycode_map[SDLK_TAB] = KEY_TAB;
    this->keycode_map[SDLK_SPACE] = KEY_SPACE;
    this->keycode_map[SDLK_EXCLAIM] = KEY_EXCLAIM;
    this->keycode_map[SDLK_QUOTEDBL] = KEY_QUOTEDBL;
    this->keycode_map[SDLK_HASH] = KEY_HASH;
    this->keycode_map[SDLK_PERCENT] = KEY_PERCENT;
    this->keycode_map[SDLK_DOLLAR] = KEY_DOLLAR;
    this->keycode_map[SDLK_AMPERSAND] = KEY_AMPERSAND;
    this->keycode_map[SDLK_QUOTE] = KEY_QUOTE;
    this->keycode_map[SDLK_LEFTPAREN] = KEY_LEFTPAREN;
    this->keycode_map[SDLK_RIGHTPAREN] = KEY_RIGHTPAREN;
    this->keycode_map[SDLK_ASTERISK] = KEY_ASTERISK;
    this->keycode_map[SDLK_PLUS] = KEY_PLUS;
    this->keycode_map[SDLK_COMMA] = KEY_COMMA;
    this->keycode_map[SDLK_MINUS] = KEY_MINUS;
    this->keycode_map[SDLK_PERIOD] = KEY_PERIOD;
    this->keycode_map[SDLK_SLASH] = KEY_SLASH;
    this->keycode_map[SDLK_0] = KEY_0;
    this->keycode_map[SDLK_1] = KEY_1;
    this->keycode_map[SDLK_2] = KEY_2;
    this->keycode_map[SDLK_3] = KEY_3;
    this->keycode_map[SDLK_4] = KEY_4;
    this->keycode_map[SDLK_5] = KEY_5;
    this->keycode_map[SDLK_6] = KEY_6;
    this->keycode_map[SDLK_7] = KEY_7;
    this->keycode_map[SDLK_8] = KEY_8;
    this->keycode_map[SDLK_9] = KEY_9;
    this->keycode_map[SDLK_COLON] = KEY_COLON;
    this->keycode_map[SDLK_SEMICOLON] = KEY_SEMICOLON;
    this->keycode_map[SDLK_LESS] = KEY_LESS;
    this->keycode_map[SDLK_EQUALS] = KEY_EQUALS;
    this->keycode_map[SDLK_GREATER] = KEY_GREATER;
    this->keycode_map[SDLK_QUESTION] = KEY_QUESTION;
    this->keycode_map[SDLK_AT] = KEY_AT;


    this->keycode_map[SDLK_LEFTBRACKET] = KEY_LEFTBRACKET;
    this->keycode_map[SDLK_BACKSLASH] = KEY_BACKSLASH;
    this->keycode_map[SDLK_RIGHTBRACKET] = KEY_RIGHTBRACKET;
    this->keycode_map[SDLK_CARET] = KEY_CARET;
    this->keycode_map[SDLK_UNDERSCORE] = KEY_UNDERSCORE;
    this->keycode_map[SDLK_BACKQUOTE] = KEY_BACKQUOTE;
    this->keycode_map[SDLK_a] = KEY_a;
    this->keycode_map[SDLK_b] = KEY_b;
    this->keycode_map[SDLK_c] = KEY_c;
    this->keycode_map[SDLK_d] = KEY_d;
    this->keycode_map[SDLK_e] = KEY_e;
    this->keycode_map[SDLK_f] = KEY_f;
    this->keycode_map[SDLK_g] = KEY_g;
    this->keycode_map[SDLK_h] = KEY_h;
    this->keycode_map[SDLK_i] = KEY_i;
    this->keycode_map[SDLK_j] = KEY_j;
    this->keycode_map[SDLK_k] = KEY_k;
    this->keycode_map[SDLK_l] = KEY_l;
    this->keycode_map[SDLK_m] = KEY_m;
    this->keycode_map[SDLK_n] = KEY_n;
    this->keycode_map[SDLK_o] = KEY_o;
    this->keycode_map[SDLK_p] = KEY_p;
    this->keycode_map[SDLK_q] = KEY_q;
    this->keycode_map[SDLK_r] = KEY_r;
    this->keycode_map[SDLK_s] = KEY_s;
    this->keycode_map[SDLK_t] = KEY_t;
    this->keycode_map[SDLK_u] = KEY_u;
    this->keycode_map[SDLK_v] = KEY_v;
    this->keycode_map[SDLK_w] = KEY_w;
    this->keycode_map[SDLK_x] = KEY_x;
    this->keycode_map[SDLK_y] = KEY_y;
    this->keycode_map[SDLK_z] = KEY_z;

    this->keycode_map[SDLK_CAPSLOCK] = KEY_CAPSLOCK;

    this->keycode_map[SDLK_F1] = KEY_F1;
    this->keycode_map[SDLK_F2] = KEY_F2;
    this->keycode_map[SDLK_F3] = KEY_F3;
    this->keycode_map[SDLK_F4] = KEY_F4;
    this->keycode_map[SDLK_F5] = KEY_F5;
    this->keycode_map[SDLK_F6] = KEY_F6;
    this->keycode_map[SDLK_F7] = KEY_F7;
    this->keycode_map[SDLK_F8] = KEY_F8;
    this->keycode_map[SDLK_F9] = KEY_F9;
    this->keycode_map[SDLK_F10] = KEY_F10;
    this->keycode_map[SDLK_F11] = KEY_F11;
    this->keycode_map[SDLK_F12] = KEY_F12;

    this->keycode_map[SDLK_PRINTSCREEN] = KEY_PRINTSCREEN;
    this->keycode_map[SDLK_SCROLLLOCK] = KEY_SCROLLLOCK;
    this->keycode_map[SDLK_PAUSE] = KEY_PAUSE;
    this->keycode_map[SDLK_INSERT] = KEY_INSERT;
    this->keycode_map[SDLK_HOME] = KEY_HOME;
    this->keycode_map[SDLK_PAGEUP] = KEY_PAGEUP;
    this->keycode_map[SDLK_DELETE] = KEY_DELETE;
    this->keycode_map[SDLK_END] = KEY_END;
    this->keycode_map[SDLK_PAGEDOWN] = KEY_PAGEDOWN;
    this->keycode_map[SDLK_RIGHT] = KEY_RIGHT;
    this->keycode_map[SDLK_LEFT] = KEY_LEFT;
    this->keycode_map[SDLK_DOWN] = KEY_DOWN;
    this->keycode_map[SDLK_UP] = KEY_UP;

    this->keycode_map[SDLK_NUMLOCKCLEAR] = KEY_NUMLOCKCLEAR;
    this->keycode_map[SDLK_KP_DIVIDE] = KEY_KP_DIVIDE;
    this->keycode_map[SDLK_KP_MULTIPLY] = KEY_KP_MULTIPLY;
    this->keycode_map[SDLK_KP_MINUS] = KEY_KP_MINUS;
    this->keycode_map[SDLK_KP_PLUS] = KEY_KP_PLUS;
    this->keycode_map[SDLK_KP_ENTER] = KEY_KP_ENTER;
    this->keycode_map[SDLK_KP_1] = KEY_KP_1;
    this->keycode_map[SDLK_KP_2] = KEY_KP_2;
    this->keycode_map[SDLK_KP_3] = KEY_KP_3;
    this->keycode_map[SDLK_KP_4] = KEY_KP_4;
    this->keycode_map[SDLK_KP_5] = KEY_KP_5;
    this->keycode_map[SDLK_KP_6] = KEY_KP_6;
    this->keycode_map[SDLK_KP_7] = KEY_KP_7;
    this->keycode_map[SDLK_KP_8] = KEY_KP_8;
    this->keycode_map[SDLK_KP_9] = KEY_KP_9;
    this->keycode_map[SDLK_KP_0] = KEY_KP_0;
    this->keycode_map[SDLK_KP_PERIOD] = KEY_KP_PERIOD;

    this->keycode_map[SDLK_APPLICATION] = KEY_APPLICATION;
    this->keycode_map[SDLK_POWER] = KEY_POWER;
    this->keycode_map[SDLK_KP_EQUALS] = KEY_KP_EQUALS;
    this->keycode_map[SDLK_F13] = KEY_F13;
    this->keycode_map[SDLK_F14] = KEY_F14;
    this->keycode_map[SDLK_F15] = KEY_F15;
    this->keycode_map[SDLK_F16] = KEY_F16;
    this->keycode_map[SDLK_F17] = KEY_F17;
    this->keycode_map[SDLK_F18] = KEY_F18;
    this->keycode_map[SDLK_F19] = KEY_F19;
    this->keycode_map[SDLK_F20] = KEY_F20;
    this->keycode_map[SDLK_F21] = KEY_F21;
    this->keycode_map[SDLK_F22] = KEY_F22;
    this->keycode_map[SDLK_F23] = KEY_F23;
    this->keycode_map[SDLK_F24] = KEY_F24;
    this->keycode_map[SDLK_EXECUTE] = KEY_EXECUTE;
    this->keycode_map[SDLK_HELP] = KEY_HELP;
    this->keycode_map[SDLK_MENU] = KEY_MENU;
    this->keycode_map[SDLK_SELECT] = KEY_SELECT;
    this->keycode_map[SDLK_STOP] = KEY_STOP;
    this->keycode_map[SDLK_AGAIN] = KEY_AGAIN;
    this->keycode_map[SDLK_UNDO] = KEY_UNDO;
    this->keycode_map[SDLK_CUT] = KEY_CUT;
    this->keycode_map[SDLK_COPY] = KEY_COPY;
    this->keycode_map[SDLK_PASTE] = KEY_PASTE;
    this->keycode_map[SDLK_FIND] = KEY_FIND;
    this->keycode_map[SDLK_MUTE] = KEY_MUTE;
    this->keycode_map[SDLK_VOLUMEUP] = KEY_VOLUMEUP;
    this->keycode_map[SDLK_VOLUMEDOWN] = KEY_VOLUMEDOWN;
    this->keycode_map[SDLK_KP_COMMA] = KEY_KP_COMMA;
    this->keycode_map[SDLK_KP_EQUALSAS400] = KEY_KP_EQUALSAS400;


    this->keycode_map[SDLK_ALTERASE] = KEY_ALTERASE;
    this->keycode_map[SDLK_SYSREQ] = KEY_SYSREQ;
    this->keycode_map[SDLK_CANCEL] = KEY_CANCEL;
    this->keycode_map[SDLK_CLEAR] = KEY_CLEAR;
    this->keycode_map[SDLK_PRIOR] = KEY_PRIOR;
    this->keycode_map[SDLK_RETURN2] = KEY_RETURN2;
    this->keycode_map[SDLK_SEPARATOR] = KEY_SEPARATOR;
    this->keycode_map[SDLK_OUT] = KEY_OUT;
    this->keycode_map[SDLK_OPER] = KEY_OPER;
    this->keycode_map[SDLK_CLEARAGAIN] = KEY_CLEARAGAIN;
    this->keycode_map[SDLK_CRSEL] = KEY_CRSEL;
    this->keycode_map[SDLK_EXSEL] = KEY_EXSEL;

    this->keycode_map[SDLK_KP_00] = KEY_KP_00;
    this->keycode_map[SDLK_KP_000] = KEY_KP_000;
    this->keycode_map[SDLK_THOUSANDSSEPARATOR] = KEY_THOUSANDSSEPARATOR;

    this->keycode_map[SDLK_DECIMALSEPARATOR] = KEY_DECIMALSEPARATOR;

    this->keycode_map[SDLK_CURRENCYUNIT] = KEY_CURRENCYUNIT;
    this->keycode_map[SDLK_CURRENCYSUBUNIT] = KEY_CURRENCYSUBUNIT;

    this->keycode_map[SDLK_KP_LEFTPAREN] = KEY_KP_LEFTPAREN;
    this->keycode_map[SDLK_KP_RIGHTPAREN] = KEY_KP_RIGHTPAREN;
    this->keycode_map[SDLK_KP_LEFTBRACE] = KEY_KP_LEFTBRACE;
    this->keycode_map[SDLK_KP_RIGHTBRACE] = KEY_KP_RIGHTBRACE;
    this->keycode_map[SDLK_KP_TAB] = KEY_KP_TAB;
    this->keycode_map[SDLK_KP_BACKSPACE] = KEY_KP_BACKSPACE;
    this->keycode_map[SDLK_KP_A] = KEY_KP_A;
    this->keycode_map[SDLK_KP_B] = KEY_KP_B;
    this->keycode_map[SDLK_KP_C] = KEY_KP_C;
    this->keycode_map[SDLK_KP_D] = KEY_KP_D;
    this->keycode_map[SDLK_KP_E] = KEY_KP_E;
    this->keycode_map[SDLK_KP_F] = KEY_KP_F;
    this->keycode_map[SDLK_KP_XOR] = KEY_KP_XOR;
    this->keycode_map[SDLK_KP_POWER] = KEY_KP_POWER;
    this->keycode_map[SDLK_KP_PERCENT] = KEY_KP_PERCENT;
    this->keycode_map[SDLK_KP_LESS] = KEY_KP_LESS;
    this->keycode_map[SDLK_KP_GREATER] = KEY_KP_GREATER;
    this->keycode_map[SDLK_KP_AMPERSAND] = KEY_KP_AMPERSAND;
    this->keycode_map[SDLK_KP_DBLAMPERSAND] = KEY_KP_DBLAMPERSAND;

    this->keycode_map[SDLK_KP_VERTICALBAR] = KEY_KP_VERTICALBAR;

    this->keycode_map[SDLK_KP_DBLVERTICALBAR] = KEY_KP_DBLVERTICALBAR;

    this->keycode_map[SDLK_KP_COLON] = KEY_KP_COLON;
    this->keycode_map[SDLK_KP_HASH] = KEY_KP_HASH;
    this->keycode_map[SDLK_KP_SPACE] = KEY_KP_SPACE;
    this->keycode_map[SDLK_KP_AT] = KEY_KP_AT;
    this->keycode_map[SDLK_KP_EXCLAM] = KEY_KP_EXCLAM;
    this->keycode_map[SDLK_KP_MEMSTORE] = KEY_KP_MEMSTORE;
    this->keycode_map[SDLK_KP_MEMRECALL] = KEY_KP_MEMRECALL;
    this->keycode_map[SDLK_KP_MEMCLEAR] = KEY_KP_MEMCLEAR;
    this->keycode_map[SDLK_KP_MEMADD] = KEY_KP_MEMADD;
    this->keycode_map[SDLK_KP_MEMSUBTRACT] = KEY_KP_MEMSUBTRACT;

    this->keycode_map[SDLK_KP_MEMMULTIPLY] = KEY_KP_MEMMULTIPLY;

    this->keycode_map[SDLK_KP_MEMDIVIDE] = KEY_KP_MEMDIVIDE;
    this->keycode_map[SDLK_KP_PLUSMINUS] = KEY_KP_PLUSMINUS;
    this->keycode_map[SDLK_KP_CLEAR] = KEY_KP_CLEAR;
    this->keycode_map[SDLK_KP_CLEARENTRY] = KEY_KP_CLEARENTRY;
    this->keycode_map[SDLK_KP_BINARY] = KEY_KP_BINARY;
    this->keycode_map[SDLK_KP_OCTAL] = KEY_KP_OCTAL;
    this->keycode_map[SDLK_KP_DECIMAL] = KEY_KP_DECIMAL;
    this->keycode_map[SDLK_KP_HEXADECIMAL] = KEY_KP_HEXADECIMAL;


    this->keycode_map[SDLK_LCTRL] = KEY_LCTRL;
    this->keycode_map[SDLK_LSHIFT] = KEY_LSHIFT;
    this->keycode_map[SDLK_LALT] = KEY_LALT;
    this->keycode_map[SDLK_LGUI] = KEY_LGUI;
    this->keycode_map[SDLK_RCTRL] = KEY_RCTRL;
    this->keycode_map[SDLK_RSHIFT] = KEY_RSHIFT;
    this->keycode_map[SDLK_RALT] = KEY_RALT;
    this->keycode_map[SDLK_RGUI] = KEY_RGUI;

    this->keycode_map[SDLK_MODE] = KEY_MODE;

    this->keycode_map[SDLK_AUDIONEXT] = KEY_AUDIONEXT;
    this->keycode_map[SDLK_AUDIOPREV] = KEY_AUDIOPREV;
    this->keycode_map[SDLK_AUDIOSTOP] = KEY_AUDIOSTOP;
    this->keycode_map[SDLK_AUDIOPLAY] = KEY_AUDIOPLAY;
    this->keycode_map[SDLK_AUDIOMUTE] = KEY_AUDIOMUTE;
    this->keycode_map[SDLK_MEDIASELECT] = KEY_MEDIASELECT;
    this->keycode_map[SDLK_WWW] = KEY_WWW;
    this->keycode_map[SDLK_MAIL] = KEY_MAIL;
    this->keycode_map[SDLK_CALCULATOR] = KEY_CALCULATOR;
    this->keycode_map[SDLK_COMPUTER] = KEY_COMPUTER;
    this->keycode_map[SDLK_AC_SEARCH] = KEY_AC_SEARCH;
    this->keycode_map[SDLK_AC_HOME] = KEY_AC_HOME;
    this->keycode_map[SDLK_AC_BACK] = KEY_AC_BACK;
    this->keycode_map[SDLK_AC_FORWARD] = KEY_AC_FORWARD;
    this->keycode_map[SDLK_AC_STOP] = KEY_AC_STOP;
    this->keycode_map[SDLK_AC_REFRESH] = KEY_AC_REFRESH;
    this->keycode_map[SDLK_AC_BOOKMARKS] = KEY_AC_BOOKMARKS;

    this->keycode_map[SDLK_BRIGHTNESSDOWN] = KEY_BRIGHTNESSDOWN;

    this->keycode_map[SDLK_BRIGHTNESSUP] = KEY_BRIGHTNESSUP;
    this->keycode_map[SDLK_DISPLAYSWITCH] = KEY_DISPLAYSWITCH;
    this->keycode_map[SDLK_KBDILLUMTOGGLE] = KEY_KBDILLUMTOGGLE;

    this->keycode_map[SDLK_KBDILLUMDOWN] = KEY_KBDILLUMDOWN;
    this->keycode_map[SDLK_KBDILLUMUP] = KEY_KBDILLUMUP;
    this->keycode_map[SDLK_EJECT] = KEY_EJECT;
    this->keycode_map[SDLK_SLEEP] = KEY_SLEEP;
    this->keycode_map[SDLK_APP1] = KEY_APP1;
    this->keycode_map[SDLK_APP2] = KEY_APP2;

    this->keycode_map[SDLK_AUDIOREWIND] = KEY_AUDIOREWIND;
    this->keycode_map[SDLK_AUDIOFASTFORWARD] = KEY_AUDIOFASTFORWARD;
}

InputProcessorKeycode SdlInputProcessor::get_keycode(SDL_Keycode keycode) {
    if(this->keycode_map.find(keycode) == this->keycode_map.end()) {
        return KEY_UNKNOWN;
    }
    return this->keycode_map[keycode];
}
