local this = class("heroTimelinePrefabHandle", require("ui.manager.hero.timeline.timelinePrefabHandle"))
local TimelineSpliteHelper = CS.Gameplay.Tools.TimelineSplitTool.TimelineSpliteHelper

function this:ctor(createCamera, isUI)
  self._lastBindTimelineName = nil
  self._lastBindHeroId = nil
  self._lastTimelineAssetName = nil
  this.super.ctor(self, createCamera, isUI)
end

function this:dispose()
  self:clearLoadHeroSchedulerId()
  self._timelineStateMap = {}
  this.super.dispose(self)
end

function this:getTimelineStateComponent(prefab)
  return prefab:GetComponent(typeof(CS.Gameplay.Tools.TimelineSplitTool.TimelineHeroState))
end

function this:setAssetsActive(isActive)
  local state = self._timelineStateMap[self._timelineName]
  if state then
    local trans = state.transform
    for i = 0, trans.childCount - 1 do
      local child = trans:GetChild(i).gameObject
      if child.name ~= "[sequence]" then
        child:ActiveTrans(isActive)
      end
    end
  end
end

function this:getFilePath(timelineName)
  local path = timelineName .. "/pre_ui_timelineAsset.prefab"
  return path
end

function this:loadTimelinePrefab(timelineName)
  if self._timelineName == timelineName then
    return
  end
  if self._prefabMap[self._timelineName] then
    self._prefabMap[self._timelineName]:ActiveTrans(false)
  end
  local prefab
  if self._prefabMap[timelineName] then
    prefab = self._prefabMap[timelineName]
  else
    local prefabPath = self:getFilePath(timelineName)
    prefab = L_ResPool:syncGameObject(prefabPath)
    prefab.transform.parent = self._root.transform
  end
  L_Vector3.setPos(prefab.transform, L_Vector3.getTemp())
  L_Vector3.setRot(prefab.transform, L_Vector3.getTemp())
  self:loadTimelineFinish(timelineName, prefab)
end

function this:schedulerCheckHeroLoadStatus(actorInfo, onLoaded)
  self:clearLoadHeroSchedulerId()
  if onLoaded ~= nil then
    self._schedulerId = Timer.repeated(0.01, function()
      if self:checkHeroIsLoadFinish(actorInfo) then
        self:clearLoadHeroSchedulerId()
        onLoaded()
      end
    end)
  end
end

function this:clearLoadHeroSchedulerId()
  if self._schedulerId then
    Timer.remove(self._schedulerId)
    self._schedulerId = nil
  end
end

function this:checkHeroIsLoadFinish(actorInfo)
  local go = actorInfo:getGameObject()
  if go == nil then
    return false
  end
  local biologyData = L_CommonUtil.getModComponent(typeof(C_BiologyData), go)
  if biologyData == nil then
    return false
  end
  local haveValue = false
  for i, v in pairs(biologyData.partDict) do
    haveValue = true
    if not v.isLoaded then
      return false
    end
  end
  if haveValue then
    return true
  end
  return false
end

function this:playTimelineAsset(timelineAsset, hero)
  local state = self._timelineStateMap[self._timelineName]
  if state then
    if self._lastBindTimelineName ~= self._timelineName or self._lastBindHeroId ~= hero.heroId or self._lastTimelineAssetName ~= timelineAsset.name then
      state.director.playableAsset = timelineAsset
      for _, v in pairs(state.timelineBindDatas) do
        v:QuickBind()
      end
      local dynamicBinds = {}
      local actorInfo = state:GetActorPlayerInfoByActorPath(state.mainBindingName)
      if actorInfo then
        dynamicBinds[actorInfo.actorPath] = hero
      end
      self:bindMainPlayerByPrefab(self._timelineName, dynamicBinds)
      self:bindMainPlayer(self._timelineName, dynamicBinds)
      state.director:SetGenericBinding()
      local cam01 = state.transform:Find("[assets]/cam_G/cam_R/cam01")
      if cam01 then
        cam01:GetComponent(typeof(C_CinemachineVirtualCamera)).Priority = 1000
      end
      self._lastBindHeroId = hero.heroId
      self._lastBindTimelineName = self._timelineName
      self._lastTimelineAssetName = timelineAsset.name
    end
    if self._camera then
      state:SetCamera(self._camera)
    end
    if self:checkHeroIsLoadFinish(hero) then
      state.director:Play()
    else
      errorf("hero \230\178\161\230\156\137\229\138\160\232\189\189\229\174\140\230\136\144")
    end
  end
end

return this
