#include <catch2/catch_test_macros.hpp>
#include "components.hpp"
#include "dinkyecs.hpp"

using namespace components;
using namespace DinkyECS;

TEST_CASE("all components can work in the world", "[components]") {
  World world;
  auto ent1 = world.entity();

  world.set<Player>(ent1, {ent1});
  world.set<Position>(ent1, {{10,1}});
  world.set<Motion>(ent1, {1,0});
  world.set<Loot>(ent1, {100});
  world.set<Inventory>(ent1, {0});
  world.set<Tile>(ent1, {"Z"});
  world.set<EnemyConfig>(ent1, {4});

  auto player = world.get<Player>(ent1);
  REQUIRE(player.entity == ent1);

  auto position = world.get<Position>(ent1);
  REQUIRE(position.location.x == 10);
  REQUIRE(position.location.y == 1);

  auto motion = world.get<Motion>(ent1);
  REQUIRE(motion.dx == 1);
  REQUIRE(motion.dy == 0);

  auto loot = world.get<Loot>(ent1);
  REQUIRE(loot.amount == 100);

  auto inv = world.get<Inventory>(ent1);
  REQUIRE(inv.gold == 0);

  auto tile = world.get<Tile>(ent1);
  REQUIRE(tile.chr == "Z");
}

TEST_CASE("all components can be facts", "[components]") {
  World world;
  auto ent1 = world.entity();

  world.set_the<Player>({ent1});
  world.set_the<Position>({{10,1}});
  world.set_the<Motion>({1,0});
  world.set_the<Loot>({100});
  world.set_the<Inventory>({0});
  world.set_the<Tile>({"Z"});
  world.set_the<EnemyConfig>({4});

  auto player = world.get_the<Player>();
  REQUIRE(player.entity == ent1);

  auto position = world.get_the<Position>();
  REQUIRE(position.location.x == 10);
  REQUIRE(position.location.y == 1);

  auto motion = world.get_the<Motion>();
  REQUIRE(motion.dx == 1);
  REQUIRE(motion.dy == 0);

  auto loot = world.get_the<Loot>();
  REQUIRE(loot.amount == 100);

  auto inv = world.get_the<Inventory>();
  REQUIRE(inv.gold == 0);

  auto tile = world.get_the<Tile>();
  REQUIRE(tile.chr == "Z");
}

TEST_CASE("confirm combat works", "[components]") {
  World world;
  auto player = world.entity();
  auto enemy = world.entity();

  world.set<Combat>(player, {100, 10});
  world.set<Combat>(enemy, {20, 10});

  auto p_fight = world.get<Combat>(player);
  REQUIRE(p_fight.hp == 100);
  REQUIRE(p_fight.damage == 10);
  REQUIRE(p_fight.dead == false);

  auto e_fight = world.get<Combat>(enemy);
  REQUIRE(e_fight.hp == 20);
  REQUIRE(e_fight.damage == 10);
  REQUIRE(e_fight.dead == false);

  for(int i = 0; e_fight.hp > 0 && i < 100; i++) {
    p_fight.attack(e_fight);
  }
}

TEST_CASE("MapConfig loads from JSON", "[components]") {

}