#include "m_menu_tecles.h"
#include "jdraw.h"
#include "jinput.h"
#include "jaudio.h"
#include "controller.h"
#include "config.h"
#include <SDL3/SDL.h>

namespace modules
{
    namespace menu_tecles
    {
        enum states { STATE_SELECTING, STATE_REDEFINING };

        const char *nom_tecles[] = {"?", "?", "?", "?", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
            "RETURN", "ESC", "BORRAR", "TAB", "ESPAI", "'", "j", "[", "]", "l", "?", "m", "k", "\\", ",", ".", "-", "BLOQMAYS", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
            "PRNTSCR", "SCRLOCK", "PAUSE", "INSERT", "INICI", "REPAG", "SUPR", "FIN", "AVPAG", "CUR-DRETA", "CUR-ESQ", "CUR-AVALL", "CUR-AMUNT",
            "NUM-LOCK", "NUM-DIV", "NUM-MUL", "NUM-MENYS", "NUM-SUM", "NUM-INTRO", "NUM-1", "NUM-2", "NUM-3", "NUM-4", "NUM-5", "NUM-6", "NUM-7", "NUM-8", "NUM-9", "NUM-0", "NUM-PUNT", "<", "MENU",
            // 101 << -- >> 224 (if num>101 then num-=122)
            "LCTRL", "LMAYUS", "LALT", "LGUI", "RCTRL", "RMAYUS", "RALT", "RGUI" };

        int selected_option = MENU_TECLES_AMUNT;
        states state = STATE_SELECTING;

        const char *getKeyName(const uint8_t key)
        {
            if (key-122>110) return nom_tecles[0];

            if (key>101)
                return nom_tecles[key-122];
            else
                return nom_tecles[key];
        }

        void init()
        {
            selected_option = MENU_TECLES_AMUNT;
        }

        int loop()
        {
            if (state == STATE_SELECTING)
            {
                if (controller::pressed(KEY_MENU)) {
                    return MENU_TECLES_TORNAR;
                }
                if (controller::pressed(KEY_DOWN) || input::keyPressed(SDL_SCANCODE_DOWN))
                {
                    audio::playSound("snd_push.wav", SOUND_BASIC);
                    selected_option++; if (selected_option>6) selected_option=0;
                }

                if (controller::pressed(KEY_UP) || input::keyPressed(SDL_SCANCODE_UP))
                {
                    audio::playSound("snd_push.wav", SOUND_BASIC);
                    selected_option--; if (selected_option<0) selected_option=6;
                }

                if (controller::pressed(KEY_JUMP) || controller::pressed(KEY_PICK) || input::keyPressed(SDL_SCANCODE_RETURN))
                {
                    audio::playSound("snd_push.wav", SOUND_BASIC);
                    if (selected_option==MENU_TECLES_TORNAR)
                        return MENU_TECLES_TORNAR;
                    else
                        state = STATE_REDEFINING;
                }
            } else {
                const uint8_t key = input::getKeyPressed();
                if (key != SDL_SCANCODE_UNKNOWN) {
                    if (key != SDL_SCANCODE_ESCAPE) config::defineKey(selected_option, key);
                    selected_option++;
                    if (selected_option==6) state = STATE_SELECTING;
                }
            }

            draw::cls(2);
            draw::color(1);

            draw::print2("REDEFINIR TECLES", 13, 3, TEAL, FONT_ZOOM_VERTICAL);

            int pos = 10;
            static const char *textos[6] = { "AMUNT:", "AVALL:", "ESQUERRA:", "DRETA:", "BOTAR: ", "AGAFAR: " };

            for (int i=0; i<6; ++i)
            {
                const bool selected = (i==selected_option);
                draw::print2(selected?"fg":"de", 8, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE);
                draw::print2(textos[i], 13, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE);
                if (!selected || state == STATE_SELECTING) draw::print2(getKeyName(config::getKey(i)), 23, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE);
                pos += selected?3:2;
            }

            const bool selected = (selected_option==MENU_TECLES_TORNAR);
            draw::print2(selected?"fg":"de", 8, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE);
            draw::print2("TORNAR", 13, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE);

            draw::print2("(C) JAILDOCTOR 2024", 11, 28, TEAL, FONT_ZOOM_NONE);

            draw::render();
            return MENU_TECLES_NONE;
        }
    }
}
