package ch.unibas.dmi.dbis.cs108.multiplayer.client;

import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class JClientProtocolParser {
  public static final Logger LOGGER = LogManager.getLogger();
  public static final BudaLogConfig l = new BudaLogConfig(LOGGER);

  /**
   * Got a request from the Server to vote which human to infect.
   */
  public static final String GVOTR = "GVOTR";
  /**
   * Got a request from Server to vote which player to throw of the train
   */
  public static final String HVOTR = "HVOTR";

  /**
   * Used by the client to parse an incoming protocol message.
   *
   * @param msg the encoded message that needs to be parsed
   * @param c   this Client(required so this method can access the Client's methods)
   */
  public static void parse(String msg, Client c) {
    //LOGGER.debug("got message: " + msg + ".");
    String header = "";             //"header" is the first 5 characters, i.e. the protocol part
    try {
      header = msg.substring(0, 5);
    } catch (IndexOutOfBoundsException e) {
      System.out.println("Received unknown command");
      e.printStackTrace();
    }
    switch (header) {
      case "SPING":
        //sends a pingback to the server
        c.sendMsgToServer("PINGB");
        break;
      case "PINGB":
        //registers pingback from server
        c.clientPinger.setGotPingBack(true);
        break;
      case "CHATM":
        /* prints out incoming chat messages / announcements into the user's console.
         * any string that follows CHATM$ is printed as is, so the message that follows
         * already has to be formatted the way it should be shown to the client.
         */
        System.out.println(msg.substring(6));
        break;
      case "QUITC":
        c.disconnectFromServer();
        break;
      case GVOTR:
        System.out.println("Ghost received Vote request");
        //TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
        break;
      case HVOTR:
        System.out.println("Human received Vote request");
        //TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
        break;
      default:
        System.out.println("Received unknown command");
    }
  }
}
