#include "PlayerList.h"
#include "Client.h"
#include "Utils/PlayerUtils.h"
#include "Utils/StringUtils.h"
#include "Utils/ImGuiRenderUtils.h"
#include "Utils/MathUtils.h"

#include "Mod/Interface.h"

auto PlayerList::onClient() -> void {
	if (Client::screenName != _("hud_screen")) {
		renderdatas.clear();
		return;
	}
}

auto PlayerList::onBaseTick(Player* _this) -> void {
	auto clientinstance = Client::clientinstance;
	if (!clientinstance) return;

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

	if (_this != localplayer) return;

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

	if (!enabled) return;

	std::vector<Player*> list;
	for (Actor* actor : level->getRuntimeActorList()) {
		if (actor && actor->isAlive() && Utils::Players::isRealPlayer((Player*)actor)) {
			list.push_back((Player*)actor);
		}
	}

	std::vector<RenderData> renderdatas_;
	for (Player* player : list) {
		renderdatas_.push_back({
		Utils::String::removeToEscape(Utils::String::removeSymbols(player->getNameTag()))
			});
	}

	renderdatas = renderdatas_;
}

auto PlayerList::onImGuiRender() -> void {
	auto interfaceMod = Client::findMod<Interface>(_("Interface"));

	if (Client::screenName != _("hud_screen")) return;

	int pagesize = 1;
	if (renderdatas.size() >= 10)
		pagesize += ((static_cast<int>(renderdatas.size()) - 2) / 10);

	Utils::ImGuiRender::setFont(ImGui::GetIO().Fonts->Fonts[0 + (4 * interfaceMod->font)]);
	Utils::ImGuiRender::setSize(1.5f);
	ImVec2 maxsize = ImVec2();
	for (RenderData renderdata : renderdatas) {
		ImVec2 size = Utils::ImGuiRender::getTextSize(renderdata.name);
		if (maxsize.x < size.x) {
			maxsize = size;
		}
	}

	static float animation = 0.f;
	animation = Utils::Math::animate(enabled ? 1.f : 0.f, animation, Client::deltaTime * 5.f);
	ImVec4 rect = ImVec4(Client::windowsize.x / 2 - ((maxsize.x / 2 + 25.f) * pagesize), -200.f + ((200.f + 20.f) * animation), Client::windowsize.x / 2 + ((maxsize.x / 2 + 25.f) * pagesize), ((maxsize.y * (10 + 1)) + 20.f) * animation);
	Utils::ImGuiRender::setDrawList(ImGui::GetBackgroundDrawList());
	Utils::ImGuiRender::setRound(10.f);
	Utils::ImGuiRender::setColor(ImColor(0.f, 0.f, 0.f, 0.6f * animation));
	Utils::ImGuiRender::fillRectangle(rect);

	Utils::ImGuiRender::setBlurStreangth(blurs);
	Utils::ImGuiRender::fillBlurRectangle(rect);

	Utils::ImGuiRender::setColor(ImColor(0.f, 0.f, 0.f, animation));
	Utils::ImGuiRender::setSize(100.f);
	Utils::ImGuiRender::setThickness(200.f);
	Utils::ImGuiRender::fillShadowRectangle(rect);
	Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, animation));

	Utils::ImGuiRender::setSize(1.5f);
	ImVec2 pos = ImVec2(rect.x + 25.f, rect.y);
	int i_ = 10;
	for (int i = 0; i < renderdatas.size(); i++) {
		for (Client::UserData data : Client::users) {
			if (data.mcid == renderdatas[i].name) {
				Utils::ImGuiRender::setColor(ImColor(1.f, 0.5f, 1.f));
				break;
			}
			else {
				Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, animation));
			}
		}

		Utils::ImGuiRender::drawText(renderdatas[i].name, pos);
		pos.y += maxsize.y * animation;
		if (i == i_) {
			pos.x += maxsize.x + 50.f;
			pos.y = rect.y;
			i_ += 10;
		}
	}
}
