#include "client/gui/JoinGameMenu.h"

#include "blitz/misc/Format.h"
#include "blitz/misc/Log.h"
#include "client/Client.h"
#include "client/display/InputManager.h"
#include <imgui.h>
#include <string>

namespace blitz {
namespace gui {

void JoinGameMenu::Render() {
	if (!IsEnabled())
		return;

	SetNextWindowFullScreen();

	static int InputPort = 25565;
	const static ImVec2 buttonSize = {300, 60};
	auto displaySize = ImGui::GetIO().DisplaySize;
	const static int paddingHeight = 40;

	ImGui::Begin("JoinWindow", nullptr, GetWindowFullScreenFlags());
	{
		ImGui::Text("Rejoindre une partie");

		ImGui::NewLine();

		ImGui::InputText("Pseudo", m_Client->GetConfig()->GetPseudo().data(), m_Client->GetConfig()->GetPseudo().size());

		static char InputAddress[256] = "localhost";
		ImGui::InputText("Adresse", InputAddress, sizeof(InputAddress));

		ImGui::Text("Adresse saisie : %s", InputAddress);

		ImGui::InputInt("Port", &InputPort);

		ImGui::Text("Port saisi : %d", InputPort);

		if (ImGui::Button("Rejoindre")) {
			utils::LOGD(utils::Format("[JoinGameMenu] Adresse saisie : %s, Port saisi : %i", InputAddress, InputPort));

			if (m_Client->JoinGame(m_Client->GetConfig()->GetPseudo().data(), InputAddress, InputPort)) {
				InputManager::GrabMouse(true);
				Disable();
			}
		}

		ImGui::SetCursorPosX(displaySize.x - buttonSize.x - paddingHeight);
		ImGui::SetCursorPosY(displaySize.y - buttonSize.y - paddingHeight);
		if (ImGui::Button("Retour", buttonSize) || ImGui::IsKeyPressed(ImGuiKey_Escape)) {
			Disable();
			m_Parent->Enable();
		}
	}
	ImGui::End();
}

}  // namespace gui
}  // namespace blitz
