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

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

        void drawPic(std::string filename);
        bool loop();

        void init()
        {
            font::selectFont(font::type::colored);

            std::string filename;

            switch (game::getConfig("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(); return; break;
            }

            sequence_file = file::getFilePointer(filename);

            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") {
                        fscanf(sequence_file, "%i", &val);
                        wait_until = SDL_GetTicks() + val;

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

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

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

                    } else if (command=="FADEOUTMUSIC") {
                        draw::fadeout();
                        audio::fadeoutMusic();
                        
                    } else if (command=="END") {
                        fclose(sequence_file);
                        const int fase = game::getConfig("fase");
                        if ( fase == -1 || fase == 30) {
                            gamestate::menu::init();
                        } else {
                            gamestate::prefase::init();
                        }
                        return true;
                    }
                }
            }
            return true;
        }

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

    }
}
