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

namespace gamestate
{
    namespace postfase
    {
        namespace state
        {
            const int initial  = 0;
            const int victoria = 1;
            const int password = 2;
        }
        
        // Variables del gamestate
        static int sub_state = postfase::state::initial;

        void initVictoria();
        void initPassword();
        bool loop();
        std::string getPassword();


        void init()
        {
            sub_state = postfase::state::initial;

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

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

        void initVictoria()
        {
            sub_state = postfase::state::victoria;

            char filename[12] = "final00.GIF";
            filename[6]= 48 + (game::getConfig("fase") / 5);

            draw::surface *fondo = draw::loadSurface(filename, true);
            draw::draw(fondo);
            draw::freeSurface(fondo);

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

        void initPassword()
        {
            sub_state = postfase::state::password;
            std::string password = getPassword();

            draw::surface *fondo = draw::loadSurface("postfase.gif", true);
            draw::draw(fondo);
            draw::freeSurface(fondo);

            font::selectFont(font::type::colored);
            font::setColor(font::color::red);
            font::print(175, 166, 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::state::victoria) {
                    gamestate::postfase::initPassword();
                } else {
                    gamestate::sequence::init();
                }
                return true;
            }

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

        std::string getPassword()
        {
            char *buffer = file::getFileBuffer("offsets.bal");

            int punter = (game::getConfig("fase")-1)*11;
            char passFile[11];
            for (int i=0;i<10;i++) passFile[i] = uint8_t(buffer[++punter]) - (101+i);
            free(buffer);

            return passFile;
        }

    }
}