#include "td/game/Towers.h"
#include "td/game/World.h"

#include <map>

namespace td {
namespace game {

bool Tower::IsMobInRange(MobPtr mob) {
	if (mob->IsDead())
		return false;
	return mob->CollidesWith(*this);
}

const std::map<std::pair<TowerType, TowerLevel>, TowerStats> TowerConstants = {
	//                                    // rate  damage range
	{{TowerType::Archer, {1, TowerPath::Base}}, {2, 5, 10}},

	{{TowerType::Archer, {2, TowerPath::Top}}, {1, 0, 12}},
	{{TowerType::Archer, {3, TowerPath::Top}}, {1, 0, 13}},
	{{TowerType::Archer, {4, TowerPath::Top}}, {0.8, 0, 15}},

	{{TowerType::Archer, {2, TowerPath::Bottom}}, {2, 10, 12}},
	{{TowerType::Archer, {3, TowerPath::Bottom}}, {2, 10, 13}},
	{{TowerType::Archer, {4, TowerPath::Bottom}}, {2, 10, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Ice, {1, TowerPath::Base}}, {1, 0, 10}},
	{{TowerType::Ice, {2, TowerPath::Base}}, {1, 0, 12}},
	{{TowerType::Ice, {3, TowerPath::Base}}, {1, 0, 13}},
	{{TowerType::Ice, {4, TowerPath::Base}}, {1, 1, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Sorcerer, {1, TowerPath::Base}}, {5, 0, 10}},
	{{TowerType::Sorcerer, {2, TowerPath::Base}}, {4, 0, 12}},

	{{TowerType::Sorcerer, {3, TowerPath::Top}}, {4, 0, 14}},
	{{TowerType::Sorcerer, {4, TowerPath::Top}}, {4, 0, 15}},

	{{TowerType::Sorcerer, {3, TowerPath::Bottom}}, {4, 0, 14}},
	{{TowerType::Sorcerer, {4, TowerPath::Bottom}}, {4, 0, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Zeus, {1, TowerPath::Base}}, {5, 0, 10}},
	{{TowerType::Zeus, {2, TowerPath::Base}}, {4, 0, 12}},

	{{TowerType::Zeus, {3, TowerPath::Top}}, {0, 0, 0}},
	{{TowerType::Zeus, {4, TowerPath::Top}}, {0, 0, 0}},

	{{TowerType::Zeus, {3, TowerPath::Bottom}}, {1.2, 0, 14}},
	{{TowerType::Zeus, {4, TowerPath::Bottom}}, {0.8, 0, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Mage, {1, TowerPath::Base}}, {5, 0, 10}},
	{{TowerType::Mage, {2, TowerPath::Base}}, {4, 0, 12}},
	{{TowerType::Mage, {3, TowerPath::Base}}, {3, 0, 13}},
	{{TowerType::Mage, {4, TowerPath::Base}}, {1, 30, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Artillery, {1, TowerPath::Base}}, {7, 0, 10}},
	{{TowerType::Artillery, {2, TowerPath::Base}}, {7, 0, 12}},

	{{TowerType::Artillery, {3, TowerPath::Top}}, {7, 0, 13}},
	{{TowerType::Artillery, {4, TowerPath::Top}}, {7, 0, 15}},

	{{TowerType::Artillery, {3, TowerPath::Bottom}}, {5, 0, 13}},
	{{TowerType::Artillery, {4, TowerPath::Bottom}}, {4, 0, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Quake, {1, TowerPath::Base}}, {5, 5, 10}},
	{{TowerType::Quake, {2, TowerPath::Base}}, {4, 7, 12}},
	{{TowerType::Quake, {3, TowerPath::Base}}, {3, 9, 13}},
	{{TowerType::Quake, {4, TowerPath::Base}}, {2, 11, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Poison, {1, TowerPath::Base}}, {5, 0, 10}},
	{{TowerType::Poison, {2, TowerPath::Base}}, {5, 0, 12}},

	{{TowerType::Poison, {3, TowerPath::Top}}, {6, 0, 13}},
	{{TowerType::Poison, {4, TowerPath::Top}}, {5, 0, 15}},

	{{TowerType::Poison, {3, TowerPath::Bottom}}, {5, 10, 13}},
	{{TowerType::Poison, {4, TowerPath::Bottom}}, {6, 20, 15}},

	//-----------------------------------------------------------------

	{{TowerType::Leach, {1, TowerPath::Base}}, {0, 0, 0}},
	{{TowerType::Leach, {2, TowerPath::Base}}, {0, 0, 0}},
	{{TowerType::Leach, {3, TowerPath::Base}}, {0, 0, 0}},

	//-----------------------------------------------------------------

	{{TowerType::Turret, {1, TowerPath::Base}}, {0.5, 0, 0}},

	{{TowerType::Turret, {2, TowerPath::Top}}, {0, 0, 0}},
	{{TowerType::Turret, {3, TowerPath::Top}}, {0, 0, 0}},

	{{TowerType::Turret, {2, TowerPath::Bottom}}, {0, 0, 0}},
	{{TowerType::Turret, {3, TowerPath::Bottom}}, {0, 0, 0}},

	//-----------------------------------------------------------------

	{{TowerType::Necromancer, {1, TowerPath::Base}}, {2, 0, 11}},
	{{TowerType::Necromancer, {2, TowerPath::Base}}, {1, 0, 14}},

	{{TowerType::Necromancer, {3, TowerPath::Top}}, {1, 0, 15}},

	{{TowerType::Necromancer, {3, TowerPath::Bottom}}, {0, 30, 0}},
};

const TowerStats* GetTowerStats(TowerType type, TowerLevel level) {
	auto it = TowerConstants.find({ type, level });
	if (it == TowerConstants.end()) return nullptr;
	return &it->second;
}





static const std::map<TowerType, TowerInfo> TowerInfoConstants = {
	{TowerType::Archer, {"Archer", "Shoot projectiles", false}},
	{TowerType::Artillery, {"Artillery", "Explosion", false}},
	{TowerType::Ice, {"Ice", "Slow down enemies", false}},
	{TowerType::Leach, {"Leach", "Shoot projectiles", true}},
	{TowerType::Mage, {"Mage", "Set enemies on fire", false}},
	{TowerType::Necromancer, {"Necromancer", "Summon troops", true}},
	{TowerType::Poison, {"Poison", "Poison enemies", false}},
	{TowerType::Quake, {"Quake", "Shoot projectiles", false}},
	{TowerType::Sorcerer, {"Sorcerer", "Summon friendly troops", false}},
	{TowerType::Turret, {"Turret", "Shoot arrow very fast", true}},
	{TowerType::Zeus, {"Zeus", "Strike lightning", false}},
};

const TowerInfo& GetTowerInfo(TowerType type) {
	return TowerInfoConstants.at(type);
}





namespace TowerFactory {

using TowerCreator = std::function<std::shared_ptr<Tower>(TowerID, std::int32_t, std::int32_t, PlayerID)>;

static const std::map<TowerType, TowerCreator> towerFactory = {
	{TowerType::Archer,         [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<ArcherTower>(id, x, y , builder);} },
	{TowerType::Artillery,      [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<ArtilleryTower>(id, x, y , builder);} },
	{TowerType::Ice,            [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<IceTower>(id, x, y , builder);} },
	{TowerType::Mage,           [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<MageTower>(id, x, y , builder);} },
	{TowerType::Poison,         [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<PoisonTower>(id, x, y , builder);} },
	{TowerType::Quake,          [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<QuakeTower>(id, x, y , builder);} },
	{TowerType::Sorcerer,       [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<SorcererTower>(id, x, y , builder);} },
	{TowerType::Zeus,           [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<ZeusTower>(id, x, y , builder);} },
	{TowerType::Leach,          [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<LeachTower>(id, x, y , builder);} },
	{TowerType::Necromancer,    [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<NecromancerTower>(id, x, y , builder);} },
	{TowerType::Turret,         [](TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) -> TowerPtr {return std::make_shared<TurretTower>(id, x, y , builder);} },
};

TowerPtr CreateTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y, PlayerID builder) {
	return towerFactory.at(type)(id, x, y, builder);
}

std::string GetTowerName(TowerType type) {
	switch (type) {

	case TowerType::Archer:
		return "Archer";
	case TowerType::Artillery:
		return "Artillery";
	case TowerType::Ice:
		return "Ice";
	case TowerType::Mage:
		return "Mage";
	case TowerType::Poison:
		return "Poison";
	case TowerType::Quake:
		return "Quake";
	case TowerType::Sorcerer:
		return "Sorcerer";
	case TowerType::Zeus:
		return "Zeus";
	case TowerType::Leach:
		return "Leach";
	case TowerType::Necromancer:
		return "Necromancer";
	case TowerType::Turret:
		return "Turret";
	default:
		return "Unknow";

	}
}

} // namespace TowerFactory
} // namespace game
} // namespace td
