local StageManager = BaseClass("StageManager")
local Localization = CS.GameEntry.Localization
local __init = function(self)
  self.lastOpenedIdleRewardStageId = nil
  self:UpdateHangUpMaxTime()
end
local __delete = function(self)
end
local InitData = function(self, message)
  if message.stageInfo ~= nil then
    local u = message.stageInfo
    local idleRewardStageId = tonumber(u.idleRewardStageId)
    local lastIdleRewardTimeStamp = tonumber(u.lastIdleRewardTimeStamp)
    if idleRewardStageId and lastIdleRewardTimeStamp then
      self:UpdateHangUpReward(lastIdleRewardTimeStamp, idleRewardStageId, nil)
    end
    local stageId = tonumber(u.stageId)
    local stageGroupId = tonumber(u.stageGroupId)
    if stageId and stageGroupId then
      local group = LocalController:instance():getLine(TableName.LW_StageGroup, stageGroupId)
      self:ProcessData(group.chapter_group, stageGroupId, stageId)
    else
      self:ProcessData(0)
    end
  else
    self:ProcessData(0)
  end
end
local UpdateHangUpMaxTime = function(self)
  local effect = LuaEntry.Effect:GetGameEffect(EffectDefine.LW_STAGE_ADDTIME) * 1000
  if effect == nil or effect < 0 then
    effect = 0
  end
  self.hangUpMaxTime = LuaEntry.DataConfig:TryGetNum("stage_idle_reward", "k2") * 1000 + effect
end
local UpdateData = function(self, stageGroupId, stageId, reward)
  local group = LocalController:instance():getLine(TableName.LW_StageGroup, stageGroupId)
  self:ProcessData(group.chapter_group, stageGroupId, stageId)
  self.reward = reward
  self.rewardStageId = stageId
end
local AlreadyPass = function(self, stageGroupId, stageId)
  if self.stageGroupMetaId == stageGroupId then
    return self.stageId >= tonumber(stageId)
  else
    return self.stageGroupMetaId > tonumber(stageGroupId)
  end
end
local ProcessData = function(self, curChapter, curStageGroup, curStage)
  if curChapter == 0 or self.chapterId == 0 then
    self.chapterId = 1
    self.stageGroupMetaId = 10001
    self.stageId = 1001
    self.lastOpenedIdleRewardStageId = 1001
    return
  end
  local group = LocalController:instance():getLine(TableName.LW_StageGroup, curStageGroup)
  local spl = group.stageGroup
  if not spl then
    spl = string.split(group.stage_group, "|")
    group.stageGroup = spl
  end
  local findNextStageGroup = false
  if tonumber(spl[#spl]) == curStage then
    findNextStageGroup = true
  else
    local find = false
    for _, stageId in ipairs(spl) do
      if find then
        self.stageId = tonumber(stageId)
        self.chapterId = curChapter
        self.stageGroupMetaId = curStageGroup
        return
      end
      if tonumber(stageId) == curStage then
        find = true
      end
    end
  end
  if findNextStageGroup then
    local find = false
    LocalController:instance():visitTable(TableName.LW_StageGroup, function(id, lineData)
      if lineData.id > curStageGroup then
        if find then
          if lineData.id < self.stageGroupMetaId then
            local spl = string.split(lineData.stage_group, "|")
            self.stageId = tonumber(spl[1])
            self.chapterId = lineData.chapter_group
            self.stageGroupMetaId = lineData.id
            find = true
          end
        else
          local spl = string.split(lineData.stage_group, "|")
          self.stageId = tonumber(spl[1])
          self.chapterId = lineData.chapter_group
          self.stageGroupMetaId = lineData.id
          find = true
        end
      end
    end)
  end
end
local UpdateHangUpReward = function(self, lastIdleRewardTimeStamp, idleRewardStageId, idleReward)
  self.lastIdleRewardTimeStamp = lastIdleRewardTimeStamp
  self.idleRewardStageId = idleRewardStageId
  self.idleReward = idleReward
  DataCenter.BuildBubbleManager:CheckShowByBubbleType(BuildBubbleType.BattleHangUp)
  EventManager:GetInstance():Broadcast(EventId.HangRewardRefreshed)
end
local StageProgress = function(self)
  local group = LocalController:instance():getLine(TableName.LW_StageGroup, self.stageGroupMetaId)
  local stage_list = string.split(group.stage_group, "|")
  for i, stage_id in ipairs(stage_list) do
    if tonumber(stage_id) == self.stageId then
      return i - 1 .. "/" .. #stage_list
    end
  end
  return "0/" .. #stage_list
end
StageManager.__init = __init
StageManager.__delete = __delete
StageManager.InitData = InitData
StageManager.ProcessData = ProcessData
StageManager.UpdateData = UpdateData
StageManager.UpdateHangUpReward = UpdateHangUpReward
StageManager.AlreadyPass = AlreadyPass
StageManager.StageProgress = StageProgress
StageManager.UpdateHangUpMaxTime = UpdateHangUpMaxTime
return StageManager
