local this = class("petHouseTimelineSceneHandle")
local petEggTpl = L_GameTpl:getPetEggTpl()
local gameTpl = L_GameTpl:getGameConstTpl()

function this:ctor(_timelinePrefabHandle)
  self._timelinePrefabHandle = _timelinePrefabHandle
  self._scenePrefabMap = {}
  self._sceneName = nil
  self.sceneNpcEntity = {}
  self.updateNpcTimer = Timer.repeated(0.1, handler(self, self.updateNpc), self)
end

function this:dispose()
  for i, v in pairs(self._scenePrefabMap) do
    C_GameObject.DestroyImmediate(v)
    self._scenePrefabMap[i] = nil
  end
  self._scenePrefabMap = nil
  self._sceneName = nil
  self.sceneNpcEntity = nil
  Timer.remove(self.updateNpcTimer)
  self.updateNpcTimer = nil
end

function this:loadScenePrefab(sceneName)
  if self._sceneName == sceneName then
    return
  end
  local prefab
  if self._scenePrefabMap[self._sceneName] then
    self._scenePrefabMap[self._sceneName]:ActiveTrans(false)
  end
  self._sceneName = sceneName
  if self._scenePrefabMap[sceneName] then
    prefab = self._scenePrefabMap[sceneName]
  else
    prefab = L_ResPool:syncGameObject(sceneName)
    prefab.transform.parent = self._timelinePrefabHandle:getRoot().transform
    L_Vector3.setPos(prefab.transform, L_Vector3.getTemp())
    L_Vector3.setRot(prefab.transform, L_Vector3.getTemp())
    self._scenePrefabMap[sceneName] = prefab
  end
  prefab:ActiveTrans(true)
end

function this:loadSceneEggPrefab(list)
  for i, v in ipairs(list) do
    local prefab
    local prefabPath = v.modPath
    local name = "egg_" .. v.index
    if self._scenePrefabMap[name] then
      self._scenePrefabMap[name]:ActiveTrans(false)
    end
    if self._scenePrefabMap[name] then
      prefab = self._scenePrefabMap[name]
    elseif not string.isEmpty(prefabPath) then
      prefab = L_ResPool:syncGameObject(prefabPath)
      prefab.transform.parent = self._timelinePrefabHandle:getRoot().transform
      L_Vector3.setPos(prefab.transform, L_Vector3.new(v.pos[1], v.pos[2], v.pos[3]))
      L_Vector3.setRot(prefab.transform, L_Vector3.new(0, math.random(0, 360), 0))
      local base_size = gameTpl:getTplById("PET_EGGHOUSE_EGGSCALE") / 10000
      local size = v.eggScale * base_size
      L_Vector3.setSize(prefab.transform, size)
      local animator = L_CommonUtil.getModComponent(typeof(C_PlayableAnimator), prefab)
      animator.cullingMode = C_AnimatorCullingMode.AlwaysAnimate
      animator.updateMode = C_PlayableAnimator.UpdateMode.LateUpdate
      animator.defaultAnimatorController = C_LuaUtility.LoadPlayableController(v.aniConfig).config
      animator:Play("Idle")
      self._scenePrefabMap[name] = prefab
    end
    if prefab then
      if v.eggGuid ~= 0 then
        prefab:ActiveTrans(true)
      else
        C_GameObject.DestroyImmediate(prefab)
        self._scenePrefabMap[name] = nil
      end
    end
  end
end

function this:getHatchEggPrefab()
  local name = "egg_hatch"
  return self._scenePrefabMap[name]
end

function this:loadHatchEggPrefab(eggInfo, disable)
  local prefabPath = eggInfo.modPath
  local name = "egg_hatch"
  return self:loadUnitPrefab(name, prefabPath, disable)
end

function this:loadUnitPrefab(name, prefabPath, disable)
  local prefab
  if self._scenePrefabMap[name] and disable then
    self._scenePrefabMap[name]:ActiveTrans(false)
    C_GameObject.DestroyImmediate(self._scenePrefabMap[name])
    self._scenePrefabMap[name] = nil
    return nil
  end
  if self._scenePrefabMap[name] then
    prefab = self._scenePrefabMap[name]
  elseif not string.isEmpty(prefabPath) then
    prefab = L_ResPool:syncGameObject(prefabPath)
    prefab.transform.parent = self._timelinePrefabHandle:getRoot().transform
    L_Vector3.setPos(prefab.transform, L_Vector3.getTemp())
    L_Vector3.setRot(prefab.transform, L_Vector3.getTemp())
    L_Vector3.setSize(prefab.transform, 1)
    self._scenePrefabMap[name] = prefab
  end
  if prefab then
    prefab:ActiveTrans(true)
  end
  return prefab
end

function this:getUnitPrefab(name)
  return self._scenePrefabMap[name]
end

function this:loadSceneNpcPrefab(unitId, pos)
  local unitTpl = L_GameTpl:getUnitTpl()
  local tpl = unitTpl:getTplById(unitId)
  local path = L_GameUtil.getAvatarModelPathByUnitId(unitId, 1)
  local prefab
  if self._scenePrefabMap[path] then
    prefab = self._scenePrefabMap[path]
  else
    prefab = L_ResPool:syncGameObject(path)
    prefab.transform.parent = self._timelinePrefabHandle:getRoot().transform
    local position = L_Vector3.zero
    if pos == nil then
      position = L_Vector3.new(4308.8, 35.95, 4380.8)
    end
    L_Vector3.setPos(prefab.transform, L_Vector3.new(position.x, position.y, position.z))
    L_Vector3.setRot(prefab.transform, L_Vector3.new(0, 180, 0))
    self._scenePrefabMap[path] = prefab
  end
  local animator = L_CommonUtil.getModComponent(typeof(C_PlayableAnimator), prefab)
  animator.cullingMode = C_AnimatorCullingMode.AlwaysAnimate
  animator.updateMode = C_PlayableAnimator.UpdateMode.LateUpdate
  animator.defaultAnimatorController = C_LuaUtility.LoadPlayableController(unitTpl:getAnimatorConfig(tpl, L_Const.avatarAnimatorConfigIndex.uiPreview)).config
  animator:Play("Idle")
  local biologyData = L_CommonUtil.getModComponent(typeof(C_BiologyData), prefab)
  if biologyData then
    biologyData.lockLod = true
    biologyData.theoryLodLv = 0
  end
  local animator = self.sceneNpcEntity[unitId]:getAnimatorComponent(true)
  animator:initialize()
end

function this:sceneNpcLoadAiBehavior(unitId, json)
  self.sceneNpcEntity[unitId]:loadAIBehavior(json)
end

function this:sceneNpcLoadAiBehaviorByState(unitId, state)
  if state == L_Const.petTimelineState.Main then
    self.sceneNpcEntity[unitId]:loadAIBehavior("behavior_petmanagermain")
  elseif state == L_Const.petTimelineState.Shop then
    self.sceneNpcEntity[unitId]:loadAIBehavior("behavior_petmanagershop")
  else
    self.sceneNpcEntity[unitId]:loadAIBehavior("behavior_petmanagermain")
  end
end

function this:setAssetsActive(isActive)
  self._timelinePrefabHandle:getRoot():ActiveTrans(isActive)
end

return this
