#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"};
        char time_text[7] = " 00:00";

        void init(bool go_direct_to_credits)
        {
            stage = go_direct_to_credits ? 2 : 0;
            time = SDL_GetTicks();
            if (go_direct_to_credits) time += 5000;

            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();

            int milliseconds = SDL_GetTicks()-actor::stats::getStartTime();
            int seconds = milliseconds/1000;
            int minutes = seconds / 60;
            seconds = seconds % 60;

            time_text[0] = minutes<100 ? ' ' : (minutes/100)+48;
            time_text[1] = minutes<10 ? ' ' : ((minutes%100)/10)+48;
            time_text[2] = (minutes%10)+48;

            time_text[4] = (seconds/10)+48;
            time_text[5] = (seconds%10)+48;
            time_text[6] = 0;
        }

        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));
        }

        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, 9, PURPLE, FONT_ZOOM_VERTICAL);

                    draw::print2(actor::stats::getRoomsVisited(), 2, 8, 14, TEAL, FONT_ZOOM_NONE);
                    draw::print2("HABITACIONS VISITADES", 11, 14, GREEN, FONT_ZOOM_NONE);

                    draw::print2(actor::stats::getLivesLost(), 2, 11, 16, TEAL, FONT_ZOOM_NONE);
                    draw::print2("VIDES PERDUDES", 14, 16, GREEN, FONT_ZOOM_NONE);

                    draw::print2(time_text, 11, 18, TEAL, FONT_ZOOM_NONE);
                    draw::print2("TEMPS TOTAL", 18, 18, GREEN, FONT_ZOOM_NONE);

                    break;

            }

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