package gui.menu;

import java.io.IOException;

import imgui.ImGui;
import imgui.ImVec2;
import imgui.type.ImInt;
import imgui.type.ImString;

public class MultiMenu extends BaseView {

	private final ImInt port = new ImInt(25565);
	private final ImString address = new ImString("localhost");

	public MultiMenu(StateMachine stateMachine) {
		super(stateMachine);
		address.resize(20);
	}

	private void renderCreate() {
		ImVec2 displaySize = ImGui.getIO().getDisplaySize();
		ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
		if (ImGui.inputInt("Port", port))
			port.set(Math.clamp(port.get(), 1, 65535));
		if (ImGui.button("Créer")) {
			try {
				this.stateMachine.pushState(new ConnexionStatusView(stateMachine, (short) port.get()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		ImGui.endChild();
	}

	private void renderJoin() {
		ImVec2 displaySize = ImGui.getIO().getDisplaySize();
		ImGui.beginChild("##JoinGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
		ImGui.inputText("Adresse", address);
		if (ImGui.inputInt("Port", port))
			port.set(Math.clamp(port.get(), 1, 65535));
		if (ImGui.button("Rejoindre")) {
			try {
				this.stateMachine.pushState(new ConnexionStatusView(stateMachine, address.get(), (short) port.get()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		ImGui.endChild();
	}

	@Override
	public void render() {

		renderCreate();
		ImGui.sameLine();
		renderJoin();

		ImVec2 displaySize = ImGui.getIO().getDisplaySize();
		ImVec2 buttonSize = new ImVec2(500, 70.0f);

		float centerX = displaySize.x / 2.0f - buttonSize.x / 2.0f;

		ImGui.setCursorPosX(centerX);
		if (ImGui.button("Retour", buttonSize)) {
			closeMenu();
		}
	}

}
