#include "m_prologo_intro.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 prologo_intro
    {
        int stage = 0;
        uint32_t time = 0;

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

            draw::swapcol(1, 9);
            draw::setSource(draw::getSurface("prologo1.gif"));
            draw::draw(96, 28, 128, 96, 0, 0);
            draw::print2("ME ANE A ESCABUSSARME", 10, 17, WHITE, FONT_ZOOM_NONE);
            draw::print2("EN LA NOVA PISCINA!", 11, 19, WHITE, FONT_ZOOM_NONE);
            draw::render();

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

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

        void drawActorAt(const char* name, const int x, const int y)
        {
            draw::swapcol(1,PURPLE);
            actor::actor_t *act = actor::createFromTemplate(name); act->flags &= ~FLAG_SPECIAL;
            actor::drawAt(act, x, y); actor::remove(act);
        }

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

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

                switch (stage)
                {
                    case 1:
                        draw::cls(2);
                        draw::swapcol(1, 9);
                        draw::setSource(draw::getSurface("prologo2.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("MIRA, ESTE SA DEJAT", 10, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("TOT TIRADO PER AI...", 10, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 2:
                        draw::cls(2);
                        draw::swapcol(1, 9);
                        draw::setSource(draw::getSurface("prologo2.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("BACH A DEJARO TOT", 12, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("EN SU PUESTO!", 14, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 3:
                        draw::cls(2);
                        draw::swapcol(1, 9);
                        draw::setSource(draw::getSurface("prologo2.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("BORAS QUE FELIS", 12, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("SE PONE!", 15, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                }
            }
            draw::render();
            return true;
        }
    }
}