public class Game {
	// Instanzvariablen - ersetzen Sie das folgende Beispiel mit Ihren Variablen
	WordleServer wordleServer;
	User user;
	static DatabaseConnector databaseConnector;// Datenbankverbindung aufstellen

	private String ip;
	private int port;

	private String wordle;
	private String[] words;
	private int count;
	private int phase;

	static {

		databaseConnector = new DatabaseConnector(null, 0, "wordle.db", null, null);
		
		System.err.println(databaseConnector.getErrorMessage());
		
	}

	public Game(WordleServer wordleServer, String pClientIP, int pClientPort) {

		this.wordleServer = wordleServer;
		this.ip = pClientIP;
		this.port = pClientPort;

		send("+OK Hello there");

	}

	public void processMessage(String msg) {

		msg = msg.toLowerCase().trim();

		if (msg.equals("quit")) {
			send("+OK bye");
			wordleServer.closeConnection(ip, port);
			System.exit(0);
		}

		switch (phase) {
		case 0:
			loginPhase(msg);
			break;
		case 1:
			gamePhase(msg);
			break;
		case 2:
			endPhase(msg);
			break;
		}
	}

	private void loginPhase(String msg) {
		
		if (msg.length() <= 5) {
			sendUniversalError();
			return;
		}

		String args = msg.substring(5);
		if (msg.startsWith("user ")) {

			databaseConnector.executeStatement("SELECT * FROM User WHERE name = '" + args + "'");
			System.err.println(databaseConnector.getErrorMessage());
			QueryResult result = databaseConnector.getCurrentQueryResult();
			
			if (result != null) {
				String name = result.getData()[0][0];
				System.out.println(name);
				user = new User(name);
			} else {
				send("-ERR User not found.");
			}

		} else if (msg.startsWith("pass ")) {

		} else if (msg.startsWith("wotd ")) {

		} else if (msg.startsWith("play ")) {

		} else {
			sendUniversalError();
		}
	}

	private void gamePhase(String msg) {
		if (msg.startsWith("send")) {

			String args = msg.substring(5);
            //vergleichen von wotd mit
            //ja = spiel beendet und ausgabe word 11111
            //nein = ausgabe 01102 oder so und prüfen ob man noch ein wort eingebn kann
            // ja game continues nein = game lost
			
		} else if (msg.startsWith("info")) {

		} else if (msg.startsWith("exit")) {

		}
	}

	private void endPhase(String msg) {
		if (msg.startsWith("stat")) {

		} else if (msg.startsWith("play")) {

		}
	}

	private void send(String msg) {
		wordleServer.send(ip, port, msg);
	}
	
	private void sendUniversalError() {
		send("-ERR command not valid in this state");
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
}
