local this = class("pageSpiritronInfo", G_UIPageBase)
local _spTpl = L_GameTpl:getSpiritronTpl()

function this.bind()
  return {
    textName = "",
    textDesc = "",
    qualityImage = "",
    path_cg = "",
    module_video = {
      moduleName = "pages/video/moduleVideo"
    },
    storyNodeActive = false,
    lockTipActive = false,
    playBtnActive = false,
    lockTips = "",
    lineActive = true
  }
end

function this.methods()
  return {
    onClose = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_storyBtn = function(self)
      if math.isEmpty(self.storyId) then
        return
      end
      L_SpiritronStore:req_CSProtoLiZiStoryRecored(self.options.id)
      AzurWorld.StoryMgr:PlayStory(self.storyId)
    end,
    onClick_storyLock = function(self)
      if math.isEmpty(self.taskId) then
        return
      end
      local data_tip = {
        txtContent = L_WordsTpl:getValue("ui_spirit_story_task_jump"),
        confirmCallback = function()
          L_UI:backToRoot()
          L_UI:open("pageTask", {
            taskId = self.taskId
          })
        end
      }
      L_GameUtil.showCommonTip(data_tip)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.options = options
  local spId = options.id
  local conf = _spTpl:getTplById(spId)
  self.bind.textName = _spTpl:getName(conf)
  self.bind.textDesc = _spTpl:getDesc(conf)
  local qual = _spTpl:getRarity(conf)
  self.bind.qualityImage = "PageSpiritron/tex_herospiritron_zk_icon_" .. qual
  self.bind.path_cg = _spTpl:getCgPath(conf)
  self:setSpineActive(false)
  self.bind.storyNodeActive = not options.isPreview
  if not options.isPreview then
    L_ReddotManager:registerReddot(self.bindComponents.storyRedDot, string.format(L_ReddotManager.DotDef.SpiritSory, spId))
  end
end

function this:open(options)
  local spId = options.id
  local conf = _spTpl:getTplById(spId)
  self:setStoryNode(false)
  self._videoModule = self.modules.module_video
  self._videoModule:playVideo(_spTpl:getVideo(conf), function(duration)
    L_AudioUtil.playSound(_spTpl:getVideoSound(conf))
    L_TimerManager:newOrResetTimer(self, "onVideoEnd", function()
      if not self._isVideoEnd then
        L_AudioUtil.playSound("Stop_SFX_Spiritron_Intro")
        L_AudioUtil.playSound(_spTpl:getAnimationSound(conf))
        self.bindComponents.ani_pageSpiritronInfo:Play("anim_heroes_spiritroninfo_in_1")
        self._isVideoEnd = true
        self:setStoryNode(true)
      end
    end, duration - 1)
  end, function()
    if not self._isVideoEnd then
      L_AudioUtil.playSound("Stop_SFX_Spiritron_Intro")
      L_AudioUtil.playSound(_spTpl:getAnimationSound(conf))
      self.bindComponents.ani_pageSpiritronInfo:Play("anim_heroes_spiritroninfo_in")
      self._isVideoEnd = true
      self:setStoryNode(true)
    end
  end)
end

function this:setStoryNode(isShow)
  if self.options.isPreview then
    self.bind.storyNodeActive = false
    return
  end
  self.bind.storyNodeActive = isShow
  if not isShow then
    return
  end
  local spId = self.options.id
  local conf = _spTpl:getTplById(spId)
  self.bind.storyNodeActive = not math.isEmpty(_spTpl:getStory(conf))
  if not self.bind.storyNodeActive then
    return
  end
  local isComplete, results, lockIndex = L_ConditionManager:isComplete(_spTpl:getStoryLock(conf))
  self.bind.lockTipActive = not isComplete
  if not isComplete then
    self.bind.lockTips = L_ConditionManager:getDescs(_spTpl:getStoryLock(conf))[lockIndex]
  end
  self.bind.playBtnActive = not self.bind.lockTipActive
  self.taskId = _spTpl:getTask(conf)
  self.storyId = _spTpl:getStory(conf)
end

function this:show()
  self:setSpineActive(true)
end

function this:hide()
  self:setSpineActive(false)
end

function this:close(options)
  this.super.close(self, options)
  L_TimerManager:clearTimer(self)
  L_AudioUtil.playSound("Stop_SFX_Spiritron_Intro")
  L_AudioUtil.playSound("Stop_SFX_Spiritron_LP")
end

function this:setSpineActive(active)
  local spineGo = L_GameUtil.findTranByName(self.bindComponents.rect_prefabBox.gameObject, "SpineObject")
  local spineGoF = L_GameUtil.findTranByName(self.bindComponents.rect_prefabBox.gameObject, "SpineObject_f")
  local live2DGo = L_GameUtil.findTranByName(self.bindComponents.rect_prefabBox.gameObject, "Live2DObject")
  local live2DGoF = L_GameUtil.findTranByName(self.bindComponents.rect_prefabBox.gameObject, "Live2DObject_f")
  if spineGo then
    spineGo.gameObject:ActiveTrans(active)
  end
  if spineGoF then
    spineGoF.gameObject:ActiveTrans(active)
  end
  if live2DGo then
    live2DGo.gameObject:ActiveTrans(active)
  end
  if live2DGoF then
    live2DGoF.gameObject:ActiveTrans(active)
  end
end

return this
