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

#define DIAPO_ESPERAR		0
#define DIAPO_FADEIN		1
#define DIAPO_SHOW		    2
#define DIAPO_PRINT		    3
#define DIAPO_MUSICA		4
#define DIAPO_FADEOUT		5
#define DIAPO_FADEMUSIC	    6

namespace gamestate
{
    namespace sequence
    {
        FILE *sequence_file = nullptr;
        uint32_t wait_until = 0;

        void drawPic(std::string filename);
        void drawText(const int x, const int y, const uint8_t color, std::string text);
        bool loop();


        void init()
        {
            draw::setTrans(255);
            const int fase = game::getConfig("fase");
            std::string filename;

            switch (fase)
            {
                case -1: filename = "seqIN.txt"; break;
                case  0: filename = "seq00.txt"; break;
                case  5: filename = "seq05.txt"; break;
                case 10: filename = "seq10.txt"; break;
                case 15: filename = "seq15.txt"; break;
                case 20: filename = "seq20.txt"; break;
                case 25: filename = "seq25.txt"; break;
                case 30: filename = "seq30.txt"; break;
                default: gamestate::prefase::init();
            }

            int filesize;
            sequence_file = file::getFilePointer(filename, &filesize);

            game::setState(&gamestate::sequence::loop);
        }

        bool loop()
        {
            if (draw::isfading()) {
                draw::render();
                return true;
            }
            if ( (wait_until > 0) && (SDL_GetTicks() < wait_until) )
            {
                if (input::anyKeyPressed() || input::mouseBtn(1)) {
                    wait_until=0;
                    if (input::keyPressed(SDL_SCANCODE_ESCAPE)) fseek(sequence_file, 0, SEEK_END);
                } else {
                    draw::render();
                    return true;
                }
            }

            if (feof(sequence_file))
            {
                fclose(sequence_file);
                const int fase = game::getConfig("fase");
                if ( fase == -1 || fase == 30) {
                    gamestate::menu::init();
                } else {
                    gamestate::prefase::init();
                }

                return true;

            } else {

                char text[100];
                int x, y, val;
                int res = fscanf(sequence_file, "%s", text);
                if (res==1)
                {
                    std::string command(text);

                    if (command=="ESPERAR") {
                        //int res = 
                        fscanf(sequence_file, "%i", &val);
                        printf("ESPERAR %i\n", val);
                        wait_until = SDL_GetTicks() + val;

                    } else if (command=="FADEIN") {
                        //int res =
                        fscanf(sequence_file, " '%[^']'", text);
                        drawPic(text);
                        draw::fadein();

                    } else if (command=="SHOW") {
                        //int res =
                        fscanf(sequence_file, " '%[^']'", text);
                        drawPic(text);
                        draw::render();
                        
                    } else if (command=="PRINT") {
                        //int res =
                        fscanf(sequence_file, " %i %i %i '%[^']'", &x, &y, &val, text);
                        drawText(x, y, val, text);

                    } else if (command=="PLAYMUSIC") {
                        //int res =
                        fscanf(sequence_file, " '%[^']'", text);
                        audio::loadMusic(text);
                        audio::playMusic();
                        
                    } else if (command=="FADEOUT") {
                        draw::fadeout();

                    } else if (command=="FADEOUTMUSIC") {
                        draw::fadeout();
                        audio::fadeoutMusic();
                    }
                }
            }
            return true;
        }

        void drawPic(std::string filename)
        {
            draw::surface *pic = draw::loadSurface(filename, true);
            draw::setSource(pic);
            draw::draw(0, 0, 320, 200, 0, 0);
            draw::freeSurface(pic);
        }

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

    }
}
