local this = class("timelineControl")

function this:ctor()
  self._timelineState = require("ui.manager.hero.timeline.timelineState").new()
  self._timelinePrefabHandle = require("ui.manager.hero.timeline.heroTimelinePrefabHandle").new(false, true)
  self._timelinePrefabHandle._camera = C_CameraManager.mainCamera
  self._timelineAssetHandle = require("ui.manager.hero.timeline.timelineAssetHandle").new()
  self._timelineActorHandle = require("ui.manager.hero.timeline.timelineActorHandle").new(self._timelinePrefabHandle)
  self._timelineSceneHandle = require("ui.manager.hero.timeline.timelineSceneHandle").new(self._timelinePrefabHandle)
  self._timelineState.control = self
  self._timelinePrefabHandle.control = self
  self._timelineAssetHandle.control = self
  self._timelineName = nil
  self._loadingTimelineName = nil
  self._state = L_Const.heroTimelineState.None
  self.assetActive = true
  self._timelineAssetName = self._timelineState.timelineAssetName.None
  self._timelinePrefabHandle.timelinePlayedHandle = handler(self, self.timelinePlayedHandle)
  self._timelinePrefabHandle.timelineStopHandle = handler(self, self.timelineStopHandle)
  self._timelinePrefabHandle.timelineLoadedHandle = handler(self, self.timelineLoadedHandle)
  self._timelineAssetHandle.loadTimelineAssetFinishHandle = handler(self, self.loadTimelineAssetFinishHandle)
  self.onPlayTimelineAssetHandle = nil
end

function this:bindMainPlayer()
  do return end
  local state = self._timelinePrefabHandle:getTimelineState(self._timelinePrefabHandle._timelineName)
  local hero = self._timelineActorHandle:getCurHero()
  if state and hero then
    local bindingPath = state.mainBindingName
    if not string.isEmpty(bindingPath) then
      local paths = string.split(bindingPath, "/")
      local path = ""
      for i, v in ipairs(paths) do
        if i ~= #paths then
          if i == 1 then
            path = v
          else
            path = path .. "/" .. v
          end
        end
      end
      local parent = state.transform:Find(path, true)
      if parent == nil then
        return
      end
      local heroTrans = hero:getGameObject().transform
      local reset = heroTrans.parent ~= parent
      heroTrans.parent = parent
      if reset then
        L_Vector3.setLocalPos(heroTrans.transform, L_Vector3.getTemp())
        L_Vector3.setLocalRot(heroTrans.transform, L_Vector3.getTemp())
      end
    end
  end
end

function this:timelinePlayedHandle(timelineName)
  if self._timelineState == nil then
    return
  end
  if self._timelinePrefabHandle:getTimelineName() ~= timelineName then
    return
  end
  self:bindMainPlayer()
end

function this:timelineStopHandle(timelineName)
  if self._timelineState == nil then
    return
  end
  if self._timelinePrefabHandle:getTimelineName() ~= timelineName then
    return
  end
  local assetName = self._timelineState:getNextStateTransitionAssetName(self._timelineAssetName, self._state)
  self:tryChangNextTimelineState(assetName)
end

function this:timelineLoadedHandle(timelineName)
end

function this:loadTimelineAssetFinishHandle(timelineName, timelineAssetName, timelineAsset)
  if self._timelineState == nil then
    return
  end
  if self._timelinePrefabHandle:getTimelineName() ~= timelineName then
    return
  end
  self._timelinePrefabHandle:setAssetsActive(self.assetActive)
  self._timelineAssetName = timelineAssetName
  if self.onPlayTimelineAssetHandle then
    self.onPlayTimelineAssetHandle()
  end
  local hero = self._timelineActorHandle:getCurHero()
  self._timelinePrefabHandle:playTimelineAsset(timelineAsset, hero)
end

function this:tryChangNextTimelineState(timelineAssetName)
  self._timelineAssetHandle:loadTimelineAsset(self._timelinePrefabHandle:getTimelineName(), timelineAssetName)
end

function this:dispose()
  if self._timelineState then
    self._timelineState:dispose()
  end
  self._timelineState = nil
  if self._timelineActorHandle then
    self._timelineActorHandle:dispose()
  end
  self._timelineActorHandle = nil
  if self._timelineSceneHandle then
    self._timelineSceneHandle:dispose()
  end
  self._timelineSceneHandle = nil
  if self._timelinePrefabHandle then
    self._timelinePrefabHandle:dispose()
  end
  self._timelinePrefabHandle = nil
  if self._timelineAssetHandle then
    self._timelineAssetHandle:dispose()
  end
  self._timelineAssetHandle = nil
  self.onPlayTimelineAssetHandle = nil
end

function this:getIsStartAni()
  return self._timelineState:getIsStartAni(self._timelineAssetName)
end

function this:isTranstion()
  return self._timelineState:getIsTransition(self._timelineAssetName)
end

function this:getState()
  return self._timelineState:getState(self._timelineAssetName)
end

function this:setAssetsActive(isActive)
  self.assetActive = isActive
  self._timelinePrefabHandle:setAssetsActive(self.assetActive)
end

function this:getTimelineAssetName(state)
  local timelineAssetName
  if state == L_Const.heroTimelineState.Select then
    timelineAssetName = self._timelineState.timelineAssetName.UISelect_Start
  elseif state == L_Const.heroTimelineState.Detail then
    timelineAssetName = self._timelineState.timelineAssetName.UIDetail_Start
  elseif state == L_Const.heroTimelineState.Equip then
    timelineAssetName = self._timelineState.timelineAssetName.UIDetail_To_Equip
  elseif state == L_Const.heroTimelineState.Skill then
    timelineAssetName = self._timelineState.timelineAssetName.UIDetail_To_Skill
  end
  return timelineAssetName
end

function this:checkPlayTimelineState(state, showHero, lastShowHero)
  if showHero and self._state ~= L_Const.heroTimelineState.None then
    if self._state ~= state or self._timelineAssetHandle._curTimelineName ~= self._timelineName then
      self:playTimelineState(state, not self:isTranstion(), false)
    else
      if not lastShowHero then
        self:replayTimeline()
      else
      end
    end
  end
end

function this:playTimelineState(state, isForce, isInit)
  self._state = state
  isForce = true
  if isForce then
    local timelineAssetName
    if isInit then
      timelineAssetName = self:getTimelineAssetName(state)
    end
    if timelineAssetName == nil then
      timelineAssetName = self._timelineState:getNextStateTransitionAssetName(self._timelineAssetName, self._state)
    end
    if timelineAssetName == self._timelineAssetHandle._curTimelineAssetName then
    else
      self:tryChangNextTimelineState(timelineAssetName)
    end
  end
end

function this:replayTimeline()
  self._timelinePrefabHandle:replayTimeline()
end

function this:asyncLoadTimelineAssets(timelineName, timelineAssetName, sceneName, heroId, isFirstInit, onPreloadEnd, onAllLoaded)
  local onUnitPrefabLoaded = function()
    if self._loadingTimelineName ~= timelineName then
      return
    end
    self._timelineSceneHandle:loadScenePrefab(sceneName)
    self:loadTimelinePrefab(timelineName)
    self._loadingTimelineName = nil
    if onAllLoaded then
      onAllLoaded()
    end
  end
  local onPreLoadTimeAssetLoaded = function()
    if self._loadingTimelineName ~= timelineName then
      return
    end
    if onPreloadEnd then
      onPreloadEnd()
    end
    if heroId ~= self._timelineActorHandle._heroId then
      self._timelineAssetHandle._curTimelineAssetName = nil
    end
    self._timelineActorHandle:loadUnitPrefab(heroId, onUnitPrefabLoaded)
  end
  self._loadingTimelineName = timelineName
  if isFirstInit then
    self._timelineSceneHandle:loadScenePrefab(sceneName)
  end
  if timelineAssetName ~= nil then
    self._timelineAssetHandle:loadTimelineAsset(timelineName, timelineAssetName, onPreLoadTimeAssetLoaded)
  else
    onPreLoadTimeAssetLoaded()
  end
end

function this:loadTimelinePrefab(name)
  self._timelineName = name
  self._state = L_Const.heroTimelineState.Detail
  self._timelinePrefabHandle:loadTimelinePrefab(name)
  self:bindMainPlayer()
end

function this:getCamera()
  return self._timelinePrefabHandle._camera
end

function this:setCustomAvatarData(avatarData)
  self._timelineActorHandle.customAvatarData = avatarData
end

function this:getCurHeroPrefab()
  local hero = self._timelineActorHandle:getCurHero()
  return hero
end

return this
