#include "client/gui/CreateGameMenu.h"

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

namespace blitz {
namespace gui {

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

	SetNextWindowFullScreen();

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

	ImGui::Begin("CreateWindow", nullptr, GetWindowFullScreenFlags());
	{
		ImGui::Text("Creer une partie");

		ImGui::NewLine();

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

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

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

		if (ImGui::Button("Creer")) {
			utils::LOGD(utils::Format("[CreateGameMenu] Port saisi : %i", InputPort));

			if (m_Client->CreateGame(InputPort, m_Client->GetConfig()->GetPseudo().data())) {
				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
