import java.time.Instant;
import java.util.HashMap;

/**
 * Server für das WORDLE Spiel.
 */
public class WordleServer extends Server {

	public static final int DEFAULT_PORT = 1000;

	public static void main(String[] args) {
		new WordleServer();
	}

	private DatabaseConnector db;

	private HashMap<String, User> users;
	private Query query;

	public WordleServer() {
		this(DEFAULT_PORT);
	}

	public WordleServer(int pPort) {
		super(pPort);
		query = new Query();
		db = new DatabaseConnector("", 0, "wordle.db", "", "");
		users = new HashMap<>();
	}

	@Override
	public void processNewConnection(String pClientIP, int pClientPort) {
		String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
		User u = new User(connectionID);
		users.put(connectionID, u);
		send(pClientIP, pClientPort, "+OK Verbindung zum Wordle Server erfolgreich aufgebaut");
		u.setState(1);

	}

	@Override
	public void processClosingConnection(String pClientIP, int pClientPort) {

	}

	@Override
	public void processMessage(String pClientIP, int pClientPort, String pMessage) {
		String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
		User u = users.get(connectionID);
		u.setConnectionID(connectionID);

		if (pMessage.equals("QUIT")) {
			send(pClientIP, pClientPort, "+OK Bye");
			close();
		}

		String m = pMessage.substring(0, 3);
		switch (u.getState()) {
		case 1:
			// Anmeldephase
			if (m.equalsIgnoreCase("USER")) {
				m = pMessage.substring(6, pMessage.length() - 1);
				u.setUsername(m);
				send(pClientIP, pClientPort, "+OK");
			} else if (m.equalsIgnoreCase("PASS")) {
				m = pMessage.substring(6, pMessage.length() - 1);
				u.setPassword(m);
				if (u.getPassword() != null && u.getUsername() != null) {
					if (query.checkLogin(u.getUsername(), u.getPassword())) {
						send(pClientIP, pClientPort, "+OK welcome");
						u.setState(3);
					} else {
						send(pClientIP, pClientPort, "+ERR login not valid");
					}
				}
			} else if(m.equalsIgnoreCase("WOTD")) {
				
			} else if(m.equalsIgnoreCase("PLAY")) {
				
			} else {
				send(pClientIP, pClientPort, "-ERR Command not valid in this state");
			}
			break;
		case 2:
			// Spielphase
			if(m.equalsIgnoreCase("SEND")) {
				
			} else if(m.equalsIgnoreCase("INFO")) {
				int n = Integer.parseInt("0" + pMessage.substring(4,pMessage.length()).replaceAll(" ", ""));
				if(n != 0) {
					
				} else {
					
				}
			} else if(m.equalsIgnoreCase("EXIT")) {
				
			}  else {
				send(pClientIP, pClientPort, "-ERR Command not valid in this state");
			}
			break;
		case 3:
			// Zwischenphase(Angemeldet)
			if(m.equalsIgnoreCase("STAT")) {
				send(pClientIP, pClientPort, "+OK" + 
						'\n' + "times played: " + u.getTimesPlayed() + 
						'\n' + "win percentage: " + u.getWinPercentage() + 
						'\n' + "current streak: " + u.getCurrentStreak() + 
						'\n' + "max streak: " + u.getMaxStreak() + 
						'\n' + "won in 1 turn: " + u.getWonInTurnIndex(1) + 
						'\n' + "won in 2 turn: " + u.getWonInTurnIndex(2) + 
						'\n' + "won in 3 turn: " + u.getWonInTurnIndex(3) + 
						'\n' + "won in 4 turn: " + u.getWonInTurnIndex(4) + 
						'\n' + "won in 5 turn: " + u.getWonInTurnIndex(5) + 
						'\n' + "won in 6 turn: " + u.getWonInTurnIndex(6) + 
						'\n' + ".");
			} else if(m.equalsIgnoreCase("PLAY")) {
				
			} else {
				send(pClientIP, pClientPort, "-ERR Command not valid in this state");
			}
			break;

		}
	}
	
	public boolean isToday(int pDay) {
		return pDay == (int) (86400000.0 / Instant.now().toEpochMilli());
	}
}
