#include "Tracers.h"
#include "Client.h"

#include "Utils/PlayerUtils.h"
#include "Utils/ImGuiRenderUtils.h"
#include "Utils/MoveUtils.h"
#include "Utils/ColorUtils.h"

auto Tracers::onClient() -> void {
	auto clientinstance = Client::clientinstance;
	if (!clientinstance) return;

	auto localplayer = clientinstance->getLocalPlayer();
	if (!localplayer) return;

	auto level = localplayer->getLevel();
	if (!level) return;

	if (!enabled) return;
	if (Client::screenName != _("hud_screen")) {
		renderdatas.clear();
		return;
	}

	std::vector<Player*> list;
	level->forEachPlayer([&](Player& player) {
		if (Utils::Players::isRealPlayer(&player)) {
			list.push_back(&player);
		}

		return true;
		});

	std::vector<RenderData> renderdatas_;
	for (Player* player : list) {
		if (player == localplayer) continue;

		auto aabbshapecomponent = player->getAABBShapeComponent();
		if (!aabbshapecomponent) continue;

		ImVec2 worldpos = Utils::ImGuiRender::getWorldPosition(aabbshapecomponent->mAABB.max);
		if (worldpos.x > 0.f && worldpos.y > 0.f) {
			renderdatas_.push_back({
				worldpos
				});
		}
	}
	renderdatas = renderdatas_;
}

auto Tracers::onImGuiRender() -> void {
	if (Client::screenName != _("hud_screen")) return;

	Utils::ImGuiRender::setDrawList(ImGui::GetBackgroundDrawList());
	Utils::ImGuiRender::setColor(Utils::Color::getColor(3.f, 0, 1.f));
	Utils::ImGuiRender::setThickness(2.f);

	ImVec2 center = ImVec2(Client::windowsize.x / 2, Client::windowsize.y / 2);

	for (RenderData renderdata : renderdatas) {
		Utils::ImGuiRender::drawLine(center, renderdata.pos);
	}
}
