#include "client/gui/ServerGui.h"

#include "client/Client.h"
#include "client/display/InputManager.h"
#include "client/game/ClientGame.h"
#include "server/Server.h"
#include "server/game/ServerGame.h"
#include <imgui.h>

namespace blitz {
namespace gui {

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

void ServerGui::Render() {
	if (!m_Client->IsAdmin())
		return;

	Keybinds keys = m_Client->GetConfig()->GetKeys();
	if (ImGui::IsKeyPressed(ImGuiKey(keys[kaFenetreAdmin])) && InputManager::MouseGrabbed()) {
		ImGui::OpenPopup("FENETRE D'ADMIN");
	}

	ImGuiWindowFlags servergui_flags =
		ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar;
	ImVec2 center = ImGui::GetMainViewport()->GetCenter();
	static float servergui_width = 1500.0f;
	static float servergui_height = 800.0f;

	const static ImVec2 buttonSize = {300, 60};

	ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
	ImGui::SetNextWindowSize(ImVec2(servergui_width, servergui_height));
	if (ImGui::BeginPopupModal("FENETRE D'ADMIN", nullptr, servergui_flags)) {
		InputManager::GrabMouse(false);

		ImGui::BeginChild("Mutateurs JOUEURS", ImVec2(servergui_width * (1.0f / 3.0f), 0.0f), true);
		{
			ImGui::Text("JOUEURS");
			ImGui::NewLine();

			ImGui::Text("AJOUTER JOUEUR");
			if (ImGui::Button("Ajouter Bot", buttonSize)) {
				m_Client->GetServer()->AddBot();
			}

			ImGui::NewLine();

			ImGui::Text("KICK JOUEURS");
			bool hasOtherPlayers = false;
			auto& players = m_Client->GetGame()->GetLeaderBoard().GetPlayers();
			float kickButtonSizex = ImGui::CalcTextSize("Kick").x + 50.0f;
			float kickButtonSizey = ImGui::CalcTextSize("Kick").y + 20.0f;
			for (game::Player* player : players) {
				if (player->GetID() == m_Client->GetPlayerID()) {
					continue;  // Skip the current player
				}
				hasOtherPlayers = true;
				ImGui::Text("%s", player->GetName().c_str());
				ImGui::SameLine();
				ImGui::PushID(player->GetID());
				if (ImGui::Button("Kick", {kickButtonSizex, kickButtonSizey})) {
					m_Client->GetServer()->KickPlayer(player->GetID());
				}
				ImGui::PopID();
			}

			if (!hasOtherPlayers) {
				ImGui::Text("Aucun autre joueur");
			}
		}
		ImGui::EndChild();

		ImGui::SameLine();

		ImGui::BeginChild("Mutateurs PARTIE", ImVec2(servergui_width * (1.0f / 3.0f), 0.0f), true);
		ImGui::Text("PARTIE");

		ImGui::NewLine();
		ImGui::Text("GRAVITE");
		float gravity[] = {20.0f, 7.0f, 40.0f};
		const char* planetes[] = {"Terrestre", "Lunaire", "Solaire"};
		static int item_current_idx;
		float currentGravity = m_Client->GetConfig()->GetServerConfig().Gravity;
		for (int n = 0; n < IM_ARRAYSIZE(planetes); n++) {
			if (gravity[n] == currentGravity) {
				item_current_idx = n;
				break;
			}
		}
		if (ImGui::BeginListBox("##Gravity", ImVec2(300.0f, 100.0f))) {
			for (int n = 0; n < IM_ARRAYSIZE(planetes); n++) {
				const bool is_selected = (item_current_idx == n);
				if (ImGui::Selectable(planetes[n], is_selected)) {
					item_current_idx = n;
					m_Client->GetConfig()->GetServerConfig().Gravity = gravity[n];
					m_Client->UpdateServerConfig();
				}

				if (is_selected)
					ImGui::SetItemDefaultFocus();
			}
			ImGui::EndListBox();
		}

		ImGui::NewLine();
		int& firingrate = m_Client->GetConfig()->GetServerConfig().FiringRate;
		ImGui::SetNextItemWidth(300.0f);
		if (ImGui::DragInt("##CADENCE TIR", &firingrate, 1, 1, 1000)) {
			firingrate = std::clamp(firingrate, 1, 1000);
			m_Client->UpdateServerConfig();
		}
		ImGui::Text("Cpm/Rpm %i", firingrate);

		ImGui::SetCursorPosY(servergui_height - 2.0f * buttonSize.y);
		if (ImGui::Button("Retour", buttonSize)) {
			ImGui::CloseCurrentPopup();
			InputManager::GrabMouse(true);
		}
		ImGui::EndChild();

		ImGui::SameLine();

		ImGui::BeginChild("MUTATEUR TEMPS", ImVec2(servergui_width * (0.9f / 3.0f), 0.0f), true);
		static int gameDurMin = static_cast<int>(m_Client->GetServer()->GetGame().GetServerDuration().m_GameDuration / 1000) / 60;
		static int gameDurSec = static_cast<int>(m_Client->GetServer()->GetGame().GetServerDuration().m_GameDuration / 1000) % 60;

		ImGui::Text("TEMPS");
		ImGui::NewLine();

		ImGui::Text("DUREE PARTIE");
		std::string min = "Min";
		std::string sec = "Sec";

		for (std::size_t i = 0; i < 2; ++i) {
			ImGui::PushID(static_cast<int>(i));
			if (i == 0) {
				ImGui::Text("%s", min.c_str());
				ImGui::SameLine();
				ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40);
				if (ImGui::VSliderInt("##Min", ImVec2(40, 160), &gameDurMin, 1, 10)) {
					m_Client->GetServer()->GetGame().GetServerDuration().m_GameDuration = (gameDurMin * 60 + gameDurSec) * 1000;
				}
				ImGui::PopStyleVar();
			} else {
				ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x * 10);
				ImGui::Text("%s", sec.c_str());
				ImGui::SameLine();
				ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40);
				if (ImGui::VSliderInt("##Sec", ImVec2(40, 160), &gameDurSec, 0, 59)) {
					m_Client->GetServer()->GetGame().GetServerDuration().m_GameDuration = (gameDurMin * 60 + gameDurSec) * 1000;
				}
				ImGui::PopStyleVar();
			}
			ImGui::PopID();
		}

		ImGui::EndChild();
		ImGui::EndPopup();
	}
}

}  // namespace gui
}  // namespace blitz
