package gui.menu;

import java.util.Arrays;

import game.Player;
import imgui.ImGui;
import network.client.Client;
import network.server.Server;
import sudoku.constraint.Constraint;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;

public class MultiPlayerView extends BaseView {

	private final Client client;
	private final Server server;

	public MultiPlayerView(StateMachine stateMachine, Client client, Server server) {
		super(stateMachine);
		this.client = client;
		this.server = server;
		this.client.onDisconnect.connect(this::onDisconnect);
		this.client.onGameStarted
				.connect(() -> this.stateMachine.pushState(new MultiPlayerDokuView(stateMachine, client, server)));
	}

	@Override
	public void closeMenu() {
		this.client.forceDisconnect();
		super.closeMenu();
	}

	public void onDisconnect() {
		this.stateMachine.popState();
	}

	public void renderGameStatus() {
		if (this.server == null) {
			ImGui.text("En attente de l'administrateur du serveur ...");
		} else {
			if (ImGui.button("Démarrer")) {
				// temp
				MultiDoku doku = SudokuFactory.createBasicXShapedMultidoku(3, Arrays.asList(Constraint.Diagonal));
				this.server.startGame(doku);
			}
		}
	}

	@Override
	public void render() {
		ImGui.text("Joueurs :");
		{
			for (Player player : this.client.getGame().getPlayers().values()) {
				ImGui.bulletText(player.getPseudo());
			}
		}
		renderGameStatus();
	}

}
