#include "gamestates.h"
#include "jgame.h"
#include <string>
#include "aux_font.h"
#include "proc_mapa.h"
#include <SDL2/SDL.h>

namespace gamestate
{
    namespace password
    {
        namespace eixir
        {
            const int no = 0;
            const int sequence = 1;
            const int menu = 2;
        }

        // Variables del gamestate
        draw::surface *fondo = nullptr;
        draw::surface *cursor = nullptr;

        int exit = password::eixir::no;
        char password[11] = "          ";
        uint8_t indice = 0;

        // Mètodes del gamestate
        bool loop();
        const int getFaseFromPassword();

        void init()
        {
            exit = password::eixir::no;

            // Carrega el gif del fondo
            fondo = draw::loadSurface("prefase.gif", true);
            cursor = draw::loadSurface("cursor.gif");

            // Pinta el text en el fondo
            draw::setDestination(fondo);
            font::selectFont(font::type::normal);
	        font::print(95, 80, "ESCRIU EL PASSWORD");

            draw::setDestination(nullptr);

            // Comencem el bucle
            draw::fadein();
            game::setState(gamestate::password::loop);
        }

        bool loop()
        {
            draw::draw(fondo);
            font::print(123, 140, password);

            draw::setSource(cursor);
            draw::draw(input::mouseX(), input::mouseY());

            draw::render();

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

            if (exit)
            {
                if (!draw::isfading()) {
                    draw::freeSurface(fondo);
                    draw::freeSurface(cursor);
                    if (exit==password::eixir::sequence) gamestate::sequence::init();
                    if (exit==password::eixir::menu) gamestate::menu::init();
                }
                draw::render();
                return true;
            }

            if (input::anyKeyPressed())
            {
                if (input::keyPressed(SDL_SCANCODE_ESCAPE))
                {
                    draw::fadeout();
                    game::setConfig("fase", 0);
                    exit = password::eixir::menu;
                    return true;
                }
                else if (input::keyPressed(SDL_SCANCODE_BACKSPACE))
                {
                    if (indice>0) password[--indice] = 32;
                }
                else
                {
                    const uint8_t tecla = input::getKeyPressed();
                    if (tecla == SDL_SCANCODE_0) {
                        password[indice++] = '0';
                    }
                    else if (tecla >= SDL_SCANCODE_1 && tecla <= SDL_SCANCODE_9)
                    {
                        password[indice++] = tecla + 18;
                    }
                    else if (tecla >= SDL_SCANCODE_A && tecla <= SDL_SCANCODE_Z)
                    {
                        password[indice++] = tecla + 61;
                    }

                    if (indice == 10)
                    {
                        draw::fadeout();
                        game::setConfig("fase", password::getFaseFromPassword());
                        exit = password::eixir::sequence;
                        return true;
                    }
                }

            }
            return true;
        }

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

            int punter = 0;

            bool salir = false;
            char passFile[11] = "          ";
            int numPassword = 0;

            while ( numPassword < 30 && !salir ) {
                for (int i=0;i<10;i++) {
                    punter++;
                    passFile[i] = buffer[punter] - (101+i);
                }
                punter++;

                salir = true;
                for (int i=0;i<10;i++) {
                    if (passFile[i] != password[i]) salir = false;
                }
                numPassword++;
            }

            if (!salir) numPassword = 0;

            return numPassword;
        }
    }
}
