local this = class("previewPetEntity", require(L_EcsConst.aliveType.preview))
local CapsuleCollider = CS.UnityEngine.CapsuleCollider

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

function this:initPreview(entityId, go, petData)
  self.goModel = go
  self.gameObject = go
  self.entityId = entityId
  self.modelControl = self.goModel:GetComponent(typeof(C_ModelControl))
  self.unitId = self:getUnitId(petData)
  local biologyData = L_CommonUtil.getModComponent(typeof(C_BiologyData), self.gameObject)
  if biologyData then
    biologyData.lockLod = true
    biologyData:SetTheoryLodLv(0)
    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
  playableAnimator.rootMotionController.selfApplyRootMotion = false
  self:setAnimatorController(playableAnimator)
  self.playableAnimator = playableAnimator
  self:createCollider()
end

function this:getUnitId(petData)
  local petGuid = self.entityId
  local tpl
  local unitId = 0
  local petInfoTpl = L_GameTpl:getPetTpl()
  petData = petData or L_PetStore:getPetItem(petGuid)
  if table.isEmpty(petData) then
    tpl = petInfoTpl:getTplById(self.entityId)
    if table.isEmpty(tpl) then
      return 0
    else
      unitId = petInfoTpl:getUnitID(tpl)
      return unitId
    end
  end
  tpl = petInfoTpl:getTplById(petData.id)
  unitId = petInfoTpl:getUnitID(tpl)
  return unitId
end

function this:setAnimatorController(playableAnimator)
  local unitTpl = L_GameTpl:getUnitTpl()
  local unit = unitTpl:getTplById(self.unitId)
  playableAnimator.defaultAnimatorController = C_LuaUtility.LoadPlayableController(unitTpl:getAnimatorConfig(unit, L_Const.avatarAnimatorConfigIndex.uiPreview)).config
end

function this:createCollider()
  this.super.createCollider(self)
  local formationTpl = L_GameTpl:getFormationTpl()
  local tpl = formationTpl:getTplById(self.unitId)
  local colliderCenter, colliderRadius, colliderHeight = formationTpl:getFormationCollider(tpl)
  local collider = self.gameObject:PickComponent(typeof(CapsuleCollider))
  collider.center = colliderCenter
  collider.radius = colliderRadius
  collider.height = colliderHeight
end

function this:setIk()
  local ik = self.gameObject:GetComponentInChildren(typeof(C_FullBodyBipedIK))
  if ik then
    ik.enabled = false
  end
end

return this
