#include <ctime>
#include <string>

#include "core/global_inputs.hpp"
#include "core/jail_audio.hpp"
#include "core/jdraw8.hpp"
#include "core/jfile.hpp"
#include "core/jgame.hpp"
#include "core/screen.hpp"
#include "game/defines.hpp"
#include "game/info.hpp"
#include "game/modulegame.hpp"
#include "game/modulesequence.hpp"
#include "game/options.hpp"

int main(int argc, char* args[]) {
    srand(unsigned(time(NULL)));

    // Crea la carpeta de configuració i carrega les opcions
    file_setconfigfolder("jailgames/aee");
    Options::setConfigFile(std::string(file_getconfigfolder()) + "config.yaml");
    Options::loadFromFile();

    JG_Init();
    Screen::init();
    JD8_Init();
    JA_Init(48000, SDL_AUDIO_S16, 2);

    info::num_habitacio = Options::game.habitacio_inicial;
    info::num_piramide = Options::game.piramide_inicial;
    info::diners = 0;
    info::diamants = 0;
    info::vida = Options::game.vides;
    info::momies = 0;
    info::nou_personatge = false;
    info::pepe_activat = false;

    FILE* ini = fopen("trick.ini", "rb");
    if (ini != NULL) {
        info::nou_personatge = true;
        fclose(ini);
    }

    int gameState = 1;

    while (gameState != -1) {
        switch (gameState) {
            case 0:
                ModuleGame* moduleGame;
                moduleGame = new ModuleGame();
                gameState = moduleGame->Go();
                delete moduleGame;
                break;
            case 1:
                ModuleSequence* moduleSequence;
                moduleSequence = new ModuleSequence();
                gameState = moduleSequence->Go();
                delete moduleSequence;
                break;
        }
    }

    Options::saveToFile();

    JA_Quit();
    JD8_Quit();
    Screen::destroy();
    JG_Finalize();

    return 0;
}
