local this = class("previewEntity", require(L_EcsConst.aliveType.default))
local CapsuleCollider = CS.UnityEngine.CapsuleCollider

function this:ctor(...)
  this.super.ctor(self, ...)
end

function this:initPreview(entityId, go, avatarData, animatorConfig)
  self.goModel = go
  self.gameObject = go
  self.entityId = entityId
  self.modelControl = self.goModel:GetComponent(typeof(C_ModelControl))
  self.unitId = self:getUnitId()
  self.biologyData = L_CommonUtil.getModComponent(typeof(C_BiologyData), self.gameObject)
  if self.biologyData then
    self.biologyData.lockLod = true
    self.biologyData:SetTheoryLodLv(0)
    self.biologyData:SetTheoryShadowLodLv(0)
  end
  self:setIk()
  local playableAnimator = L_CommonUtil.getModComponent(typeof(C_PlayableAnimator), self.gameObject)
  playableAnimator.cullingMode = C_AnimatorCullingMode.AlwaysAnimate
  playableAnimator.updateMode = C_PlayableAnimator.UpdateMode.LateUpdate
  self:setAnimatorController(playableAnimator, animatorConfig)
  self.playableAnimator = playableAnimator
  self:createCollider()
  if avatarData then
    self.avatarCtrl = AzurWorld.avatarMgr:SetAvatar(self.modelControl, avatarData, animatorConfig ~= nil and C_AnimatorControllerType.World or C_AnimatorControllerType.UIPreview)
  end
end

function this:getId()
  return self.entityId
end

function this:setAnimatorController(playableAnimator)
end

function this:createCollider()
  local childCollider = self.gameObject:GetComponentsInChildren(typeof(CapsuleCollider))
  for i = 0, childCollider.Length - 1 do
    if childCollider[i].transform ~= self.gameObject.transform then
      childCollider[i].enabled = false
    end
  end
  local unitTpl = L_GameTpl:getUnitTpl()
  local unit = unitTpl:getTplById(self.unitId)
  local colliderId = unitTpl:getColliderId(unit)
  local colliderTpl = L_GameTpl:getUnitColliderTpl()
  local colliderList = colliderTpl:getColliderList(colliderTpl:getTplById(colliderId))[1]
  local x = colliderList[1]
  local y = colliderList[2]
  local z = colliderList[3]
  local radius = colliderList[4]
  local height = colliderList[5]
  local collider = self.gameObject:PickComponent(typeof(CapsuleCollider))
  collider.center = L_Vector3.new(x, y, z)
  collider.radius = radius
  collider.height = height
end

function this:getUnitId()
  return 0
end

function this:setIk()
end

function this:getTarget()
  return self.target or nil
end

function this:onDestroy()
  this.super.onDestroy(self)
  if self.avatarCtrl then
    AzurWorld.avatarMgr:ReleaseAvatarController(self.avatarCtrl)
  end
end

return this
