local this = class("announcementManager")

function this:checkAnnounceTimeout()
  L_AnnouncementStore:onAnnounceTimeout()
  local minEndTime, minStartTime = self:getAnnounceMinEndTime()
  if 0 < minEndTime then
    if self.timer ~= nil then
      Timer.remove(self.timer)
      self.timer = nil
    end
    self.timer = Timer.once(minEndTime + 1, self.onAnnounceTimeout, self)
  end
  if 0 < minStartTime then
    if self.startTimer ~= nil then
      Timer.remove(self.startTimer)
      self.startTimer = nil
    end
    self.startTimer = Timer.once(minStartTime + 1, self.onAnnounceTimecome, self)
  end
end

function this:onAnnounceTimecome()
  if self.startTimer ~= nil then
    Timer.remove(self.startTimer)
    self.startTimer = nil
  end
  self:checkAnnounceTimeout()
  L_AnnouncementStore:onAnnounceTimeout()
end

function this:onAnnounceTimeout()
  if self.timer ~= nil then
    Timer.remove(self.timer)
    self.timer = nil
  end
  self:checkAnnounceTimeout()
  L_AnnouncementStore:onAnnounceTimeout()
end

function this:getAnnounceMinEndTime()
  local minEndTime = -1
  local minStartTime = -1
  if not L_AnnouncementStore:isInit() then
    return minEndTime, minStartTime
  end
  local datas = L_AnnouncementStore:getAnnouncementDatas()
  for i, data in pairs(datas) do
    if data:IsValid() then
      local releaseTime = data:getReleaseTime()
      if 0 < releaseTime and (minEndTime < 0 or minEndTime >= releaseTime) then
        minEndTime = releaseTime
      end
    else
      local startInterval = data:getStartInterval()
      if 0 < startInterval and (minStartTime < 0 or minStartTime >= startInterval) then
        minStartTime = startInterval
      end
    end
  end
  return minEndTime, minStartTime
end

return this
