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

namespace modules
{
    namespace end_prologo_sequence
    {
        int stage = 0;
        uint32_t time = 0;

        void init()
        {
            time = SDL_GetTicks();
            stage = 0;
            draw::restorecol(2);
            draw::cls(2);

            draw::swapcol(1, 7);
            draw::setSource(draw::getSurface("prologo3.gif"));
            draw::draw(96, 28, 128, 96, 0, 0);
            draw::print2("ALE! QUE BON", 13, 17, WHITE, FONT_ZOOM_NONE);
            draw::print2("TRABAJ HE HECH!", 11, 19, WHITE, FONT_ZOOM_NONE);
            draw::render();

            if (audio::getCurrentMusic() != "mus_menu.ogg") audio::playMusic("mus_menu.ogg");
            actor::templates::load();
        }

        const bool shouldGoToNext()
        {
            if (SDL_GetTicks()-time < 1000) return false;
            return (SDL_GetTicks()-time > (stage==15||stage==16?10000:5000)) || 
                   (controller::pressed(KEY_JUMP)) || (controller::pressed(KEY_PICK)) ||
                   (input::keyPressed(SDL_SCANCODE_SPACE)) || (input::keyPressed(SDL_SCANCODE_RETURN));
        }

        bool loop()
        {
            if (controller::pressed(KEY_MENU)) return false;

            if (shouldGoToNext())
            {
                time = SDL_GetTicks();
                stage++;
                if (stage==2) return false;

                switch (stage)
                {
                    case 1:
                        draw::cls(2);
                        draw::swapcol(1, 8);
                        draw::setSource(draw::getSurface("prologo4.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("PERO QUE...?", 12, 17, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;

                }
            }

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