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

namespace gamestate
{
    namespace mort
    {
        namespace eixir
        {
            const int no = 0;
            const int prefase = 1;
            const int menu = 2;
        }

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

        bool loop();

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

            audio::playMusic(audio::loadMusic("mus5.ogg"));

            draw::fadein();

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

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

            draw::draw(fondo);

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

            draw::render();

            if (input::mouseClk(1))
            {
                if (x >= 100 && y >= 50 && x <= 210 && y <= 70) {
                    draw::fadeout();
                    exit = mort::eixir::prefase;
                    return true;
                } else if (x >= 120 && y >= 72 && x <= 190 && y <= 95) {
                    draw::fadeout();
                    exit = mort::eixir::menu;
                    return true;
                } else if (x >= 120 && y >= 95 && x <= 190 && y <= 115) {
                    return false;
                }
            }

            return true;
        }

    }
}