#include "m_intro.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 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("intro1.gif"));
            draw::draw(96, 28, 128, 96, 0, 0);
            draw::print2("AH!", 19, 17, WHITE, FONT_ZOOM_NONE);
            draw::print2("QUE BON DIA FA!", 13, 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 == 9) return false;

                switch (stage)
                {
                    case 1:
                        draw::cls(2);
                        draw::swapcol(1, 9);
                        draw::setSource(draw::getSurface("intro2.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();
                        break;
                    case 2:
                        draw::cls(2);
                        draw::swapcol(1, 8);
                        draw::setSource(draw::getSurface("intro3.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("PERO QUE...??", 14, 17, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 3:
                        draw::cls(2);
                        draw::swapcol(1, 8);
                        draw::setSource(draw::getSurface("intro3.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("ESTOS PAIASOS ENCARA", 10, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("NO L'HAN ACABAT!", 12, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 4:
                        draw::cls(2);
                        draw::swapcol(1, 7);
                        draw::setSource(draw::getSurface("intro4.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("TINDRE QUE TROBAR JO LES", 8, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("PECES DE LA DEPURADORA", 9, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 5:
                        draw::cls(2);
                        draw::swapcol(1, 9);
                        draw::setSource(draw::getSurface("intro1.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("I ARA ON ESTAN LES ", 11, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("MEUES COSES...?", 12, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 6:
                        draw::cls(2);
                        draw::swapcol(1, 10);
                        draw::setSource(draw::getSurface("intro5.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("ELS GATS ME LES HAN", 11, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("REPARTIT PER TOTA LA CASA", 8, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 7:
                        draw::cls(2);
                        draw::swapcol(1, 10);
                        draw::setSource(draw::getSurface("intro5.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("ESPERE TROBARME ALGUN", 9, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("CAFE PEL CAMI...", 12, 19, WHITE, FONT_ZOOM_NONE);
                        //draw::render();
                        break;
                    case 8:
                        actor::templates::load();
                        draw::cls(2);

                        draw::print2("HABILITATS:", 4, 3, YELLOW, FONT_ZOOM_VERTICAL);

                        draw::print2("PARTS DE LA", 24, 3, YELLOW, FONT_ZOOM_VERTICAL);
                        draw::print2("DEPURADORA:", 24, 6, YELLOW, FONT_ZOOM_VERTICAL);

                        draw::print2("CAFE:", 6, 24, YELLOW, FONT_ZOOM_VERTICAL);

                        draw::print2("SABATES", 7, 7, WHITE, FONT_ZOOM_NONE);
                        draw::print2("GUANTS", 7, 11, WHITE, FONT_ZOOM_NONE);
                        draw::print2("PANTALONS", 7, 15, WHITE, FONT_ZOOM_NONE);
                        draw::print2("MOTXILA", 7, 19, WHITE, FONT_ZOOM_NONE);

                        draw::print("PER A PODER BOTAR", 57, 51+16, TEAL, PAPER);
                        draw::print("PER A ESPENTAR OBJECTES", 57, 83+16, TEAL, PAPER);
                        draw::print("PER A AGAFAR UN OBJECTE", 57, 115+16, TEAL, PAPER);
                        draw::print("PER A ARREPLEGAR LES PARTS", 57, 147+16, TEAL, PAPER);

                        draw::print("BOT LLARG, SUPERVELOCITAT,", 47, 211, TEAL, PAPER);
                        draw::print("IMMUNITAT, VIDA EXTRA", 47, 218, TEAL, PAPER);

                        drawActorAt("S-SHOES", 18, 38+16);
                        drawActorAt("S-GLOVES", 20, 71+16);
                        drawActorAt("S-PANTS", 20, 101+16);
                        drawActorAt("S-BAG", 20, 131+16);

                        drawActorAt("P-FILTER", 200, 80);
                        drawActorAt("P-PIPE", 250, 80);
                        drawActorAt("P-ELBOW", 200, 120);
                        drawActorAt("P-SALT", 250, 120);
                        drawActorAt("P-TIMER", 200, 160);
                        drawActorAt("P-PUMP", 250, 160);

                        drawActorAt("BOOSTER", 20, 190);

                        /*draw::swapcol(1, 10);
                        draw::setSource(draw::getSurface("intro5.gif"));
                        draw::draw(96, 28, 128, 96, 0, 0);
                        draw::print2("ESPERE TROBARME ALGUN", 9, 17, WHITE, FONT_ZOOM_NONE);
                        draw::print2("CAFE PEL CAMI...", 12, 19, WHITE, FONT_ZOOM_NONE);*/
                        break;
                }
            }
            draw::render();
            return true;
        }
    }
}