local HeroBountyDataManager = BaseClass("HeroBountyDataManager")
local HeroBountyTaskData = require("DataCenter.HeroBountyDataManager.HeroBountyTaskData")
local __init = function(self)
  self.taskList = {}
  self.oldHeroLevel = {}
  self.curTaskEndTime = 0
  self.timer = nil
  self.timeFinishFlag = false
  self.updateFlag = false
  self.hasFreeTask = false
  
  function self.timer_action(temp)
    self:CheckAllQueueTimeFinish()
  end
  
  self.showUIAnim = true
  self:AddTimer()
end
local __delete = function(self)
  self.taskList = nil
  self.oldHeroLevel = nil
  self.timer_action = nil
  self.curTaskEndTime = nil
  self.timeFinishFlag = nil
  self.updateFlag = nil
  self.showUIAnim = nil
  self:DeleteTimer()
end
local DeleteTimer = function(self)
  if self.timer ~= nil then
    self.timer:Stop()
    self.timer = nil
  end
end
local AddTimer = function(self)
  if self.timer == nil then
    self.timer = TimerManager:GetInstance():GetTimer(1, self.timer_action, self, false, false, false)
  end
  self.timer:Start()
end
local RefreshTaskEndTime = function(self)
  self.curTaskEndTime = 0
  self.timeFinishFlag = false
  self.updateFlag = true
  self.hasFreeTask = false
  for k, v in pairs(self.taskList) do
    if 0 < v.endTime then
      if self.curTaskEndTime > 0 then
        if self.curTaskEndTime > v.endTime then
          self.curTaskEndTime = v.endTime
        end
      else
        self.curTaskEndTime = v.endTime
      end
    else
      self.hasFreeTask = true
    end
  end
  if self.curTaskEndTime > 0 then
    local curTime = UITimeManager:GetInstance():GetServerTime()
    if curTime > self.curTaskEndTime then
      self.timeFinishFlag = true
      self.updateFlag = false
    end
  else
    self.updateFlag = false
  end
  EventManager:GetInstance():Broadcast(EventId.RefreshHeroBountyBubble)
end
local GetShowUIAnim = function(self)
  return self.showUIAnim
end
local SetShowUIAnim = function(self, value)
  self.showUIAnim = value
end
local CheckAllQueueTimeFinish = function(self)
  if self.updateFlag == true and self.curTaskEndTime > 0 then
    local curTime = UITimeManager:GetInstance():GetServerTime()
    if curTime > self.curTaskEndTime then
      self.timeFinishFlag = true
      self.updateFlag = false
      EventManager:GetInstance():Broadcast(EventId.RefreshHeroBountyBubble)
    end
  end
end
local GetIsShowBubble = function(self)
  if self.hasFreeTask == true or self.timeFinishFlag == true then
    return true
  else
    return false
  end
end
local GetIsTaskFinish = function(self)
  return self.timeFinishFlag
end
local UpdateAllData = function(self, message)
  self.taskList = {}
  if message.tasks ~= nil then
    local arr = message.tasks
    for k, v in pairs(arr) do
      local oneData = HeroBountyTaskData.New()
      oneData:ParseData(v)
      if oneData.index ~= 0 then
        self.taskList[oneData.index] = oneData
      end
    end
  end
  self:RefreshTaskEndTime()
  EventManager:GetInstance():Broadcast(EventId.OnHeroBountyTaskRefresh)
end
local RefreshOneTaskData = function(self, message)
  local index = 0
  if message.index ~= nil then
    index = message.index
    self.taskList[message.index] = nil
  end
  if message.newTask ~= nil then
    local oneData = HeroBountyTaskData.New()
    oneData:ParseData(message.newTask)
    if oneData.index ~= 0 then
      self.taskList[oneData.index] = oneData
    end
  end
  if message.gold ~= nil then
    LuaEntry.Player.gold = message.gold
    EventManager:GetInstance():Broadcast(EventId.UpdateGold)
  end
  if index ~= 0 then
    EventManager:GetInstance():Broadcast(EventId.OnHeroBountyOneTaskRefresh, index)
  end
  self:RefreshTaskEndTime()
end
local SendReceiveMessage = function(self, index)
  self.oldHeroLevel[index] = {}
  local task = self:GetTaskDataByIndex(index)
  if task ~= nil then
    local hero = task.heroInfos
    if hero ~= nil then
      for k, v in pairs(hero) do
        local heroData = DataCenter.HeroDataManager:GetHeroByUuid(k)
        if heroData ~= nil then
          self.oldHeroLevel[index][k] = heroData.level
        end
      end
    end
  end
  SFSNetwork.SendMessage(MsgDefines.ReceiveHeroBountyTaskReward, index)
end
local OnRewardReceive = function(self, message)
  local index = 0
  if message.index ~= nil then
    index = message.index
    self.taskList[message.index] = nil
  end
  if message.newTask ~= nil then
    local oneData = HeroBountyTaskData.New()
    oneData:ParseData(message.newTask)
    if oneData.index ~= 0 then
      self.taskList[oneData.index] = oneData
    end
  end
  if message.reward ~= nil then
    if message.heroExpReward ~= nil then
      local reportReward = PBController.ParsePb1(message.heroExpReward, "protobuf.ReportReward")
      local exps = reportReward.rewardHeroExps
      if self.oldHeroLevel[index] ~= nil then
        local oldData = self.oldHeroLevel[index]
        for k, v in pairs(exps) do
          local uuid = v.heroUuid
          if oldData[uuid] ~= nil then
            v.oldLevel = oldData[uuid]
          else
            v.oldLevel = 0
          end
        end
        self.oldHeroLevel[index] = {}
      end
      DataCenter.RewardManager:ShowCommonReward(message, CS.GameEntry.Localization:GetString("132234"), nil, exps)
    else
      DataCenter.RewardManager:ShowCommonReward(message, nil, nil)
    end
    DataCenter.RewardManager:AddRewardsAndRes(message)
  end
  if index ~= 0 then
    EventManager:GetInstance():Broadcast(EventId.OnHeroBountyOneTaskRefresh, index)
  end
  self:RefreshTaskEndTime()
end
local OnStartTaskReceive = function(self, message)
  if message.task ~= nil then
    local oneData = HeroBountyTaskData.New()
    oneData:ParseData(message.task)
    if oneData.index ~= 0 then
      self.taskList[oneData.index] = oneData
      EventManager:GetInstance():Broadcast(EventId.OnHeroBountyOneTaskRefresh, oneData.index)
    end
  end
  self:RefreshTaskEndTime()
end
local GetTaskDataByIndex = function(self, index)
  return self.taskList[index]
end
local GetSlotDataByBuildData = function(self)
  local list = {}
  local level = 1
  local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.FUN_BUILD_HERO_BOUNTY)
  if buildData ~= nil then
    level = buildData.level
  end
  local buildLevelTemplate = DataCenter.BuildTemplateManager:GetBuildingLevelTemplate(BuildingTypes.FUN_BUILD_HERO_BOUNTY, level)
  if buildLevelTemplate ~= nil then
    local para1 = buildLevelTemplate.para1
    list = string.split(para1, ";")
  end
  return list
end
local GetNextLevelBuildUnlockMsg = function(self)
  local level = 1
  local list = {}
  local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.FUN_BUILD_HERO_BOUNTY)
  if buildData ~= nil then
    level = buildData.level
  end
  local buildLevelTemplate = DataCenter.BuildTemplateManager:GetBuildingLevelTemplate(BuildingTypes.FUN_BUILD_HERO_BOUNTY, level)
  if buildLevelTemplate ~= nil then
    local para2 = buildLevelTemplate.para2
    list = string.split(para2, ";")
  end
  return list
end
local GetHeroIsInTaskByUuid = function(self, uuid)
  for k, v in pairs(self.taskList) do
    if v.heroInfos ~= nil and v.heroInfos[uuid] ~= nil then
      return true
    end
  end
  return false
end
HeroBountyDataManager.__init = __init
HeroBountyDataManager.__delete = __delete
HeroBountyDataManager.GetTaskDataByIndex = GetTaskDataByIndex
HeroBountyDataManager.OnStartTaskReceive = OnStartTaskReceive
HeroBountyDataManager.OnRewardReceive = OnRewardReceive
HeroBountyDataManager.UpdateAllData = UpdateAllData
HeroBountyDataManager.GetSlotDataByBuildData = GetSlotDataByBuildData
HeroBountyDataManager.GetNextLevelBuildUnlockMsg = GetNextLevelBuildUnlockMsg
HeroBountyDataManager.GetHeroIsInTaskByUuid = GetHeroIsInTaskByUuid
HeroBountyDataManager.SendReceiveMessage = SendReceiveMessage
HeroBountyDataManager.RefreshOneTaskData = RefreshOneTaskData
HeroBountyDataManager.DeleteTimer = DeleteTimer
HeroBountyDataManager.AddTimer = AddTimer
HeroBountyDataManager.CheckAllQueueTimeFinish = CheckAllQueueTimeFinish
HeroBountyDataManager.GetIsShowBubble = GetIsShowBubble
HeroBountyDataManager.GetIsTaskFinish = GetIsTaskFinish
HeroBountyDataManager.RefreshTaskEndTime = RefreshTaskEndTime
HeroBountyDataManager.GetShowUIAnim = GetShowUIAnim
HeroBountyDataManager.SetShowUIAnim = SetShowUIAnim
return HeroBountyDataManager
