local this = class("timelineActorHandle")

function this:ctor(_timelinePrefabHandle)
  self._timelinePrefabHandle = _timelinePrefabHandle
  self._unitPrefabMap = {}
  self._heroId = nil
end

function this:dispose()
  if self.avatarCtrl then
    AzurWorld.avatarMgr:ReleaseAvatarController(self.avatarCtrl)
    self.avatarCtrl = nil
  end
  for i, v in pairs(self._unitPrefabMap) do
    C_LuaUtility.DestroyObject(v.objHandle)
    self._unitPrefabMap[i] = nil
  end
  self._unitPrefabMap = nil
end

function this:getCurHero()
  return self._unitPrefabMap[self._heroId]
end

function this:loadUnitPrefab(heroId, onLoaded)
  local lastPrefabMap = self._unitPrefabMap[self._heroId]
  local afterLoaded = function()
    if lastPrefabMap and lastPrefabMap ~= self._unitPrefabMap[self._heroId] then
      lastPrefabMap:getGameObject():ActiveTrans(false)
    end
    if onLoaded ~= nil then
      onLoaded()
    end
  end
  self._heroId = heroId
  if self._unitPrefabMap[heroId] then
    afterLoaded()
  else
    local heroTpl = L_GameTpl:getHeroTpl()
    local hero = heroTpl:getTplById(heroId)
    local unitId = heroTpl:getUnitId(hero)
    local gameConstTpl = L_GameTpl:getGameConstTpl()
    local maleUnit = tonumber(gameConstTpl:getValue("AVATAR_UNIT_ID_MALE"))
    local femaleUnit = tonumber(gameConstTpl:getValue("AVATAR_UNIT_ID_FEMALE"))
    local isSelfRole = unitId == maleUnit or unitId == femaleUnit
    local avatarModelPath
    if isSelfRole then
      local avatarData = self:getAvatarData()
      avatarModelPath = L_GameUtil.getMainHeroPath(avatarData and avatarData.Sex)
    else
      local unitTpl = L_GameTpl:getUnitTpl()
      local tpl = unitTpl:getTplById(unitId)
      avatarModelPath = unitTpl:getModel(tpl, L_Const.avatarModelIndex.UnitModel)
    end
    local objHandle = C_LuaUtility.InstantiateSync(avatarModelPath)
    local obj = objHandle.gameObject
    obj.transform:SetParent(self._timelinePrefabHandle:getRoot().transform)
    L_Vector3.setLocalPos(obj.transform, L_Vector3.getTemp())
    L_Vector3.setLocalRot(obj.transform, L_Vector3.getTemp())
    local comps = obj:GetComponentsInChildren(typeof(CS.UnityEngine.Animations.RotationConstraint), true)
    for i = 0, comps.Length - 1 do
      comps[i].enabled = false
    end
    local worldAnimator = L_CommonUtil.getModComponent(typeof(C_PlayableAnimator), obj)
    local groundIk = L_CommonUtil.getModComponent(typeof(C_GrounderIK), obj)
    if groundIk then
      groundIk.enabled = false
      for i = 0, groundIk.legs.Length - 1 do
        groundIk.legs[i].enabled = false
      end
    end
    local biologyData = L_CommonUtil.getModComponent(typeof(C_BiologyData), obj)
    if biologyData then
      biologyData.lockLod = true
      biologyData.isSync = true
      local lodLv = -1
      local heroDetailLod = L_GameTpl:getGameConstTpl():getTplById("HERO_DETAIL_LOD")
      if heroDetailLod ~= nil then
        for i = 1, #heroDetailLod do
          if heroDetailLod[i][1] == heroId then
            lodLv = heroDetailLod[i][2]
            print("set lodLv", heroId, lodLv)
            break
          end
        end
      end
      biologyData:SetTheoryLodLv(lodLv)
      biologyData:SetTheoryShadowLodLv(0)
    end
    local rootObj = worldAnimator.gameObject
    Unity.GameObject.Destroy(worldAnimator)
    worldAnimator = rootObj:AddComponent(typeof(C_PlayableAnimator))
    worldAnimator.cullingMode = C_AnimatorCullingMode.AlwaysAnimate
    if isSelfRole then
      local avatarData = self:getAvatarData()
      if avatarData then
        local modelCtrl = obj:GetComponent(typeof(C_ModelControl))
        self.avatarCtrl = AzurWorld.avatarMgr:SetAvatar(modelCtrl, avatarData)
      end
    else
    end
    local actorInfo = require("ui.manager.hero.timeline.heroTimelineDynamicBindInfo").new()
    actorInfo:setObjHandle(objHandle)
    actorInfo:setGameObject(obj)
    actorInfo:setHeroId(heroId)
    L_Vector3.setLocalPos(obj.transform, L_Vector3.getTemp(10000, 10000, 10000))
    self._unitPrefabMap[heroId] = actorInfo
    local magicaAnimWeight = obj:GetComponentInChildren(typeof(CS.MagicaAnimWeight))
    if magicaAnimWeight then
      magicaAnimWeight:MagicaSwitchOff()
    end
    self._timelinePrefabHandle:schedulerCheckHeroLoadStatus(actorInfo, afterLoaded)
  end
  self._unitPrefabMap[self._heroId]:getGameObject():ActiveTrans(true)
end

function this:getAvatarData()
  if not self.customAvatarData then
    self.customAvatarData = L_ModStore:getPlayerModData()
  end
  return self.customAvatarData
end

return this
