local Commands = {};
Commands.WarThunderSpawn = {};
local ZoneData = {}

-- 数据存储辅助函数
local function getOrCreatecheckWarThunderSpawnData()
    return ModData.getOrCreate("WarThunderSpawn")
end

-- 全局数据初始化
function ZoneData.OnInitGlobalModData()
    local SpawnData = ModData.getOrCreate("WarThunderSpawn")
    SpawnData.Location = SpawnData.Location or {}
    SpawnData.DefaultSpawn = SpawnData.DefaultSpawn or {}
end
Events.OnInitGlobalModData.Add(ZoneData.OnInitGlobalModData)

-- 载具生成核心函数
local function SpawnVehicle(VehicleType, x, y, z)
    local vehicle = addVehicle(VehicleType, x, y, z)
    if vehicle and SandboxVars.WT.RepairWhenSpawn then
        vehicle:repair()
    end
    return vehicle
end

-- 距离检测函数（三维欧几里得距离）
local function checkNearbySpawns(newX, newY, newZ)
    local SpawnData = getOrCreatecheckWarThunderSpawnData()
    local minDistance = 10 -- 最小间隔距离

    -- 检查战争载具生成点
    for vehicleType, data in pairs(SpawnData.Location) do
        for coordStr, status in pairs(data) do
            if coordStr:find("%d+_%d+_%d+") then -- 匹配坐标格式
                if status == "normal" or status == "burned" then
                    local x, y, z = coordStr:match("^(%d+)_(%d+)_(%d+)$")
                    x, y, z = tonumber(x), tonumber(y), tonumber(z)
                    local distance = math.sqrt((x - newX) ^ 2 + (y - newY) ^ 2 + (z - newZ) ^ 2)
                    if distance < minDistance then
                        return true
                    end
                end
            end
        end
    end

    -- 检查默认生成点
    for coordStr, _ in pairs(SpawnData.DefaultSpawn) do
        local x, y, z = coordStr:match("^(%d+)_(%d+)_(%d+)$")
        x, y, z = tonumber(x), tonumber(y), tonumber(z)
        local distance = math.sqrt((x - newX) ^ 2 + (y - newY) ^ 2 + (z - newZ) ^ 2)
        if distance < minDistance then
            return true
        end
    end

    return false
end

-- 战争载具生成逻辑
Commands.WarThunderSpawn.SpawnNew = function(player, args)
    local VehicleType = args.VehicleType
    local x, y, z = args.x, args.y, args.z

    -- 距离检测
    if checkNearbySpawns(x, y, z) then
        return
    end

    local SpawnData = getOrCreatecheckWarThunderSpawnData()
    local coordKey = ("%s_%s_%s"):format(x, y, z)
    SpawnData.Location[VehicleType] = SpawnData.Location[VehicleType] or {
        Count = 0,
        Times = 0
    }
    if not SpawnData.Location[VehicleType][coordKey] then
        local warType = GetPanzerTypeString(VehicleType) or GetHeliTypeString(VehicleType)
        local config = (PanzerList and PanzerList[warType]) or (HeliList and HeliList[warType]) or {}

        SpawnData.Location[VehicleType].Times = SpawnData.Location[VehicleType].Times + 1

        local shouldSpawn = (ZombRand(100) < args.chance)
        if config.MinSpawnCount then
            local remaining = config.MinSpawnCount - SpawnData.Location[VehicleType].Count
            if remaining > 0 and SpawnData.Location[VehicleType].Times >= (#config.SpawnLocation or 0) then
                shouldSpawn = true
            end
        end

        if shouldSpawn then

            local finalType = "Base." .. VehicleType
            if args.burned then
                SpawnData.Location[VehicleType][coordKey] = "burned"
                finalType = config.BurnedType or finalType
            else
                SpawnData.Location[VehicleType].Count = SpawnData.Location[VehicleType].Count + 1
                SpawnData.Location[VehicleType][coordKey] = "normal"
            end
            SpawnVehicle(finalType, x, y, z)
        elseif SandboxVars.WT.SpawnBurned then

            SpawnData.Location[VehicleType][coordKey] = "burned"
            SpawnVehicle(config.BurnedType, x, y, z)
        else
            SpawnData.Location[VehicleType][coordKey] = "blocked"
        end

        ModData.transmit("WarThunderSpawn")
    end
end

Commands.WarThunderSpawn.SpawnNewDefault = function(player, args)
    local x, y, z = args.x, args.y, args.z

    if checkNearbySpawns(x, y, z) then
        return
    end

    local SpawnData = getOrCreatecheckWarThunderSpawnData()
    local coordKey = ("%s_%s_%s"):format(x, y, z)

    if not SpawnData.DefaultSpawn[coordKey] then
        if ZombRand(100) < args.chance then
            SpawnVehicle("Base." .. args.VehicleType, x, y, z)
        end
        SpawnData.DefaultSpawn[coordKey] = args.VehicleType
        ModData.transmit("WarThunderSpawn")
    end
end

Commands.WarThunderSpawn.ClearTheSpawnDate = function()
    local SpawnData = getOrCreatecheckWarThunderSpawnData()
    SpawnData.Location = {}
    SpawnData.DefaultSpawn = {}
    ModData.transmit("WarThunderSpawn")
end

local function onClientCommand(module, command, player, args)
    if Commands[module] and Commands[module][command] then
        Commands[module][command](player, args)
    end
end
Events.OnClientCommand.Add(onClientCommand)
