#include <ctime>
#include <string>

#include "core/jail/jail_audio.hpp"
#include "core/jail/jdraw8.hpp"
#include "core/jail/jfile.hpp"
#include "core/jail/jgame.hpp"
#include "core/rendering/menu.hpp"
#include "core/rendering/overlay.hpp"
#include "core/rendering/screen.hpp"
#include "core/system/director.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();

    // Carrega presets de shaders
    Options::setPostFXFile(std::string(file_getconfigfolder()) + "postfx.yaml");
    Options::loadPostFXFromFile();
    Options::setCrtPiFile(std::string(file_getconfigfolder()) + "crtpi.yaml");
    Options::loadCrtPiFromFile();

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

    // Arranca el Director: crea game thread, bucle principal, sincronització de frames
    Director::get()->run();

    Options::saveToFile();

    Director::destroy();
    Menu::destroy();
    Overlay::destroy();
    JA_Quit();
    JD8_Quit();
    Screen::destroy();
    JG_Finalize();

    return 0;
}
