package com.zombie.game.model;

import lombok.Getter;
import java.util.*;

import static com.zombie.game.model.Constants.*;

/** Game room holding players and game state / 游戏房间，管理玩家和游戏状态 */
@Getter
public class Room {
    // Room unique identifier / 房间唯一标识
    private String id;
    // Host player ID / 房主玩家ID
    private String hostId;
    // Map of playerId to PlayerInfo / 玩家ID到玩家信息的映射
    private Map<String, PlayerInfo> players;
    // Whether the game has started / 游戏是否已开始
    private boolean gameStarted;
    // Maximum number of players allowed / 最大玩家数量
    private final int maxPlayers = 4;

    // Set game started flag / 设置游戏开始标志
    public void setGameStarted(boolean started) { this.gameStarted = started; }

    // Create a new room with the host player / 创建新房间并添加房主
    public Room(String id, String hostId, String hostName) {
        this.id = id;
        this.hostId = hostId;
        this.players = new LinkedHashMap<>();
        players.put(hostId, new PlayerInfo(hostId, hostName));
    }

    // Add a player to the room / 添加玩家到房间
    public boolean addPlayer(String playerId, String playerName) {
        if (players.size() >= maxPlayers) return false;
        if (players.containsKey(playerId)) return false;
        players.put(playerId, new PlayerInfo(playerId, playerName));
        return true;
    }

    // Remove a player from the room / 从房间移除玩家
    public void removePlayer(String playerId) {
        players.remove(playerId);
    }

    // Get player by ID / 根据ID获取玩家
    public PlayerInfo getPlayer(String playerId) {
        return players.get(playerId);
    }

    // Get all players / 获取所有玩家
    public Collection<PlayerInfo> getPlayers() {
        return players.values();
    }

    // Get current player count / 获取当前玩家数量
    public int getPlayerCount() {
        return players.size();
    }

    // Check if a player is the host / 检查玩家是否为房主
    public boolean isHost(String playerId) {
        return hostId.equals(playerId);
    }

    // Check if all non-host players are ready / 检查所有非房主玩家是否已准备
    public boolean allReady() {
        for (PlayerInfo p : players.values()) {
            if (!p.getId().equals(hostId) && !p.isReady()) return false;
        }
        return true;
    }

    // Convert room state to a map for a specific player / 将房间状态转换为指定玩家的Map
    public Map<String, Object> toStateMap(String playerId) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("roomId", id);
        map.put("hostId", hostId);
        map.put("isHost", hostId.equals(playerId));
        map.put("playerId", playerId);

        // Build player list with index / 构建带索引的玩家列表
        List<Map<String, Object>> playerList = new ArrayList<>();
        int index = 0;
        for (PlayerInfo p : players.values()) {
            Map<String, Object> pm = new LinkedHashMap<>();
            pm.put("id", p.getId());
            pm.put("name", p.getName());
            pm.put("ready", p.isReady());
            pm.put("index", index++);
            playerList.add(pm);
        }
        map.put("players", playerList);
        return map;
    }

    // Convert to a lightweight room list entry / 转换为轻量级房间列表条目
    public Map<String, Object> toRoomListMap() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id", id);
        PlayerInfo host = players.get(hostId);
        map.put("hostName", host != null ? host.getName() : "Unknown");
        map.put("playerCount", players.size());
        return map;
    }
}
