local module = class("cellHeroDramaItem", G_UIModuleBase)

function module.bind()
  return {
    plotImgAlpha = C_Color.white,
    unlockPlotName = "",
    unlockPlotChapter = "",
    lockedPlotName = "",
    lockedPlotChapter = "",
    plotImg = "",
    unlockTips = "",
    unlockNameActive = true,
    lockedNameAcitve = false,
    unlockNodeActive = false,
    taskBtnActive = false,
    playPlotBtnActive = false
  }
end

function module.methods()
  return {
    onClick_taskBtn = function(self)
      local new = not L_HeroStore:getIsCharDataRead(self.bind.heroConfigId, L_Const.heroData.drama, self.bind.id)
      if new then
        AzurWorld.heroMgr:ReqUpdateHeroCharData(self.bind.heroConfigId, L_Const.heroData.drama, {
          self.bind.id
        })
      end
      L_UI:open("pageTask", {
        taskId = self.bind.taskId
      })
    end,
    onClick_plotPlayBtn = function(self)
      if self.bind.plotId and self.bind.plotId ~= 0 then
        AzurWorld.StoryMgr:PlayStory(self.bind.plotId, function(isFinish, _)
          if isFinish then
            L_SystemBreakManager:jumpTo(L_SystemConst.enum.heroData, {
              heroId = self.bind.heroId,
              selectModuleParam = {
                openPanelEnum = L_Const.heroDataModuleType.Drama
              }
            })
          end
        end)
      end
    end
  }
end

function module:refresh()
  if not self.isBind then
    return
  end
  local charPlotTpl = L_GameTpl:getCharPlotTpl()
  local heroConfigId = self.bind.heroConfigId
  local index = self.bind.id
  if self.bind.isPreview then
    self.bindComponents.reddotNormal.gameObject:SetActive(false)
  else
    L_ReddotManager:registerReddot(self.bindComponents.reddotNormal, string.format(L_ReddotManager.DotDef.HeroInfo, heroConfigId .. "_drama_" .. index))
  end
  local charTpl = charPlotTpl:getTplById(index)
  self.bind.taskId = charPlotTpl:getTaskId(charTpl)
  self.bind.plotId = charPlotTpl:getPlotId(charTpl)
  self.bind.unlockPlotName = charPlotTpl:getTitle(charTpl)
  self.bind.unlockPlotChapter = charPlotTpl:getEnglishNum(charTpl)
  self.bind.lockedPlotName = charPlotTpl:getTitle(charTpl)
  self.bind.lockedPlotChapter = charPlotTpl:getEnglishNum(charTpl)
  self.bind.plotImg = charPlotTpl:getPic(charTpl)
  local taskFinished = self:checkTaskFinished()
  self.bind.taskBtnActive = not taskFinished and self.bind.isUnlock
  self.bind.playPlotBtnActive = taskFinished and self.bind.isUnlock
  self.bind.unlockNameActive = self.bind.isUnlock
  self.bind.lockedNameAcitve = not self.bind.isUnlock
  self.bind.unlockNodeActive = not self.bind.isUnlock
  self.bind.taskId = charPlotTpl:getTaskId(charTpl)
  if self.bind.isUnlock then
    self.bind.plotImgAlpha = C_Color.white
  else
    self.bind.plotImgAlpha = C_Color(1, 1, 1, 0.6)
  end
end

function module:checkTaskFinished()
  local taskFinish = true
  if self.bind.taskId and self.bind.taskId ~= 0 then
    taskFinish = L_TaskStore:checkTaskIsFinished(self.bind.taskId)
  end
  return taskFinish
end

return module
