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

namespace modules
{
    namespace menu_audio
    {
        int selected_option = MENU_AUDIO_MUSICA;

        void init()
        {
            selected_option = MENU_AUDIO_MUSICA;
        }

        int loop()
        {
            if (controller::pressed(KEY_MENU)) {
                return MENU_AUDIO_TORNAR;
            }
            if (controller::pressed(KEY_DOWN) || input::keyPressed(SDL_SCANCODE_DOWN))
            {
                selected_option = (selected_option==2)?0:selected_option+1;
                audio::playSound("snd_push.wav", SOUND_BASIC);
            }
            if (controller::pressed(KEY_UP) || input::keyPressed(SDL_SCANCODE_UP))
            {
                selected_option = (selected_option==0)?2:selected_option-1;
                audio::playSound("snd_push.wav", SOUND_BASIC);
            }
            if (controller::pressed(KEY_JUMP) || controller::pressed(KEY_PICK) || input::keyPressed(SDL_SCANCODE_RETURN)) {
                if (selected_option==MENU_AUDIO_MUSICA) {
                    config::toggleMusic();
                    if (config::isMusicEnabled())
                        audio::playMusic("mus_menu.ogg");
                    else
                        audio::stopMusic();
                }
                if (selected_option==MENU_AUDIO_SO) config::toggleSound();
                if (selected_option==MENU_AUDIO_TORNAR) return MENU_AUDIO_TORNAR;
                audio::playSound("snd_push.wav", SOUND_BASIC);
            }

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

            draw::print2("CONFIGURAR AUDIO", 13, 3, TEAL, FONT_ZOOM_VERTICAL);

            const char *musica_msg = config::isMusicEnabled() ? "MUSICA: SI" : "MUSICA: NO";
            const int soundmode=config::getSoundMode();
            const char *so_msg = soundmode==SOUND_ALL? "SO: TOT" : soundmode==SOUND_BASIC?"SO: BASIC" : "SO: SILENCI";

            int pos = 14;

            if (selected_option==MENU_AUDIO_MUSICA) {
                draw::print2("fg", 13, pos, YELLOW, FONT_ZOOM_VERTICAL);
                draw::print2(musica_msg, 18, pos, YELLOW, FONT_ZOOM_VERTICAL);
                pos+=3;
            } else {
                draw::print2("de", 13, pos, GREEN, FONT_ZOOM_NONE);
                draw::print2(musica_msg, 18, pos, GREEN, FONT_ZOOM_NONE);
                pos+=2;
            }

            if (selected_option==MENU_AUDIO_SO) {
                draw::print2("fg", 13, pos, YELLOW, FONT_ZOOM_VERTICAL);
                draw::print2(so_msg, 18, pos, YELLOW, FONT_ZOOM_VERTICAL);
                pos+=3;
            } else {
                draw::print2("de", 13, pos, GREEN, FONT_ZOOM_NONE);
                draw::print2(so_msg, 18, pos, GREEN, FONT_ZOOM_NONE);
                pos+=2;
            }

            if (selected_option==MENU_AUDIO_TORNAR) {
                draw::print2("fg   TORNAR", 13, pos, YELLOW, FONT_ZOOM_VERTICAL);
            } else {
                draw::print2("de   TORNAR", 13, pos, GREEN, FONT_ZOOM_NONE);
            }

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

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