package gltronic.tronio.business;

import java.io.IOException;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import gltronic.tronio.model.Game;

public class SocketUtils {
  public static void sendMessage(WebSocketSession session, String type, String message) {
    try {
      session.sendMessage(new TextMessage("{\"type\":\"" + type + "\",\"message\": \"" + message.toString() + "\" }"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void sendObject(WebSocketSession session, String type, String object) {
    try {
      session.sendMessage(new TextMessage("{\"type\":\"" + type + "\",\"message\": " + object + " }"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void broadcast(Game game, String type, String message) {
    game.getSessions().forEach((id, session) -> {
      sendObject(session, type, message);
    });
  }
}