package network.server;

import java.io.IOException;
import java.net.Socket;

import game.Player;
import game.Game.GameState;
import network.Connexion;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;

public class ServerConnexion extends Connexion {

	private final Server server;
	private final KeepAliveHandler keepAliveHandler;
	private boolean shouldClose = false;
	private Player player = null;

	public ServerConnexion(Socket socket, Server server) throws IOException {
		super(socket);
		this.server = server;
		this.keepAliveHandler = new KeepAliveHandler(this);
	}

	public boolean update() {
		if (shouldClose | isClosed())
			return false;
		return this.keepAliveHandler.update();
	}

	public void nukeConnection() {
		if (player != null) {
			sendPacket(new DisconnectPacket("Le serveur a été fermé !"));
			this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
			this.server.getGame().removePlayer(player.getId());
		}
	}

	@Override
	public synchronized void close() {
		if(shouldClose)
			return;
		super.close();
		shouldClose = true;
		System.out.println("[Server] Closing connexion !");
	}

	private void finishLogin() {
		// send players that have already joined (excluding this one)
		for (Player p : this.server.getGame().getPlayers().values()) {
			if (p.getId() != player.getId())
				sendPacket(new PlayerJoinPacket(p));
		}
		this.server.broadcastPacket(new PlayerJoinPacket(player));
		sendPacket(new ConnexionInfoPacket(player.getId()));
		if (this.server.getGame().getGameState() == GameState.GameGoing) {
			sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
		}
	}

	@Override
	public void visitPacket(KeepAlivePacket packet) {
		this.keepAliveHandler.recievedKeepAlive(packet.getKeepAliveId());
	}

	@Override
	public void visitPacket(DisconnectPacket packet) {
		close();
	}

	@Override
	public void visitPacket(LoginPacket packet) {
		if (this.player != null)
			return;
		this.player = this.server.addPlayer(packet.getPseudo());
		finishLogin();
	}

	@Override
	public void visitPacket(ConnexionInfoPacket packet) {
		throw new UnsupportedOperationException("Unimplemented method 'visitPacketConnexionInfo'");
	}

	@Override
	public void visitPacket(PlayerJoinPacket packet) {
		throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerJoin'");
	}

	@Override
	public void visitPacket(PlayerLeavePacket packet) {
		throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerLeave'");
	}

	@Override
	public void visitPacket(StartGamePacket packet) {
		throw new UnsupportedOperationException("Unimplemented method 'visitPacketStartGame'");
	}

}
