#include "client/game/WorldClient.h"
#include "td/protocol/PacketDispatcher.h"
#include "client/game/ClientGame.h"
#include "client/render/WorldRenderer.h"

#include "td/protocol/packets/WorldAddTowerPacket.h"
#include "td/protocol/packets/WorldBeginDataPacket.h"
#include "td/protocol/packets/WorldDataPacket.h"
#include "td/protocol/packets/SpawnMobPacket.h"
#include "td/protocol/packets/UpgradeTowerPacket.h"
#include "td/protocol/packets/RemoveMobPacket.h"
#include "td/protocol/packets/RemoveTowerPacket.h"
#include "td/protocol/packets/UpdateCastleLifePacket.h"
#include "td/protocol/packets/UpdateMobStatesPacket.h"

namespace td {
namespace client {

WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::PacketHandler(game->GetDispatcher()), m_Game(game) {
	GetDispatcher()->RegisterHandler(protocol::PacketType::WorldAddTower, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::WorldBeginData, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveMob, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveTower, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateCastleLife, this);
	GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMobStates, this);
}

WorldClient::~WorldClient() {
	GetDispatcher()->UnregisterHandler(this);
}

void WorldClient::HandlePacket(const protocol::WorldBeginDataPacket* packet) {
	LoadMap(packet);
	if (m_Game->GetGameState() == game::GameState::Game) {
		const Color& backgroundColor = GetBackgroundColor();
		m_Game->GetRenderer()->SetBackgroundColor({ static_cast<float>(backgroundColor.r / 255.0f), static_cast<float>(backgroundColor.g / 255.0f),
			static_cast<float>(backgroundColor.b / 255.0f) });
	}
}

void WorldClient::HandlePacket(const protocol::WorldDataPacket* packet) {
	LoadMap(packet);
}

void WorldClient::HandlePacket(const protocol::SpawnMobPacket* packet) {
	SpawnMobAt(packet->GetMobID(), packet->GetMobType(), packet->GetMobLevel(), packet->GetSender(),
		packet->GetMobX(), packet->GetMobY(), packet->GetMobDirection());
}

void WorldClient::HandlePacket(const protocol::UpgradeTowerPacket* packet) {
	game::TowerPtr tower = GetTowerById(packet->GetTowerID());

	SAFE_CHECK(tower);

	tower->Upgrade(packet->GetTowerLevel().GetLevel(), packet->GetTowerLevel().GetPath());
}

void WorldClient::HandlePacket(const protocol::WorldAddTowerPacket* packet) {
	game::TowerPtr newTower = PlaceTowerAt(packet->GetTowerID(), packet->GetTowerType(), packet->GetTowerX(), packet->GetTowerY(), packet->GetBuilder());

	GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerAdd, newTower);
}

void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
	game::TowerPtr tower = RemoveTower(packet->GetTowerID());

	SAFE_CHECK(tower);

	GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower);
}

void WorldClient::HandlePacket(const protocol::RemoveMobPacket* packet) {
	game::MobPtr mob = RemoveMob(packet->GetMobID());

	SAFE_CHECK(mob);

	//GetWorldNotifier().NotifyListeners(&MobListener::OnMobDie, mob.get());
}

void WorldClient::HandlePacket(const protocol::UpdateMobStatesPacket* packet) {
	for (auto mobState : packet->GetMobStates()) {
		game::MobID mobId = mobState.GetMobId();
		auto it = std::find_if(GetMobList().begin(), GetMobList().end(), [mobId](game::MobPtr mob) { return mob->GetMobID() == mobId; });
		if (it != GetMobList().end()) {
			game::MobPtr& mob = *it;
			mob->SetCenter(mobState.GetMobPosition());
			mob->SetDirection(mobState.GetMobDirection());
			mob->SetHealth(mobState.GetMobLife());
		}
	}
}

void WorldClient::HandlePacket(const protocol::UpdateCastleLifePacket* packet) {
	GetTeam(packet->GetTeamColor()).GetCastle().SetLife(packet->GetCastleLife());
}

} // namespace client
} // namespace td
