local this = class("petTimelineController")
local unitTpl = L_GameTpl:getUnitTpl()
local petInfoTpl = L_GameTpl:getPetTpl()
local petRankTpl = L_GameTpl:getPetRankTpl()

function this:ctor()
  self._timelinePrefabHandle = require("ui.pages.pet.timeline.petTimelinePrefabHandle").new(true, true)
  self._petAvatarHandle = require("ui.pages.pet.timeline.petAvatarHandle").new(self._timelinePrefabHandle)
  self._timelinePrefabHandle.timelineStopHandle = handler(self, self.timelineStopHandle)
  
  function self._timelinePrefabHandle.getRealActorEntityHandle(remarkName, actorName)
    if actorName == "pet_1" then
      return self._petAvatarHandle:getAvatar(self.pet1)
    elseif actorName == "pet_2" then
      return self._petAvatarHandle:getAvatar(self.pet2)
    end
    return nil
  end
  
  self.loadErrorFunc = nil
  self.startPlayFunc = nil
  self.clipAudioEvents = nil
  self._isDispose = false
end

function this:dispose()
  if self._petAvatarHandle then
    self._petAvatarHandle:dispose()
  end
  self._petAvatarHandle = nil
  if self._timelinePrefabHandle then
    self._timelinePrefabHandle:dispose()
  end
  self._timelinePrefabHandle = nil
  self.loadErrorFunc = nil
  self.startPlayFunc = nil
  if self._startPlayCo then
    co.stop(self._startPlayCo)
    self._startPlayCo = nil
  end
  if self._audioCo then
    co.stop(self._audioCo)
    self._audioCo = nil
  end
  self._isDispose = true
  self.petData = nil
end

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

function this:timelineStopHandle(timelineName)
  if self._timelinePrefabHandle:getTimelineName() ~= timelineName then
    return
  end
  local state = self._timelinePrefabHandle:getTimelineState(self._timelinePrefabHandle._timelineName)
  if state == nil then
    return
  end
  state.director.time = state.loopTime + 0.01
  state.director:Evaluate()
  state.director:Play()
end

function this:getIsLoop()
  local state = self._timelinePrefabHandle:getTimelineState(self._timelinePrefabHandle._timelineName)
  if state == nil then
    return true
  end
  return state.director.time >= (state.loopTime or 0)
end

function this:playTimelineState(timelineAsset, petData, loopFrame)
  self._timelinePrefabHandle:loadTimelinePrefab(timelineAsset)
  local state = self._timelinePrefabHandle:getTimelineState(self._timelinePrefabHandle._timelineName)
  if state == nil then
    if self.loadErrorFunc then
      self.loadErrorFunc()
    end
    return
  end
  self.petData = petData
  self.pet1 = petData.pet1
  self.pet2 = petData.pet2
  self._petAvatarHandle:loadUnitPrefab(self.pet1, petData.pet1Rot, petData.pet1Scale, handler(self, self.onLoadCalback), petData.isSpecial, petData.isFlash)
  self._petAvatarHandle:loadUnitPrefab(self.pet2, petData.pet2Rot, petData.pet2Scale, handler(self, self.onLoadCalback), petData.isSpecial, petData.isFlash)
  self:onLoadCalback()
end

function this:onLoadCalback()
  if self._isDispose then
    return
  end
  local avatar1 = self._petAvatarHandle:getAvatar(self.pet1)
  local avatar2 = self._petAvatarHandle:getAvatar(self.pet2)
  if avatar1 == nil or avatar2 == nil then
    return
  end
  local tpl = petRankTpl:getTplById(self.pet1)
  local evoPre = petRankTpl:getEvolutionPre(tpl)
  local evoAfter = petRankTpl:getEvolutionAfterStart(tpl)
  local evoPreStrs = string.split(evoPre, "|")
  local evoAfterStrs = string.split(evoAfter, "|")
  printf("inori", evoPreStrs, evoAfterStrs)
  avatar1:setAnimId(evoPreStrs[1])
  avatar2:setAnimId(evoAfterStrs[1])
  
  function avatar1.onFixGetAniname(aniName)
    return evoPreStrs[2]
  end
  
  function avatar2.onFixGetAniname(aniName)
    if aniName == "EvolutionAfter_Start" then
      return evoAfterStrs[2]
    else
      return evoAfterStrs[3]
    end
  end
  
  self._timelinePrefabHandle:playTimelineAsset(self._timelinePrefabHandle._timelineName)
  if self.startPlayFunc then
    self._startPlayCo = co.start(self.startPlayFunc)
  end
  local audioEvents = self.clipAudioEvents
  if audioEvents ~= nil then
    local state = self._timelinePrefabHandle:getTimelineState(self._timelinePrefabHandle._timelineName)
    local list = state.timelineBindDatas
    if list.Count < 2 then
      return
    end
    local director = list[1].transform:GetComponent(typeof(CS.UnityEngine.Playables.PlayableDirector))
    local playableTimeline = director.playableAsset
    self._audioCo = co.start(function()
      if audioEvents.EvolutionPre then
        L_AudioUtil.playSound(audioEvents.EvolutionPre)
      end
      co.wait(state.loopTime or 5)
      if audioEvents.EvolutionPre then
        L_AudioUtil.playSound(audioEvents.EvolutionAfter_Loop)
      end
    end)
  end
end

return this
