#include "game/level.hpp"
#include "game/components.hpp"
#include "game/worldbuilder.hpp"
#include "constants.hpp"
#include "game/systems.hpp"
#include "graphics/textures.hpp"
#include <list>

using lighting::LightRender;
using std::shared_ptr, std::make_shared;
using namespace components;

struct LevelScaling {
  int map_width=INITIAL_MAP_W;
  int map_height=INITIAL_MAP_H;
};

namespace GameDB {
  using std::shared_ptr, std::string, std::make_shared;

  struct LevelDB {
    std::list<GameDB::Level> levels;
    Level* current_level = nullptr;
    int level_count = 0;
  };

  shared_ptr<LevelDB> LDB = nullptr;
  bool initialized = false;

  LevelScaling scale_level() {
    return {
      INITIAL_MAP_W + int(LDB->level_count * 2),
      INITIAL_MAP_H + int(LDB->level_count * 2)
    };
  }

  void init() {
    components::init();
    textures::init();

    if(!initialized) {
      reset();
    }
  }

  void reset() {
    LDB = make_shared<LevelDB>();
    initialized = true;
    new_level(NULL);
  }

  shared_ptr<DinkyECS::World> clone_load_world(shared_ptr<DinkyECS::World> prev_world) {
    auto world = make_shared<DinkyECS::World>();

    if(prev_world == nullptr) {
      GameDB::load_configs(*world);
    } else {
      prev_world->clone_into(*world);
    }

    return world;
  }

  void register_level(Level level) {
    // size BEFORE push to get the correct index
    level.index = LDB->levels.size();

    LDB->levels.push_back(level);

    dbc::check(level.index == LDB->levels.size() - 1, "Level index is not the same as LDB->levels.size() - 1, off by one error");

    LDB->level_count = level.index;
    LDB->current_level = &LDB->levels.back();
  }

  void new_level(std::shared_ptr<DinkyECS::World> prev_world) {
    dbc::check(initialized, "Forgot to call GameDB::init()");
    auto world = clone_load_world(prev_world);

    auto scaling = scale_level();

    auto map = make_shared<Map>(scaling.map_width, scaling.map_height);
    auto collision = std::make_shared<SpatialMap>();

    WorldBuilder builder(*map, *collision);
    builder.generate(*world);

    auto lights = make_shared<LightRender>(map->tiles());

    auto player = world->get_the<Player>();

    register_level({
      .player=player.entity,
      .map=map,
      .world=world,
      .lights=lights,
      .collision=collision});
  }

  shared_ptr<DinkyECS::World> current_world() {
    dbc::check(initialized, "Forgot to call GameDB::init()");
    return current_level().world;
  }

  Level& create_level() {
    dbc::check(initialized, "Forgot to call GameDB::init()");

    new_level(current_world());

    return LDB->levels.back();
  }

  Level &current_level() {
    dbc::check(initialized, "Forgot to call GameDB::init()");
    return *LDB->current_level;
  }

  components::Position& player_position() {
    dbc::check(initialized, "Forgot to call GameDB::init()");
    auto& level = current_level();
    return level.world->get<components::Position>(level.player);
  }

  DinkyECS::Entity the_player() {
    dbc::check(initialized, "Forgot to call GameDB::init()");
    return current_level().player;
  }

  void load_configs(DinkyECS::World &world) {
    world.set_the<GameConfig>({
        settings::get("config"),
        settings::get("enemies"),
        settings::get("items"),
        settings::get("tiles"),
        settings::get("devices"),
        settings::get("scenes"),
    });
  }
}
