#include "gui.hpp"
#include "dinkyecs.hpp"
#include "systems.hpp"
#include "events.hpp"
#include "components.hpp"
#include "constants.hpp"
#include "dbc.hpp"
#include "spatialmap.hpp"
#include "render.hpp"
#include "save.hpp"
#include "lights.hpp"
#include "worldbuilder.hpp"
#include "ftxui/screen/terminal.hpp"  // for SetColorSupport, Color, TrueColor
#include <filesystem>
#include <fcntl.h>
#include <fmt/core.h>

using namespace ftxui;
using namespace components;
using lighting::LightSource;
namespace fs = std::filesystem;

int main(int argc, char *argv[]) {
  DinkyECS::World world;
  Map game_map(GAME_MAP_X, GAME_MAP_Y);
  save::load_configs(world);

  if(argc == 2) {
    fmt::println("Loading save file {}", argv[1]);
    fs::path save_path{argv[1]};
    save::from_file(save_path, world, game_map);
  } else {
    WorldBuilder builder(game_map);
    builder.generate(world);
  }

  SpatialMap collider;
  world.set_the<SpatialMap>(collider);
  System::init_positions(world);

  GUI gui(world, game_map);

  return gui.main();
}
