local PveUnitViewUtil = {}
local UnitViewParamArray, UnitViewParamArrayAccess, HpBarParamArray, HpBarParamArrayAccess, UnitViewFacade
local loadedCount = 0
local VIEW_INVALID_HANDLE = -1

function PveUnitViewUtil.InitView()
  if UnitViewParamArray == nil then
    UnitViewParamArray = LuaCSharpArray.New(512)
    UnitViewParamArrayAccess = UnitViewParamArray:GetCSharpAccess()
    HpBarParamArray = LuaCSharpArray.New(8)
    HpBarParamArrayAccess = HpBarParamArray:GetCSharpAccess()
    UnitViewFacade = CS.PVEBattleLogic.Unit.UnitViewFacade
    UnitViewFacade.InitUnitView(UnitViewParamArrayAccess, HpBarParamArrayAccess)
  end
end

function PveUnitViewUtil.UnInitView()
  if UnitViewParamArray ~= nil then
    UnitViewParamArray:DestroyCSharpAccess()
    UnitViewParamArray = nil
    UnitViewParamArrayAccess = nil
    HpBarParamArray:DestroyCSharpAccess()
    HpBarParamArray = nil
    HpBarParamArrayAccess = nil
    UnitViewFacade.UnInitUnitView()
    UnitViewFacade.ClearAll()
    UnitViewFacade = nil
  end
end

function PveUnitViewUtil.CreateUnitView(guid, path, parent, scale, posX, posY, posZ, eulerX, eulerY, eulerZ, layer)
  UnitViewParamArray[1] = guid
  UnitViewParamArray[2] = scale
  UnitViewParamArray[3] = posX
  UnitViewParamArray[4] = posY
  UnitViewParamArray[5] = posZ
  UnitViewParamArray[6] = eulerX
  UnitViewParamArray[7] = eulerY
  UnitViewParamArray[8] = eulerZ
  UnitViewParamArray[9] = layer
  local handle, loaded = UnitViewFacade.CreateUnitView(path, parent)
  return handle, loaded
end

function PveUnitViewUtil.CheckLoadedCount()
  UnitViewFacade.CheckLoaded()
  loadedCount = UnitViewParamArray[1]
  return loadedCount
end

function PveUnitViewUtil.GetLoadedObjId(index)
  if index <= loadedCount then
    return UnitViewParamArray[index * 2], UnitViewParamArray[index * 2 + 1]
  end
  return 0, VIEW_INVALID_HANDLE
end

function PveUnitViewUtil.Preload(path, count)
  UnitViewFacade.PreloadUnitView(path, count)
end

function PveUnitViewUtil.InitTxtNumberText(handle, number)
  return UnitViewFacade.InitTxtNumberText(handle, number)
end

function PveUnitViewUtil.ShowHpTweenScale(handle)
  UnitViewFacade.ShowHpTweenScale(handle)
end

function PveUnitViewUtil.InitHpText(handle, number)
  return UnitViewFacade.InitHpText(handle, number)
end

function PveUnitViewUtil.SetNumberHpText(handle, number)
  UnitViewFacade.SetNumberHpText(handle, number)
end

function PveUnitViewUtil.SetNumberText(handle, number)
  UnitViewFacade.SetNumberText(handle, number)
end

function PveUnitViewUtil.NumberHpTextActive(handle, active)
  UnitViewFacade.NumberHpTextActive(handle, active)
end

function PveUnitViewUtil.AddAgent(rvoManager, viewHandle, speed, radius)
  HpBarParamArray[1] = viewHandle
  HpBarParamArray[2] = speed
  HpBarParamArray[3] = radius
  return UnitViewFacade.AddAgent(rvoManager)
end

function PveUnitViewUtil.CreateSelfHpBarWithHandle(parentViewHandle, height, offsetX, curHp, maxHp, shieldValue)
  height = height or 0.0
  offsetX = offsetX or 0.0
  curHp = curHp or 0
  maxHp = maxHp or 1
  shieldValue = shieldValue or 0
  HpBarParamArray[1] = height
  HpBarParamArray[2] = offsetX
  HpBarParamArray[3] = curHp
  HpBarParamArray[4] = maxHp
  HpBarParamArray[5] = shieldValue
  HpBarParamArray[6] = parentViewHandle
  return UnitViewFacade.CreateSelfHpBarWithHandle()
end

function PveUnitViewUtil.CreateEnemyHpBarWithHandle(parentViewHandle, height, offsetX, curHp, maxHp, shieldValue)
  height = height or 0.0
  offsetX = offsetX or 0.0
  curHp = curHp or 0
  maxHp = maxHp or 1
  shieldValue = shieldValue or 0
  HpBarParamArray[1] = height
  HpBarParamArray[2] = offsetX
  HpBarParamArray[3] = curHp
  HpBarParamArray[4] = maxHp
  HpBarParamArray[5] = shieldValue
  HpBarParamArray[6] = parentViewHandle
  return UnitViewFacade.CreateEnemyHpBarWithHandle()
end

function PveUnitViewUtil.SetHpBar(viewHandle, curHp, maxHp, shieldValue)
  curHp = curHp or 0
  maxHp = maxHp or 1
  shieldValue = shieldValue or 0
  HpBarParamArray[1] = viewHandle
  HpBarParamArray[2] = curHp
  HpBarParamArray[3] = maxHp
  HpBarParamArray[4] = shieldValue
  UnitViewFacade.SetHpBar()
end

function PveUnitViewUtil.SetHpBarOffsetX(viewHandle, offsetX)
  HpBarParamArray[1] = viewHandle
  HpBarParamArray[2] = offsetX
  UnitViewFacade.SetHpBarOffsetX(viewHandle, offsetX)
end

function PveUnitViewUtil.EnableHpBar(viewHandle, enable)
  HpBarParamArray[1] = viewHandle
  HpBarParamArray[2] = enable and 1 or 0
  UnitViewFacade.EnableHpBar()
end

function PveUnitViewUtil.ReplaceHpBarTargetWithHandle(viewHandle, targetViewHandle)
  HpBarParamArray[1] = viewHandle
  HpBarParamArray[2] = targetViewHandle
  UnitViewFacade.ReplaceHpBarTargetWithHandle()
end

function PveUnitViewUtil.PreloadHpBar(count)
  UnitViewFacade.PreloadHpBar(count)
end

function PveUnitViewUtil.UpdateHpBar()
  UnitViewFacade.UpdateHpBar()
end

function PveUnitViewUtil.DestroyHpBar(viewHandle)
  return UnitViewFacade.DestroyHpBar(viewHandle)
end

function PveUnitViewUtil.InitGlassAndWaterRender(viewHandle, glassRenderName, waterRenderName)
  UnitViewFacade.InitGlassAndWaterRender(viewHandle, glassRenderName, waterRenderName)
end

function PveUnitViewUtil.SetGlassCrackEffect(viewHandle, crackValue)
  UnitViewFacade.SetGlassCrackEffect(viewHandle, crackValue)
end

function PveUnitViewUtil.SetWaveIntensityEffect(viewHandle, heightValue)
  UnitViewFacade.SetWaveIntensityEffect(viewHandle, heightValue)
end

return ConstClass("PveUnitViewUtil", PveUnitViewUtil)
