local ServerStatusManager = BaseClass("ServerStatusManager")

function ServerStatusManager:__init()
  self.globalStatus = {}
  self.globalEffectDic = {}
end

function ServerStatusManager:__delete()
  self.globalStatus = nil
  self.globalEffectDic = nil
end

function ServerStatusManager:InitData(message)
  if message ~= nil and message.globalState ~= nil then
    self:SetGlobalStatus(message.globalState.list)
  end
end

function ServerStatusManager:SetGlobalStatus(message)
  self.globalStatus = {}
  self.globalEffectDic = {}
  local needBroadcast = false
  if message then
    for k, v in pairs(message) do
      if v and v.effects and v.reason and v.stateId and (v.reason == FetchGlobalStateReason.CITY_BATTLE_S1_REST_DEFEND or v.reason == FetchGlobalStateReason.WEATHER) then
        for index, effect in ipairs(v.effects) do
          local effectId = effect.eff
          local effectValue = effect.val
          self.globalEffectDic[effectId] = (self.globalEffectDic[effectId] or 0) + effectValue
        end
        table.insert(self.globalStatus, v)
        needBroadcast = true
      end
    end
  end
  if needBroadcast then
    EventManager:GetInstance():Broadcast(EventId.ServerStatusChanged)
  end
end

function ServerStatusManager:GetGlobalStatus()
  return self.globalStatus or {}
end

function ServerStatusManager:GetEffectById(effectId)
  if BattleFieldUtil.InBattleField() then
    return 0
  end
  return self.globalEffectDic[effectId] or 0
end

return ServerStatusManager
