#include "gamestates.h"
#include "jgame.h"
#include <string>

#define POSTFASE_INITIAL    0
#define POSTFASE_VICTORIA   1
#define POSTFASE_PASSWORD   2

namespace gamestate
{
    namespace postfase
    {
        // Variables del gamestate
        static int sub_state = POSTFASE_INITIAL;

        void init_victoria();
        void init_password();
        bool loop();
        void drawText(const int x, const int y, const uint8_t color, std::string text);
        char *ObtenerPasswordDeFase();


        void init()
        {
            sub_state = POSTFASE_INITIAL;

            if (game::getConfig("fase") == 30) {
                gamestate::sequence::init();
                return;
            } else {
                audio::loadMusic("mus3.mp3");
                audio::playMusic();
            }

            if (game::getConfig("fase") % 5 == 0) {
                gamestate::postfase::init_victoria();
            } else {
                gamestate::postfase::init_password();
            }
        }

        void init_victoria()
        {
            sub_state = POSTFASE_VICTORIA;
            draw::surface *fondo = nullptr;

            switch (game::getConfig("fase")) {
                case 5:
                    fondo = draw::loadSurface("final01.GIF", true);
                    break;
                case 10:
                    fondo = draw::loadSurface("final02.GIF", true);
                    break;
                case 15:
                    fondo = draw::loadSurface("final03.GIF", true);
                    break;
                case 20:
                    fondo = draw::loadSurface("final04.GIF", true);
                    break;
                case 25:
                    fondo = draw::loadSurface("final05.GIF", true);
                    break;
            }
            draw::setSource(fondo);
            draw::draw(0,0,320,200,0,0);
            draw::freeSurface(fondo);

            draw::fadein();
            game::setState(gamestate::postfase::loop);
        }

        void init_password()
        {
            sub_state = POSTFASE_PASSWORD;
            draw::surface *fondo = draw::loadSurface("postfase.gif", true);

            char *password = ObtenerPasswordDeFase();

            draw::setSource(fondo);
            draw::draw(0,0,320,200,0,0);
            draw::freeSurface(fondo);

            drawText(175, 166, 1, password);
            free(password);

            draw::fadein();
            game::setState(gamestate::postfase::loop);
        }

        bool loop()
        {
            static bool salir = false;

            draw::render();

            if (draw::isfading()) return true;

            if (salir)
            {
                salir = false;
                if (sub_state == POSTFASE_VICTORIA) {
                    gamestate::postfase::init_password();
                } else {
                    gamestate::sequence::init();
                }
                return true;
            }

            if (input::anyKeyPressed() || input::mouseBtn(1))
            {
                draw::fadeout();
                salir = true;
                return true;
            }
            return true;
        }

        void drawText(const int x, const int y, const uint8_t color, std::string text)
        {
            draw::surface *pic = draw::loadSurface("fuente2.gif");
            draw::setSource(pic);
            draw::setTrans(0);
            switch (color) {
                case 0: draw::setPaletteEntry(63, 255, 255, 255); break;
                case 1: draw::setPaletteEntry(63, 255, 0, 0); break;
                case 2: draw::setPaletteEntry(63, 0, 255, 0); break;
                case 3: draw::setPaletteEntry(63, 0, 0, 255); break;
            }
            const int len = text.length();
            for (int i=0;i<len;++i)
            {
                char chr = text[i];
                draw::draw(x+i*7, y, 6, 6, (int(chr)-32)*7, 0);
            }
            draw::setTrans(255);
            draw::freeSurface(pic);
            draw::render();
        }

        char *ObtenerPasswordDeFase()
        {
            int filesize = 0;
            const char *buffer = file::getFileBuffer("offsets.bal", &filesize);

            int punter = (game::getConfig("fase")-1)*11;

            char *passFile = (char*)malloc(11);

            for (int i=0;i<10;i++) {
                punter++;
                passFile[i] = uint8_t(buffer[punter]) - (101+i);
            }

            return passFile;
        }

    }
}