local this = class("announcementStore", G_BaseStore)
this:importPartialClass(require(L_R.store .. "announcement.announcementState"))
this:importPartialClass(require(L_R.store .. "announcement.announcementAction"))
this.announcementDataCls = require(L_R.store .. "announcement.announcementData")
this.announcementResponeDataInfo = require(L_R.store .. "announcement.announcementResponeDataInfo")
this.event = {
  announcementClear = "announcementClear",
  announcementAdd = "announcementAdd",
  announcementEnd = "announcementEnd",
  refreshAnnouncementRedDot = "refreshAnnouncementRedDot",
  syncAnnouncementResponeDataInfoOfGatewayComplete = "syncAnnouncementResponeDataInfoOfGatewayComplete"
}

function this:syncAnnouncementDatas(protoData, errorCode)
  if errorCode ~= L_Const.errorCode.ErrCodeSucc or not protoData then
    self:syncClear()
    return
  end
  local list = {}
  local refreshList = {}
  local haveNew = false
  local needRefresh = false
  for _, data in ipairs(protoData.notics or L_Const.emptyTable) do
    local id = data.id
    if id ~= nil then
      if self.data.announcementDatas[id] == nil then
        haveNew = true
      else
        table.insert(refreshList, id)
      end
      needRefresh = true
      self.data.announcementDatas[id] = self.data.announcementDatas[id] or this.announcementDataCls.new(id)
      self.data.announcementDatas[id]:populate(data)
      if haveNew then
        table.insert(list, self.data.announcementDatas[id])
      end
    end
  end
  if haveNew or needRefresh then
    self:call(this.event.announcementAdd, self.data.announcementDatas, refreshList)
  end
  self:call(this.event.refreshAnnouncementRedDot)
  self._init = true
  L_AnnouncementManager:checkAnnounceTimeout()
end

function this:syncAnnouncementResponeDataInfoOfGateway(announcementRespone, errorCode)
  if errorCode ~= L_Const.errorCode.ErrCodeSucc or not announcementRespone then
    self.data.announcementResponeDataInfoOfGateway = nil
    return
  end
  local csAnnouncementResponeDataInfoOfGateway = announcementRespone.data.gateway or nil
  self.data.announcementResponeDataInfoOfGateway = csAnnouncementResponeDataInfoOfGateway and this.announcementResponeDataInfo.new(csAnnouncementResponeDataInfoOfGateway) or nil
  self:call(this.event.syncAnnouncementResponeDataInfoOfGatewayComplete)
end

function this:syncAnnouncementDatasByHttp(announcementRespone, errorCode)
  if errorCode ~= L_Const.errorCode.ErrCodeSucc or not announcementRespone then
    self:syncClear()
    return
  end
  local protoData = {}
  protoData.notics = {}
  local curCacheServerName = L_CommonUtil.getGlobalValue(L_Const.prefsKey.serverName)
  local isLoginPage = L_UI:checkPageOpen("pagePlatformLogin")
  local list = announcementRespone.data.infos
  if list ~= nil then
    for i = 0, list.Count - 1 do
      local sData = list[i]
      local serverName = sData.name
      local needShow_position = isLoginPage and 0 < sData.showPosition or not isLoginPage and (sData.showPosition == 0 or sData.showPosition == 2)
      local needShow_server = isLoginPage or not isLoginPage and (serverName == "0" or serverName ~= "0" and serverName == curCacheServerName)
      if needShow_position and needShow_server then
        local data = {}
        data.publish_time = sData.publishTime
        data.id = sData.id
        data.order_id = sData.orderId
        data.tab_id = sData.tabId
        data.end_time = sData.endTime
        data.type = sData.type
        data.jump_id = sData.jumpId
        data.hide = sData.hide
        data.meta = sData.meta
        data.showPosition = sData.showPosition
        table.insert(protoData.notics, data)
      end
    end
  end
  if table.isEmpty(protoData.notics) then
    self:syncClear()
  else
    self:syncAnnouncementDatas(protoData, errorCode)
  end
end

function this:syncClear()
  self.data.announcementDatas = {}
  self:call(this.event.announcementClear)
end

function this:isInit()
  return self._init
end

function this:getAnnouncementDatas()
  return self.data.announcementDatas
end

function this:getAnnouncementDataById(id)
  return self.data.announcementDatas[id]
end

function this:getAnnouncementIsRead(id)
  return L_ReddotManager:haveNew(L_ReddotManager.DotDef.AnnouncementActive, id)
end

function this:setAnnouncementIsRead(id)
  if id == nil then
    return
  end
  L_ReddotManager:markNew(L_ReddotManager.DotDef.AnnouncementActive, id)
  self:call(this.event.refreshAnnouncementRedDot)
end

function this:onAnnounceTimeout()
  local timeComeList = {}
  local timeOutList = {}
  for i, data in pairs(self.data.announcementDatas) do
    if not data.cacheTimeOut and not data:IsValid() then
      data.cacheTimeOut = true
      table.insert(timeOutList, data)
    end
    if data.cacheTimeOut and data:IsValid(true) then
      data.cacheTimeOut = false
      table.insert(timeComeList, data)
    end
  end
  local haveValue = false
  for i, v in pairs(timeOutList) do
    haveValue = true
  end
  if haveValue then
    self:call(this.event.announcementEnd, timeOutList)
    self:call(this.event.refreshAnnouncementRedDot)
  end
  if 0 < #timeComeList then
    self:call(this.event.announcementAdd, timeComeList)
    self:call(this.event.refreshAnnouncementRedDot)
  end
  for i, v in pairs(timeOutList) do
    self.data.announcementDatas[v:getId()] = nil
  end
end

return this
