#include "client/gui/LeaderBoardGui.h"

#include "blitz/misc/Format.h"
#include "blitz/misc/Log.h"
#include "client/Client.h"
#include "client/game/ClientGame.h"
#include "client/gui/GuiWidget.h"
#include <imgui.h>

namespace blitz {
namespace gui {

LeaderBoardGui::LeaderBoardGui(GuiWidget* parent, Client* client) : GuiWidget(parent, client) {}

LeaderBoardGui::~LeaderBoardGui() {}

void LeaderBoardGui::Draw(const char* title, bool* p_open) {
	static float leaderboard_width = 800.0f;
	static float leaderboard_height = 450.0f;

	ImGuiWindowFlags leaderboard_flags =
		ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize;

	ImVec2 center = ImGui::GetMainViewport()->GetCenter();
	ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
	ImGui::SetNextWindowSize(ImVec2(leaderboard_width, leaderboard_height));
	ImGui::Begin(title, p_open, leaderboard_flags);
	{
		std::string playerLeaderBoard = "Player";
		std::string killsLeaderBoard = "Kills";
		std::string deathsLeaderBoard = "Deaths";
		std::string kdLeaderBoard = "K/D";
		std::string precisionLeaderBoard = "Accuracy";
		for (game::Player* player : m_Client->GetGame()->GetLeaderBoard().GetPlayers()) {
			playerLeaderBoard += utils::Format("\n%s", player->GetName().c_str());
			killsLeaderBoard += utils::Format("\n%i", player->GetStats().m_Kills);
			deathsLeaderBoard += utils::Format("\n%i", player->GetStats().m_Deaths);

			// Check if the denominator is zero before calculating K/D ratio
			float kdRatio = (player->GetStats().m_Deaths != 0)
								? static_cast<float>(player->GetStats().m_Kills) / player->GetStats().m_Deaths
								: 0.0f;
			kdLeaderBoard += utils::Format("\n%.2f", kdRatio);

			// Check if the denominator is zero before calculating precision percentage
			float precisionPercentage =
				(player->GetStats().m_ShootCount != 0)
					? static_cast<float>(player->GetStats().m_ShootSuccessCount) / player->GetStats().m_ShootCount * 100.0f
					: 0.0f;
			precisionLeaderBoard += utils::Format("\n%.2f%%", precisionPercentage);
		}
		ImGui::Text("%s", playerLeaderBoard.c_str());
		ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x * 10);
		ImGui::Text("%s", killsLeaderBoard.c_str());
		ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x * 10);
		ImGui::Text("%s", deathsLeaderBoard.c_str());
		ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x * 10);
		ImGui::Text("%s", kdLeaderBoard.c_str());
		ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x * 10);
		ImGui::Text("%s", precisionLeaderBoard.c_str());
	}
	ImGui::End();
}

void LeaderBoardGui::Render() {
	if (!m_Client->IsConnected())
		return;

	if (ImGui::IsKeyDown(ImGuiKey_Tab) && !(m_Client->GetGame()->GetGameState() == game::GameState::gsEnd)) {
		Draw("Leaderboard", nullptr);
	}

	if (m_Client->GetGame()->GetGameState() == game::GameState::gsEnd) {
		if (m_Timer.Update(ImGui::GetIO().DeltaTime)) {
			Draw("Leaderboard", nullptr);
		}
	} else {
		m_Timer.Reset();
	}
}

}  // namespace gui
}  // namespace blitz