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

namespace modules
{
    namespace end_sequence
    {
        int stage = 0;
        uint32_t time = 0;
        actor::actor_t *act = nullptr;
        const char *actor_names[] = {"JAILDESIGNER", "BATMAN", "ROBIN", "EL ALTRE BATMAN", "EL ABAD", "LA ROOMBA", "EL OBRER", "BAMBOLLA DE CAFE", "EL YONKI", "LA PILOTA", "SAM", "LORD ABAD"};
        const char *actor_ids[] = {"JAILDES", "GAT-BATMAN", "GAT-ROBIN", "BATMAN", "ABAD", "ROOMBA2", "OBRER", "COFFEE", "YONKI", "PILOTA", "SAM", "LORD-ABAD"};

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

            draw::swapcol(1, 7);
            draw::setSource(draw::getSurface("end1.gif"));
            draw::draw(96, 28, 128, 96, 0, 0);
            draw::print2("PER FI! JA PUC", 13, 17, WHITE, FONT_ZOOM_NONE);
            draw::print2("TASTAR LA PISCINA!", 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()
        {
            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));
        }

        void drawActorAt(const char* name, const int x, const int y)
        {
            draw::swapcol(1,YELLOW);
            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==2) && (config::isProgoloDesbloquejat()) ) stage++;
                if (stage == 17) return false;

                switch (stage)
                {
                    case 1:
                        draw::cls(2);
                        draw::swapcol(1, 8);
                        draw::setSource(draw::getSurface("end2.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("PUTA, QUE GUST...", 12, 17, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 2:
                        draw::cls(2);
                        draw::swapcol(1, 8);
                        draw::setSource(draw::getSurface("end2.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("LLASTIMA QUE ENCARA", 11, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("NO HE TROBAT TOTES", 11, 19, WHITE, FONT_ZOOM_NONE);
                        draw::print2("LES ANBERNICS...", 13, 21, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 3: case 4: case 5: case 6: case 7: case 8:
                    case 9: case 10: case 11: case 12: case 13: case 14:
                        ::game::setUpdateTicks(64);
                        if (act) actor::remove(act);
                        act = actor::createFromTemplate(actor_ids[stage-3]);
                        act->flags &= ~FLAG_MOVING;
                        break;
                    case 15:
                        if (act) actor::remove(act);
                        act = actor::createFromTemplate("GAT-BATMAN");
                        act->flags &= ~FLAG_MOVING;
                        break;
                    case 16:
                        if (act) actor::remove(act);
                    break;
                }
            }

            switch (stage)
            {
                case 3: case 4: case 5: case 6: case 7: case 8:
                case 9: case 10: case 11: case 12: case 13: case 14:
                {
                    draw::cls(2);
                    draw::print2("THE POOL", 16, 3, TEAL, FONT_ZOOM_VERTICAL);
                    draw::swapcol(1,YELLOW);
                    actor::update(act, false);
                    actor::drawAt(act, 148, 100);
                    const int x = 160-strlen(actor_names[stage-3])*2;
                    draw::print(actor_names[stage-3], x, 145, WHITE, PAPER);
                    draw::print2("(C) JAILDOCTOR 2024", 11, 28, TEAL, FONT_ZOOM_NONE);
                    break;
                }
                case 15:
                {
                    draw::cls(2);
                    draw::swapcol(1,GREEN);
                    actor::update(act, false);
                    actor::drawAt(act, 148, 100);
                    const int x = 160-strlen("DEDICAT A BATMAN")*2;
                    draw::print("DEDICAT A BATMAN", x, 145, WHITE, PAPER);
                    break;
                }
                case 16:
                    draw::cls(2);
                    draw::print2("GRACIES PER JUGAR!", 11, 15, PURPLE, FONT_ZOOM_NONE);
                    break;

            }

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