local EpidemicZoneBlockRange = {}
local BlockGroup = {}
local BlockArea = {}

function EpidemicZoneBlockRange.SetBlockRangeData(idx)
  local data = BlockGroup[idx]
  if data ~= nil then
    BlockArea = data
    return data
  end
  data = require("Util.BattleFieldBlockRange.Epidemic" .. idx)
  BlockGroup[idx] = data
  BlockArea = data
  return data
end

function EpidemicZoneBlockRange.GetBlockRangeValue(pointId)
  local posV2 = SceneUtils.IndexToTilePos(toInt(pointId), ForceChangeScene.World)
  local data = BlockArea[posV2.y]
  local t = 0
  if data then
    for _, v in ipairs(data) do
      if posV2.x >= v.f and posV2.x <= v.t then
        t = v.i
        break
      end
    end
  end
  return t
end

function EpidemicZoneBlockRange.IsInBlockRange(pointId)
  local t = EpidemicZoneBlockRange.GetBlockRangeValue(pointId)
  if t == 0 then
    return false
  end
  if t == 1 then
    return true
  end
  local curSide = DataCenter.ActEpidemicZoneManager:GetCurSide()
  if curSide == 0 then
    return true
  end
  if t == 2 or t == 3 then
    return curSide ~= 1
  elseif t == 4 then
    return curSide ~= 2
  elseif t == 5 then
    return curSide ~= 3
  end
  return true
end

function EpidemicZoneBlockRange.GetNearSafePoint(pointId, targetI)
  local curSide = DataCenter.ActEpidemicZoneManager:GetCurSide()
  local posV2 = SceneUtils.IndexToTilePos(toInt(pointId), ForceChangeScene.World)
  local mAbs = math.abs
  local tX, tY, disX, disY, tmpX, tmpY, midX
  local flag = false
  for y, list in pairs(BlockArea) do
    tmpY = mAbs(y - posV2.y)
    if disY == nil or disY > tmpY then
      for _, info in pairs(list) do
        if targetI ~= nil then
          flag = info.i == targetI
        else
          flag = curSide == 1 and (info.i == 2 or info.i == 3) or curSide == 2 and info.i == 4 or curSide == 3 and info.i == 5
        end
        if flag then
          tY = y
          disY = tmpY
          midX = (info.t + info.f) / 2
          tmpX = mAbs(midX - posV2.x)
          if tX == nil or disX > tmpX then
            disX = tmpX
            tX = midX
          end
        end
      end
    end
  end
  return tX, tY
end

function EpidemicZoneBlockRange.TestBlockRange()
  if not CS.CommonUtils.IsDebug() then
    return
  end
  local ResourceManager = CS.GameEntry.Resource
  for x = 365, 635 do
    for y = 365, 635 do
      local pointId = SceneUtils.TileXYToIndex(x, y, ForceChangeScene.World)
      if EpidemicZoneBlockRange.IsInBlockRange(pointId) then
        do
          local effReq = ResourceManager:InstantiateAsync("Assets/_Art/Effect/prefab/scene/Build/XuanQu/VFX_RoadBlockRed.prefab")
          effReq:completed("+", function(req)
            if req.isError then
              return
            end
            req.gameObject:SetActive(true)
            req.gameObject.transform:SetParent(CS.SceneManager.World.DynamicObjNode)
            req.gameObject.transform.position = SceneUtils.TileIndexToWorld(pointId, ForceChangeScene.World)
          end)
        end
      end
    end
  end
end

return ConstClass("EpidemicZoneBlockRange", EpidemicZoneBlockRange)
