#include "samples/ghhb_game.h"
#include "samples/instrument_select.h"
#include "samples/song_select.h"
#include "samples/title_screen.h"
#include "entities/song.h"
#include "entities/song_catalog.h"

// Emscripten is used for web builds.
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif

int INSTRUMENT_GAMEPAD_INDEX[MAX_INSTRUMENT_TYPES] = {-1, -1, -1, -1};
int INSTRUMENT_PHYSICAL_GAMEPAD[MAX_INSTRUMENT_TYPES] = {-1, -1, -1, -1};
std::string SELECTED_SONG_PATH = get_default_song_path();
int SELECTED_TRACK_OVERRIDE = get_default_track_override();

Game game;

void update()
{
    float delta_time = GetFrameTime();
    game.update(delta_time);
}

// TODO: Make this a GUI app for each platform.
int main(int argc, char** argv)
{
    game.add_manager<WindowManager>(1280, 720, "Guitar Hero But Better");
    auto font_manager = game.add_manager<FontManager>();
    auto json_manager = game.add_manager<JsonManager>();
    auto song_manager = game.add_manager<SongManager>(json_manager);
    game.init();

    // Game::init initializes all managers, so we can load fonts now.
    font_manager->load_font("Roboto", "assets/fonts/Roboto.ttf", 64);
    font_manager->load_font("Tiny5", "assets/fonts/Tiny5.ttf", 64);
    font_manager->set_texture_filter("Roboto", TEXTURE_FILTER_BILINEAR);

    game.add_scene<TitleScreen>("title");
    game.add_scene<SongSelectScreen>("song_select");
    game.add_scene<InstrumentSelectScreen>("instrument_select");
    game.add_scene<GHHBScene>("ghhb");

    auto catalog = get_song_catalog();
    for (const auto& entry : catalog)
    {
        song_manager->load_song(entry.first, entry.first);
    }


#ifdef __EMSCRIPTEN__
    emscripten_set_main_loop(update, 0, true);
#else
    while (!WindowShouldClose())
    {
        update();
    }
#endif
    return 0;
}
