package com.zombie.game.model;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

/** Serializable map data for storage or network transfer / 可序列化的地图数据，用于存储或网络传输 */
public class MapData {
    // Map unique identifier / 地图唯一标识
    private String id;
    // Map display name / 地图显示名称
    private String name;
    // Map width in grid units / 地图宽度（网格单位）
    private int width;
    // Map height in grid units / 地图高度（网格单位）
    private int height;
    // List of wall data maps / 墙壁数据映射列表
    private List<Map<String, Object>> walls;
    // List of player spawn coordinates / 玩家出生点坐标列表
    private List<Map<String, Integer>> playerSpawns;
    // List of zombie spawn coordinates / 僵尸出生点坐标列表
    private List<Map<String, Integer>> zombieSpawns;

    // Default constructor / 默认构造函数
    public MapData() {
        this.walls = new ArrayList<>();
        this.playerSpawns = new ArrayList<>();
        this.zombieSpawns = new ArrayList<>();
    }

    // Full constructor with all fields / 全字段构造函数
    public MapData(String id, String name, int width, int height,
                   List<Map<String, Object>> walls,
                   List<Map<String, Integer>> playerSpawns,
                   List<Map<String, Integer>> zombieSpawns) {
        this.id = id;
        this.name = name;
        this.width = width;
        this.height = height;
        this.walls = walls;
        this.playerSpawns = playerSpawns;
        this.zombieSpawns = zombieSpawns;
    }

    // Get map ID / 获取地图ID
    public String getId() { return id; }
    // Set map ID / 设置地图ID
    public void setId(String id) { this.id = id; }
    // Get map name / 获取地图名称
    public String getName() { return name; }
    // Set map name / 设置地图名称
    public void setName(String name) { this.name = name; }
    // Get map width / 获取地图宽度
    public int getWidth() { return width; }
    // Set map width / 设置地图宽度
    public void setWidth(int width) { this.width = width; }
    // Get map height / 获取地图高度
    public int getHeight() { return height; }
    // Set map height / 设置地图高度
    public void setHeight(int height) { this.height = height; }
    // Get walls list / 获取墙壁列表
    public List<Map<String, Object>> getWalls() { return walls; }
    // Set walls list / 设置墙壁列表
    public void setWalls(List<Map<String, Object>> walls) { this.walls = walls; }
    // Get player spawns / 获取玩家出生点
    public List<Map<String, Integer>> getPlayerSpawns() { return playerSpawns; }
    // Set player spawns / 设置玩家出生点
    public void setPlayerSpawns(List<Map<String, Integer>> playerSpawns) { this.playerSpawns = playerSpawns; }
    // Get zombie spawns / 获取僵尸出生点
    public List<Map<String, Integer>> getZombieSpawns() { return zombieSpawns; }
    // Set zombie spawns / 设置僵尸出生点
    public void setZombieSpawns(List<Map<String, Integer>> zombieSpawns) { this.zombieSpawns = zombieSpawns; }

    // Convert this map data to a 2D cell array / 将此地图数据转换为二维格子数组
    public int[][] toCells() {
        int[][] cells = new int[height][width];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                cells[y][x] = 0;
            }
        }
        // Mark wall cells / 标记墙壁格子
        for (Map<String, Object> wall : walls) {
            int x = (Integer) wall.get("x");
            int y = (Integer) wall.get("y");
            if (x >= 0 && x < width && y >= 0 && y < height) {
                cells[y][x] = 1;
            }
        }
        // Mark player spawn cells / 标记玩家出生点格子
        for (Map<String, Integer> spawn : playerSpawns) {
            int x = spawn.get("x");
            int y = spawn.get("y");
            if (x >= 0 && x < width && y >= 0 && y < height) {
                cells[y][x] = 2;
            }
        }
        // Mark zombie spawn cells / 标记僵尸出生点格子
        for (Map<String, Integer> spawn : zombieSpawns) {
            int x = spawn.get("x");
            int y = spawn.get("y");
            if (x >= 0 && x < width && y >= 0 && y < height) {
                cells[y][x] = 3;
            }
        }
        return cells;
    }

    // Create MapData from a GameMap instance / 从GameMap实例创建MapData
    public static MapData fromGameMap(String id, String name, GameMap map) {
        List<Map<String, Object>> walls = new ArrayList<>();
        List<Map<String, Integer>> playerSpawns = new ArrayList<>();
        List<Map<String, Integer>> zombieSpawns = new ArrayList<>();

        // Convert static walls from raw data
        for (int[] pos : map.getStaticWallData()) {
            Map<String, Object> wallData = new HashMap<>();
            wallData.put("x", pos[0]);
            wallData.put("y", pos[1]);
            wallData.put("type", "static");
            walls.add(wallData);
        }

        // Convert nut walls from raw data
        for (int[] pos : map.getNutWallData()) {
            Map<String, Object> wallData = new HashMap<>();
            wallData.put("x", pos[0]);
            wallData.put("y", pos[1]);
            wallData.put("type", "nut");
            walls.add(wallData);
        }

        // Convert player spawn points
        for (int[] spawn : map.getSpawnPoints()) {
            Map<String, Integer> sp = new HashMap<>();
            sp.put("x", spawn[0]);
            sp.put("y", spawn[1]);
            playerSpawns.add(sp);
        }

        return new MapData(id, name, map.getWidth(), map.getHeight(), walls, playerSpawns, zombieSpawns);
    }
}
