#include "gamestates.h"
#include "jgame.h"
#include "aux_font.h"

namespace gamestate
{
    namespace menu
    {
        namespace eixir
        {
            const int no = 0;
            const int sequence = 1;
            const int password = 2;
        }

        draw::surface *fondo = nullptr;
        draw::surface *cursor = nullptr;
        int exit = 0;

        bool loop();

        void init()
        {
            menu::exit = menu::eixir::no;
            menu::fondo = draw::loadSurface("menuprin.gif", true);
            menu::cursor = draw::loadSurface("cursor.gif");

            if (audio::getMusicState() != audio::music_state::MUSIC_PLAYING || audio::whichMusic() != "mus3.ogg") {
                audio::playMusic(audio::loadMusic("mus3.ogg"));
            }

            font::selectFont(font::type::colored);
            font::setColor(font::color::white);

            draw::fadein();

            game::setState(&gamestate::menu::loop);
        }

        bool loop()
        {
            if (exit) {
                if (!draw::isfading()) {
                    draw::freeSurface(fondo);
                    draw::freeSurface(cursor);
                    if (exit==menu::eixir::sequence) gamestate::sequence::init();
                    if (exit==menu::eixir::password) gamestate::password::init();
                }
                draw::render();
                return true;
            }
            const int x = input::mouseX();
            const int y = input::mouseY();

            draw::draw(fondo);

#ifdef VERSION
            font::print(277,192, VERSION);
#endif

            draw::setSource(cursor);
            draw::draw(x, y);

            draw::render();

            if (input::mouseClk(1))
            {
                if (x >= 200 && y >= 100 && x <= 270 && y <= 120) {
                    draw::fadeout();
                    game::setConfig("fase", 0);
                    exit = menu::eixir::sequence;
                    return true;
                } else if (x >= 175 && y >= 125 && x <= 290 && y <= 145) {
                    draw::fadeout();
                    game::setConfig("fase", 0);
                    exit = menu::eixir::password;
                    return true;
                } else if (x >= 200 && y >= 150 && x <= 265 && y <= 170) {
                    return false;
                }
            }

            return true;
        }

    }
}