package com.zombie.game.server;

import com.zombie.game.model.PlayerInfo;
import com.zombie.game.model.Room;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Room manager for tracking game rooms and player-room assignments / 房间管理器，用于追踪游戏房间和玩家房间分配
 */
public class RoomManager {
    // All active rooms mapped by room ID / 所有活跃房间，以房间ID映射
    private final Map<String, Room> rooms = new ConcurrentHashMap<>();
    // Player ID to room ID mapping / 玩家ID到房间ID的映射
    private final Map<String, String> playerToRoom = new ConcurrentHashMap<>();

    // Add a new room to the manager / 向管理器添加新房间
    public void addRoom(String roomId, Room room) {
        rooms.put(roomId, room);
        // Map existing players in room to room ID / 将房间中现有玩家映射到房间ID
        for (PlayerInfo player : room.getPlayers()) {
            playerToRoom.put(player.getId(), roomId);
        }
    }

    // Add a player to a room / 将玩家添加到房间
    public boolean joinRoom(String roomId, String playerId, String playerName) {
        Room room = rooms.get(roomId);
        if (room == null) return false;
        // Reject if room is full / 房间已满则拒绝
        if (room.getPlayerCount() >= 4) return false;

        boolean added = room.addPlayer(playerId, playerName);
        if (added) {
            playerToRoom.put(playerId, roomId);
        }
        return added;
    }

    // Remove a player from their room / 从房间中移除玩家
    public Room leaveRoom(String playerId) {
        String roomId = playerToRoom.remove(playerId);
        if (roomId == null) return null;

        Room room = rooms.get(roomId);
        if (room == null) return null;

        room.removePlayer(playerId);
        // Delete room if empty / 如果房间为空则删除
        if (room.getPlayerCount() == 0) {
            rooms.remove(roomId);
            return null;
        }
        return room;
    }

    // Get room by player ID / 通过玩家ID获取房间
    public Room getRoomByPlayerId(String playerId) {
        String roomId = playerToRoom.get(playerId);
        if (roomId == null) return null;
        return rooms.get(roomId);
    }

    // Get room by room ID / 通过房间ID获取房间
    public Room getRoom(String roomId) {
        return rooms.get(roomId);
    }

    // Get all rooms / 获取所有房间
    public Collection<Room> getAllRooms() {
        return rooms.values();
    }

    // Get rooms that haven't started a game yet / 获取尚未开始游戏的房间
    public List<Room> getAvailableRooms() {
        List<Room> available = new ArrayList<>();
        for (Room room : rooms.values()) {
            if (!room.isGameStarted()) {
                available.add(room);
            }
        }
        return available;
    }

    // Remove a room and all player mappings / 移除房间及其所有玩家映射
    public void removeRoom(String roomId) {
        Room room = rooms.remove(roomId);
        if (room != null) {
            for (PlayerInfo player : room.getPlayers()) {
                playerToRoom.remove(player.getId());
            }
        }
    }

    // Check if a player is in any room / 检查玩家是否在任意房间中
    public boolean isPlayerInRoom(String playerId) {
        return playerToRoom.containsKey(playerId);
    }
}
