local CumulativeRechargeManager = BaseClass("CumulativeRechargeManager")
local RechargeStageInfo = require("DataCenter.CumulativeRechargeManager.RechargeStageInfo")
local __init = function(self)
  self.rechargeStage = {}
  self.tempStage = 0
  self.isFirst = true
  self.cacheLastReachStage = {}
end
local __delete = function(self)
  self:RemoveTimer()
  self.recharge = nil
  self.isFirst = nil
  self.cacheLastReachStage = nil
end
local GetReachStage = function(self, rechargeId, score)
  if self.rechargeStage[rechargeId] then
    local rechargeStage = self.rechargeStage[rechargeId]
    for i = #rechargeStage.stageInfo, 1, -1 do
      if rechargeStage.stageInfo[i].state == 0 and score >= rechargeStage.stageInfo[i].needScore then
        return i
      end
    end
  end
  return 0
end
local InitDat = function(self, message)
  self.rechargeStage = {}
  if message.rechargeArr then
    local recharge = message.rechargeArr
    for i = 1, #recharge do
      self.rechargeStage[recharge[i].rechargeId] = {}
      self.rechargeStage[recharge[i].rechargeId].score = 0
      self.rechargeStage[recharge[i].rechargeId].endTime = recharge[i].endTime
      self.rechargeStage[recharge[i].rechargeId].rechargeId = recharge[i].rechargeId
      self.rechargeStage[recharge[i].rechargeId].startTime = recharge[i].startTime
      self.rechargeStage[recharge[i].rechargeId].stageInfo = {}
      self.rechargeStage[recharge[i].rechargeId].nextResetTime = message.nextResetTime or 0
      if self.rechargeStage[recharge[i].rechargeId].endTime and self.rechargeStage[recharge[i].rechargeId].nextResetTime ~= 0 and self.rechargeStage[recharge[i].rechargeId].endTime <= self.rechargeStage[recharge[i].rechargeId].nextResetTime then
        self.rechargeStage[recharge[i].rechargeId].nextResetTime = 0
      end
    end
  end
end
local RequestDat = function(self, message)
  for k, v in pairs(self.rechargeStage) do
    SFSNetwork.SendMessage(MsgDefines.GetRechargeInfo, k)
  end
end
local SendGetRechargeInfo = function(self, rechargeId)
  SFSNetwork.SendMessage(MsgDefines.GetRechargeInfo, rechargeId)
end
local UpdateRechargeStageInfo = function(self, message)
  local rechargeId = message.rechargeId
  if not rechargeId then
    return
  end
  if message.rechargeStageArr then
    if not self.rechargeStage[rechargeId] then
      self.rechargeStage[rechargeId] = {}
    end
    self.rechargeStage[rechargeId].stageInfo = {}
    for i = 1, #message.rechargeStageArr do
      local stageInfo = RechargeStageInfo.New()
      stageInfo:ParseData(message.rechargeStageArr[i])
      table.insert(self.rechargeStage[message.rechargeId].stageInfo, stageInfo)
    end
  end
  self.rechargeStage[rechargeId].score = message.score
  self.rechargeStage[rechargeId].nextResetTime = message.nextResetTime or 0
  if self.rechargeStage[rechargeId].endTime and self.rechargeStage[rechargeId].nextResetTime ~= 0 and self.rechargeStage[rechargeId].endTime <= self.rechargeStage[rechargeId].nextResetTime then
    self.rechargeStage[rechargeId].nextResetTime = 0
  end
  self:CheckAddTimer()
  EventManager:GetInstance():Broadcast(EventId.UpdateAccuRechargeData, rechargeId)
  EventManager:GetInstance():Broadcast(EventId.RefreshActivityRedDot)
end
local PushRechargeScoreHandle = function(self, message)
  local rechargeId = message.rechargeId
  if rechargeId then
    local prevScore = self.rechargeStage[rechargeId].score
    if prevScore and not self.cacheLastReachStage[rechargeId] then
      self.cacheLastReachStage[rechargeId] = self:GetReachStage(rechargeId, prevScore)
    end
    self.rechargeStage[rechargeId].score = message.score
    EventManager:GetInstance():Broadcast(EventId.RefreshAccuRechargePoint, rechargeId)
    local lastReachStage = self.cacheLastReachStage[rechargeId] or 0
    local curReachStage = self:GetReachStage(rechargeId, message.score)
    if lastReachStage ~= curReachStage then
      EventManager:GetInstance():Broadcast(EventId.RefreshActivityRedDot)
      self.cacheLastReachStage[rechargeId] = curReachStage
    end
  end
end
local PushNewRechargeHandle = function(self, message)
  self.rechargeStage[message.rechargeId] = {}
  self.rechargeStage[message.rechargeId].score = 0
  self.rechargeStage[message.rechargeId].endTime = message.endTime
  self.rechargeStage[message.rechargeId].rechargeId = message.rechargeId
  self.rechargeStage[message.rechargeId].startTime = message.startTime
  self.rechargeStage[message.rechargeId].stageInfo = {}
  self.rechargeStage[message.rechargeId].nextResetTime = message.nextResetTime or 0
  if self.rechargeStage[message.rechargeId].endTime and self.rechargeStage[message.rechargeId].nextResetTime ~= 0 and self.rechargeStage[message.rechargeId].endTime <= self.rechargeStage[message.rechargeId].nextResetTime then
    self.rechargeStage[message.rechargeId].nextResetTime = 0
  end
  SFSNetwork.SendMessage(MsgDefines.GetRechargeInfo, message.rechargeId)
end
local SendReward = function(self, rechargeId, stageId)
  SFSNetwork.SendMessage(MsgDefines.ReceiveRechargeReward, tonumber(rechargeId), tonumber(stageId))
end
local SendRewardHandle = function(self, message)
  local info = self.rechargeStage[message.rechargeId].stageInfo
  for i = 1, #info do
    if info[i].stageId == message.stageId then
      info[i]:UpdateState(1)
    end
  end
  EventManager:GetInstance():Broadcast(EventId.CumulativeReward, message.stageId)
  EventManager:GetInstance():Broadcast(EventId.RefreshActivityRedDot)
  DataCenter.RewardManager:ShowCommonReward(message)
  DataCenter.RewardManager:AddRewardsAndRes(message)
end
local CheckIsShow = function(self)
  return DataCenter.ActivityListDataManager:CheckIsShowGiftPackPoint()
end
local GetInfo = function(self)
  for i, v in pairs(self.rechargeStage) do
    if next(v) then
      return i, v
    end
  end
end
local GetCurStage = function(self, rechargeId)
  local index = 0
  if self.rechargeStage[rechargeId] then
    local data = self.rechargeStage[rechargeId]
    local stageInfo = data.stageInfo
    for i = 1, #stageInfo do
      if data.score >= stageInfo[i].needScore then
        index = i
      end
    end
  end
  return index
end
local GetRedNum = function(self)
  local count = 0
  for i, v in pairs(self.rechargeStage) do
    if next(v) then
      for i = 1, #v.stageInfo do
        if v.stageInfo[i].state == 0 and v.score >= v.stageInfo[i].needScore then
          count = count + 1
        end
      end
    end
  end
  return count
end
local GetActRedNum = function(self, id)
  local count = 0
  if self.rechargeStage[id] then
    local rechargeStage = self.rechargeStage[id]
    for i = 1, #rechargeStage.stageInfo do
      if rechargeStage.stageInfo[i].state == 0 and rechargeStage.score >= rechargeStage.stageInfo[i].needScore then
        count = count + 1
      end
    end
  end
  return count
end
local GetRechargeStage = function(self, id)
  return self.rechargeStage[tonumber(id)]
end
local CheckCanRecv = function(self)
  local id, data = self:GetInfo()
  for i = 1, #data.stageInfo do
    if data.stageInfo[i].state == 0 and data.score >= data.stageInfo[i].needScore then
      return i
    end
  end
  return 0
end
local SetCurStage = function(self, Stage)
  self.tempStage = Stage
  self.isFirst = false
end
local GetLastStage = function(self)
  return self.tempStage
end
local GetStageIsFirst = function(self)
  return self.isFirst
end
local CheckAddTimer = function(self)
  self:RemoveTimer()
  local curTime = UITimeManager:GetInstance():GetServerSeconds()
  if not table.IsNullOrEmpty(self.rechargeStage) then
    local minTime
    for i, v in pairs(self.rechargeStage) do
      if v.nextResetTime > 0 and curTime < v.nextResetTime then
        if not minTime then
          minTime = v.nextResetTime
        elseif minTime > v.nextResetTime then
          minTime = v.nextResetTime
        end
      end
    end
    if minTime then
      local callback = function()
        self.refreshTimer = nil
        local curTime = UITimeManager:GetInstance():GetServerSeconds()
        for i, v in pairs(self.rechargeStage) do
          if v.nextResetTime > 0 and curTime > v.nextResetTime then
            v.nextResetTime = 0
            SFSNetwork.SendMessage(MsgDefines.GetRechargeInfo, v.rechargeId)
          end
        end
        self:CheckAddTimer()
      end
      self.refreshTimer = TimerManager:GetInstance():GetTimer(minTime - curTime, callback, nil, true)
      self.refreshTimer:Start()
    end
  end
end
local RemoveTimer = function(self)
  if self.refreshTimer then
    self.refreshTimer:Stop()
    self.refreshTimer = nil
  end
end
CumulativeRechargeManager.__init = __init
CumulativeRechargeManager.__delete = __delete
CumulativeRechargeManager.InitDat = InitDat
CumulativeRechargeManager.RequestDat = RequestDat
CumulativeRechargeManager.SendGetRechargeInfo = SendGetRechargeInfo
CumulativeRechargeManager.UpdateRechargeStageInfo = UpdateRechargeStageInfo
CumulativeRechargeManager.PushRechargeScoreHandle = PushRechargeScoreHandle
CumulativeRechargeManager.PushNewRechargeHandle = PushNewRechargeHandle
CumulativeRechargeManager.SendReward = SendReward
CumulativeRechargeManager.SendRewardHandle = SendRewardHandle
CumulativeRechargeManager.CheckIsShow = CheckIsShow
CumulativeRechargeManager.GetInfo = GetInfo
CumulativeRechargeManager.GetCurStage = GetCurStage
CumulativeRechargeManager.GetRedNum = GetRedNum
CumulativeRechargeManager.GetActRedNum = GetActRedNum
CumulativeRechargeManager.GetRechargeStage = GetRechargeStage
CumulativeRechargeManager.CheckCanRecv = CheckCanRecv
CumulativeRechargeManager.SetCurStage = SetCurStage
CumulativeRechargeManager.GetLastStage = GetLastStage
CumulativeRechargeManager.GetStageIsFirst = GetStageIsFirst
CumulativeRechargeManager.GetReachStage = GetReachStage
CumulativeRechargeManager.CheckAddTimer = CheckAddTimer
CumulativeRechargeManager.RemoveTimer = RemoveTimer
return CumulativeRechargeManager
