#include "m_menu.h"
#include "jgame.h"
#include "jinput.h"
#include "controller.h"
#include "jdraw.h"
#include "config.h"
#include <SDL3/SDL.h>
#include "jaudio.h"
#include "actor.h"
#include "versio.h"

namespace modules
{
    namespace menu
    {
        draw::surface *surf;
        int anim_pos=0;
        int retras=4;

        int selected_option = OPTION_JUGAR;
        bool prologo_desbloquejat = false;
        int min_option = 1;

        void init()
        {
            if (audio::getCurrentMusic() != "mus_menu.ogg") audio::playMusic("mus_menu.ogg");
            selected_option = OPTION_JUGAR;
            ::game::setUpdateTicks(64);
            //draw::loadPalette("test.gif");
            surf = draw::getSurface("test.gif");
            draw::restorecol(2);
            if (config::isProgoloDesbloquejat()) min_option=0;
        }

        int loop()
        {
            int max_option = config::getKioskMode() ? 4 : 5;

            if (controller::pressed(KEY_MENU) && !config::getKioskMode()) {
                return OPTION_EIXIR;
            }
            if (controller::pressed(KEY_DOWN) || input::keyPressed(SDL_SCANCODE_DOWN))
            {
                audio::playSound("snd_push.wav", SOUND_BASIC);
                selected_option++; if (selected_option==max_option+1) selected_option=min_option;
            }

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

            if (controller::pressed(KEY_JUMP) || controller::pressed(KEY_PICK) || input::keyPressed(SDL_SCANCODE_RETURN) )
            {
                audio::playSound("snd_push.wav", SOUND_BASIC);
                return selected_option;
            }

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

            draw::swapcol(1, WHITE);
            draw::setSource(surf);
            draw::draw(150,56,20,32,64+anim_pos*20,32);
            retras--;
            if (retras==0) {retras=4; anim_pos=(anim_pos+1)&1; }

            draw::print2("THE POOL", 16, 3, TEAL, FONT_ZOOM_VERTICAL);

            switch (selected_option)
            {
                case OPTION_PROLOGO:
                    draw::print2("fg   JUGAR AL PROLOGO", 11, 12, YELLOW, FONT_ZOOM_VERTICAL);
                    draw::print2("de   JUGAR AL JOC", 11, 15, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR TECLES", 11, 17, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR GAMEPAD", 11, 19, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE);
                    if (!config::getKioskMode()) draw::print2("de   EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE);
                    break;
                case OPTION_JUGAR:
                    if (config::isProgoloDesbloquejat()) draw::print2("de   JUGAR AL PROLOGO", 11, 12, GREEN, FONT_ZOOM_NONE);
                    draw::print2("fg   JUGAR AL JOC", 11, 14, YELLOW, FONT_ZOOM_VERTICAL);
                    draw::print2("de   REDEFINIR TECLES", 11, 17, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR GAMEPAD", 11, 19, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE);
                    if (!config::getKioskMode()) draw::print2("de   EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE);
                    break;
                case OPTION_TECLES:
                    if (config::isProgoloDesbloquejat()) draw::print2("de   JUGAR AL PROLOGO", 11, 12, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE);
                    draw::print2("fg   REDEFINIR TECLES", 11, 16, YELLOW, FONT_ZOOM_VERTICAL);
                    draw::print2("de   REDEFINIR GAMEPAD", 11, 19, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE);
                    if (!config::getKioskMode()) draw::print2("de   EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE);
                    break;
                case OPTION_GAMEPAD:
                    if (config::isProgoloDesbloquejat()) draw::print2("de   JUGAR AL PROLOGO", 11, 12, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR TECLES", 11, 16, GREEN, FONT_ZOOM_NONE);
                    draw::print2("fg   REDEFINIR GAMEPAD", 11, 18, YELLOW, FONT_ZOOM_VERTICAL);
                    draw::print2("de   CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE);
                    if (!config::getKioskMode()) draw::print2("de   EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE);
                    break;
                case OPTION_AUDIO:
                    if (config::isProgoloDesbloquejat()) draw::print2("de   JUGAR AL PROLOGO", 11, 12, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR TECLES", 11, 16, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR GAMEPAD", 11, 18, GREEN, FONT_ZOOM_NONE);
                    draw::print2("fg   CONFIGURAR AUDIO", 11, 20, YELLOW, FONT_ZOOM_VERTICAL);
                    if (!config::getKioskMode()) draw::print2("de   EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE);
                    break;
                case OPTION_EIXIR:
                    if (config::isProgoloDesbloquejat()) draw::print2("de   JUGAR AL PROLOGO", 11, 12, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR TECLES", 11, 16, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   REDEFINIR GAMEPAD", 11, 18, GREEN, FONT_ZOOM_NONE);
                    draw::print2("de   CONFIGURAR AUDIO", 11, 20, GREEN, FONT_ZOOM_NONE);
                    draw::print2("fg   EIXIR", 11, 22, YELLOW, FONT_ZOOM_VERTICAL);
                    break;

            };

            //draw::print2(input::getPadBtnPressed(), 3, 0, 24, RED, FONT_ZOOM_NONE);

            draw::print2("(C) JAILDOCTOR 2024", 11, 28, TEAL, FONT_ZOOM_NONE);
            draw::print(VERSIO, 299, 233, WHITE, PAPER);

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