import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.print.attribute.standard.NumberUp;

/**
 * Serves Hangman for telnet (the terminal version of the Hangman game). All
 * methods and fields are static.
 */
public class Server {
	// TODO TCP-Server-Socket and -Clients-Sockets definition.

	/** Server Socket */
	private static ServerSocket welcomeSocket;

	/** An array  socket for n players */
	private static Socket[] connectSocket;



	/** A BufferedWriter for each player. */
	private static BufferedWriter[] writers;

	/** A BufferedReader for each player; */
	private static BufferedReader[] readers;

	/** Number of players. */
	private static final int NUM_PLAYERS = 2;

	/** Currently active player (0 - n-1). */
	private static int curPlayer;

	/**
	 * Initializes the game. Loops until solution is found or hangman is dead.
	 * 
	 * @param argv
	 *            Optional command line arguments.
	 * @throws Exception
	 */
	public static void main(String[] argv) throws Exception {
		// TODO Init game and hangman.
		initGame();
		Hangman hangman = new Hangman();
		String clientSentence;
		char c;

		



		while (true)// TODO Loop until solution is found or hangman is dead.
		{
			// TODO Inform players and read input.
			writeToAll(hangman.getHangman());
			writeToAllButCur("\n Please wait your tour ;) \n");
			writers[curPlayer].write("\nPlease enter a character or a word starting with \"!\" :  ");
			writers[curPlayer].flush();
			clientSentence = readers[curPlayer].readLine();



			// TODO Process input and inform players.
			if(!clientSentence.isEmpty())
				c = clientSentence.charAt(0);
			else
				c = ' ';
			if(c == '!') {
				writers[curPlayer].write(hangman.checkWord(clientSentence.replace("!", ""))+"\n");
				writers[curPlayer].flush();
			} else {
				writers[curPlayer].write(hangman.checkChar(c)+"\n");
				writers[curPlayer].flush();
			}




			// TODO Set curPlayer to next player.
			curPlayer++;
			if(curPlayer == NUM_PLAYERS)
				curPlayer = 0;


			if(hangman.win() == true || hangman.dead() == true)
				break;
		}

		// TODO Inform players about the game result.
		writeToAll(hangman.getHangman());





		// TODO Close player sockets.
		for(int i = 0; i < NUM_PLAYERS; i++) {
			connectSocket[i].close();
		}
		welcomeSocket.close();



	}

	/**
	 * Initializes sockets until number of players {@link #NUM_PLAYERS
	 * NUM_PLAYERS} is reached.
	 * 
	 * @throws Exception
	 */
	private static void initGame() throws Exception {
		// TODO Initialize sockets/arrays and current player.

		/** open the Server */
		welcomeSocket = new ServerSocket(2345);

		/** Init array of players socket */
		connectSocket = new Socket[NUM_PLAYERS];

		/** init readers and writers */
		writers = new BufferedWriter[NUM_PLAYERS];
		readers = new BufferedReader[NUM_PLAYERS];


		curPlayer = 0;

		while (true)// TODO Not all players connected
		{
			// TODO Initialize socket and reader/writer for every new connected
			// player.
			connectSocket[curPlayer] = welcomeSocket.accept();
			writers[curPlayer] = new BufferedWriter(
				new OutputStreamWriter(connectSocket[curPlayer].getOutputStream(), "UTF-8"));
			readers[curPlayer] = new BufferedReader(
				new InputStreamReader(connectSocket[curPlayer].getInputStream(), "UTF-8"));



			// TODO Welcome new player and increment current player.
			writers[curPlayer].write("\nWelcome to the play :)\n");
			writers[curPlayer].flush();

			curPlayer++;
			if(curPlayer == NUM_PLAYERS)
				break;



		}
		// TODO Reset current player.
		curPlayer = 0;


		// TODO Prevent more connections to be established. Inform players about
		// start of the game.
		welcomeSocket.close();
		writeToAll("Now beginn the Play");



	}

	/**
	 * Writes the String s to all players.
	 * 
	 * @param s
	 *            The String to be sent.
	 * @throws Exception
	 */
	private static void writeToAll(String s) throws Exception {
		// TODO
		for(int i = 0; i < NUM_PLAYERS; i++) {
			writers[i].write(s);
			writers[i].flush();
		}

	}

	/**
	 * Writes the String s to all players but to the current player.
	 * 
	 * @param s
	 *            The String to be sent.
	 * @throws Exception
	 */
	private static void writeToAllButCur(String s) throws Exception {
		// TODO
		for(int i = 0; i < NUM_PLAYERS; i++) {
			if(i != curPlayer) {
				writers[i].write(s);
				writers[i].flush();
			}
		}


	}

}