package de.dhbwstuttgart.server.packet;

import com.fasterxml.jackson.annotation.JsonIgnore;
import de.dhbwstuttgart.server.SocketClient;
import de.dhbwstuttgart.server.SocketFuture;
import de.dhbwstuttgart.server.SocketServer;
import org.java_websocket.WebSocket;

/**
 * A packet to send simple error messages between the client and the server
 */
public class ErrorPacket implements IServerToClientPacket {

  /**
   * The error endpoint for messages from the server that should be logged out as errors and possibly abort the process
   */
  public String error;
  public boolean isFatal;

  @JsonIgnore
  public static ErrorPacket create(String error, boolean isFatal) {
    ErrorPacket packet = new ErrorPacket();
    packet.error = error;
    packet.isFatal = isFatal;
    return packet;
  }


  @JsonIgnore
  public void onHandle(WebSocket webSocket, SocketClient socketClient) {
    SocketClient.logger.exception(new RuntimeException(this.error));
    if (this.isFatal) {
      socketClient.close(1, "Received fatal error from server");
    }
  }
}
