package gltronic.voozik.business;

import java.util.ArrayList;
import java.util.Optional;

import org.springframework.web.socket.WebSocketSession;

import gltronic.voozik.model.VRoom;
import gltronic.voozik.model.VUser;

public class UserUtils {
  public static VUser getUser (WebSocketSession session, ArrayList<VUser> users) {
    Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();

    if (ouser.isEmpty()) {
      SocketUtils.sendMessage(session, "error", "need login");
      return null;
    }

    return ouser.get();
  }

  public static VRoom getRoom (WebSocketSession session, VUser user) {
    if (user.getRoom() == null) {
      SocketUtils.sendMessage(session, "error", "no room");
      return null;
    }
    else return user.getRoom();
  }

  public static boolean checkIsAdmin (WebSocketSession session, VUser user) {
    if (!user.getRoom().getAdmin().equals(user)) {
      SocketUtils.sendMessage(session, "error", "not admin");
      return false;
    } 
    else return true;
  }
}