/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bingoserver.interactions;

import bingoserver.models.Room;
import bingoserver.models.User;
import bingoserver.repositories.RoomRepository;
import bingoserver.repositories.UserRepository;
import bingoserver.responses.AvailableRoomsResponse;
import bingoserver.responses.TooLessUsersInRoomResponse;
import bingoserver.responses.UsersInRoomChangedResponse;
import java.util.HashMap;
import java.util.List;
import org.json.simple.JSONObject;

/**
 *
 * @author guilherme
 */
public class UserDisconnected extends UserInteractor {

    @Override
    public void perform(JSONObject params) throws Exception {
        User user = getSessionUser();

        if (user == null) {
            return;
        }

        UserRepository userRepo = getRepositoryManager().getUserRepository();
        userRepo.removeUser(user);
        
        RoomRepository roomRepo = getRepositoryManager().getRoomRepository();
        Room room = roomRepo.findRoomByUser(user);

        if (room == null) {
            return;
        }

        getRepositoryManager().getUserCardRepository().removeUserFromRoom(user, room);
        List<User> users = roomRepo.usersInRoom(room);

        if (users.size() < Settings.MINIMUM_USERS_IN_ROOM) {
            roomRepo.removeRoom(room);
            
            getResponseManager().respondToUsers(new TooLessUsersInRoomResponse(), users);
            
            if (room.getState() == Room.RoomState.initialized) {
                HashMap<Room, List<User>> rooms = roomRepo.currentOpenRoomsWithUsers();
                getResponseManager().respondToUsers(new AvailableRoomsResponse(rooms), userRepo.usersWithoutRoom(roomRepo.getUsersInAnyRoom()));
            }
        } else {
            if (room.getState() == Room.RoomState.initialized) {
                getResponseManager().respondToUsers(new UsersInRoomChangedResponse(users), users);
            }
        }
    }
}

@@ -1357,57 +1357,96 @@
 if (
-users.size() %3C Settings.MINIMUM_USERS_IN_ROOM) %7B%0A
+room.getState() == Room.RoomState.initialized) %7B%0A            if (users.isEmpty()) %7B%0A    
     
@@ -1484,33 +1484,45 @@
 m);%0A            
-%0A
+%7D else %7B%0A    
             getR
@@ -1557,23 +1557,16 @@
 ers(new 
-TooLess
 UsersInR
@@ -1560,32 +1560,39 @@
 (new UsersInRoom
+Changed
 Response(), user
@@ -1584,16 +1584,21 @@
 esponse(
+users
 ), users
@@ -1604,32 +1604,33 @@
 s);%0A            
+%7D
 %0A            if 
@@ -1630,65 +1630,9 @@
     
-if (room.getState() == Room.RoomState.initialized) %7B%0A    
+%0A
     
@@ -1707,28 +1707,24 @@
 ithUsers();%0A
-    
             
@@ -1851,16 +1851,19 @@
 oom()));
+   
 %0A       
@@ -1867,26 +1867,27 @@
         
-%7D
 %0A
+  
         
 %7D else %7B
@@ -1874,32 +1874,43 @@
  %0A          
-%7D else %7B
+  return;%0A        %7D
 %0A           
@@ -1902,32 +1902,37 @@
   %7D%0A        
+%0A    
     if (
 room.getStat
@@ -1923,61 +1923,96 @@
 if (
-room.getState() == Room.RoomState.initialized) %7B%0A    
+users.size() %3C Settings.MINIMUM_USERS_IN_ROOM) %7B%0A            roomRepo.removeRoom(room);%0A
     
@@ -2051,32 +2051,39 @@
 pondToUsers(new 
+TooLess
 UsersInRoomChang
@@ -2069,39 +2069,32 @@
 oLessUsersInRoom
-Changed
 Response(users),
@@ -2078,37 +2078,32 @@
 sInRoomResponse(
-users
 ), users);%0A     
@@ -2093,38 +2093,24 @@
 (), users);%0A
-            %7D%0A
         %7D%0A  

Fix disconnect