local DragonBlockRange = {}
local BlockGroup = {}
local BlockArea = {}

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

function DragonBlockRange.IsInBlockRange(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
  if t == 0 then
    return false
  end
  if t == 1 then
    return true
  end
  local BattleInfo = DataCenter.ActDragonManager:GetCurBattleInfo()
  if BattleInfo == nil then
    return false
  end
  if BattleInfo.selfSide == 0 then
    return t == 2
  else
    return t == 3
  end
  return false
end

function DragonBlockRange.TestBlockRange()
  if not CS.CommonUtils.IsDebug() then
    return
  end
  local ResourceManager = CS.GameEntry.Resource
  for x = 390, 610 do
    for y = 390, 610 do
      local pointId = SceneUtils.TileXYToIndex(x, y, ForceChangeScene.World)
      if DragonBlockRange.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("DragonBlockRange", DragonBlockRange)
