local WorldNoticeManager = BaseClass("WorldNoticeManager", Singleton)
local WorldNoticeDataInfo = require("DataCenter.WorldNoticeManager.WorldNoticeDataInfo")

function WorldNoticeManager:__init()
  self.dataList = {}
  self.curUuid = 0
  self:AddListener()
end

function WorldNoticeManager:__delete()
  self.dataList = nil
  self.curUuid = nil
  self:RemoveListener()
end

function WorldNoticeManager:InitData()
  SFSNetwork.SendMessage(MsgDefines.GetNoticeList)
end

function WorldNoticeManager:ParseNoticeList(message)
  if message and message.noticeList ~= nil then
    local list = message.noticeList
    for i = 1, table.count(list) do
      local tempData = WorldNoticeDataInfo.New()
      tempData:UpdateDataInfo(list[i])
      table.insert(self.dataList, tempData)
    end
  end
end

function WorldNoticeManager:NewNoticeHandle(message)
  if message then
    local data = message.notice
    local tempData = WorldNoticeDataInfo.New()
    tempData:UpdateDataInfo(data)
    if self.dataList then
      table.insert(self.dataList, tempData)
    else
      self.dataList = {}
      table.insert(self.dataList, tempData)
    end
    EventManager:GetInstance():Broadcast(EventId.RefreshNotice)
  end
end

function WorldNoticeManager:DeleteNoticeHandle(message)
  if self.dataList then
    for i = 1, table.count(self.dataList) do
      if self.dataList[i].noticeId == message.noticeId then
        table.remove(self.dataList, i)
        break
      end
    end
  end
  EventManager:GetInstance():Broadcast(EventId.RefreshNotice)
end

function WorldNoticeManager:AddListener()
end

function WorldNoticeManager:RemoveListener()
end

function WorldNoticeManager:PushWorldTrendBubble()
  EventManager:GetInstance():Broadcast(EventId.WorldTrendRedUpdate)
end

function WorldNoticeManager:GetDataInfo()
  return self.dataList
end

function WorldNoticeManager:GetNoticeInfoById(uuid)
  if self.dataList then
    for i = 1, table.count(self.dataList) do
      if self.dataList[i].uuid == uuid then
        return self.dataList[i]
      end
    end
  end
  return nil
end

function WorldNoticeManager:SendReadNotice(uuid)
  SFSNetwork.SendMessage(MsgDefines.ReadNotice, uuid)
end

function WorldNoticeManager:SetCurId(uid)
  self.curUuid = uid
end

function WorldNoticeManager:GetCurId()
  return self.curUuid
end

function WorldNoticeManager:ReadNoticeHandle(message)
  if message then
    for i = 1, table.count(self.dataList) do
      if self.dataList[i].uuid == message.uuid then
        self.dataList[i].status = message.status
      end
    end
    EventManager:GetInstance():Broadcast(EventId.RefreshNotice)
  end
end

function WorldNoticeManager:SendReceiveReward(uuid)
  SFSNetwork.SendMessage(MsgDefines.ReceiceNoticeReward, uuid)
end

function WorldNoticeManager:ReceiveRewardHandle(message)
  if message.reward ~= nil then
    DataCenter.RewardManager:ShowCommonReward(message)
    DataCenter.RewardManager:AddRewardsAndRes(message)
  end
  for i = 1, table.count(self.dataList) do
    if self.dataList[i].uuid == message.uuid then
      self.dataList[i].rewardStatus = message.rewardStatus
    end
  end
  EventManager:GetInstance():Broadcast(EventId.RefreshNotice)
  EventManager:GetInstance():Broadcast(EventId.NoticeItemReward)
end

function WorldNoticeManager:CheckShow()
  local data = self:GetDataInfo()
  if data then
    for i = 1, table.count(data) do
      if data[i].status == 0 or data[i].rewardStatus == 0 then
        return true
      end
    end
  end
  return false
end

function WorldNoticeManager:CheckNotice()
  local data = self:GetDataInfo()
  if data and table.count(data) > 0 then
    return true
  end
  return false
end

return WorldNoticeManager
