#include "m_menu_gamepad.h"
#include "jdraw.h"
#include "jinput.h"
#include "jaudio.h"
#include "controller.h"
#include "config.h"
#include <SDL2/SDL.h>

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

        int selected_option = MENU_GAMEPAD_AMUNT;
        states state = STATE_SELECTING;
        char name[7] = "BTN 00";

        const char *getPadBtnName(const int8_t key)
        {
            name[4] = (key/10)+48;
            name[5] = (key%10)+48;
            return name;
        }

        void init()
        {
            selected_option = MENU_GAMEPAD_AMUNT;
        }

        int loop()
        {
            if (state == STATE_SELECTING)
            {
                if (controller::pressed(KEY_MENU)) {
                    return MENU_GAMEPAD_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_GAMEPAD_TORNAR)
                        return MENU_GAMEPAD_TORNAR;
                    else
                        state = STATE_REDEFINING;
                }
            } else {
                const int8_t btn = input::getPadBtnPressed();
                if (btn != SDL_CONTROLLER_BUTTON_INVALID) {
                    config::definePadBtn(selected_option, btn);
                    selected_option++;
                    if (selected_option==6) state = STATE_SELECTING;
                }
                if (input::keyPressed(SDL_SCANCODE_ESCAPE)) state = STATE_SELECTING;
            }

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

            draw::print2("REDEFINIR GAMEPAD", 12, 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(getPadBtnName(config::getPadBtn(i)), 23, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE);
                pos += selected?3:2;
            }

            const bool selected = (selected_option==MENU_GAMEPAD_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_GAMEPAD_NONE;
        }
    }
}
