local ScreenEffectManager = BaseClass("ScreenEffectManager", CEventable)
local ScreenEffectSnowStormEffect = require("DataCenter.ScreenEffectManager.ScreenEffectSnowStormEffect")

function ScreenEffectManager:__init()
  self.effectMap = {}
  self:AddListener()
  local isInSeason = SeasonUtil.IsInSeason(true)
  local data = ScreenEffectSnowStormEffect.New()
  table.insert(self.effectMap, data)
  if not isInSeason then
    return
  end
  local seasonType = SeasonUtil.GetSeasonType()
  if seasonType == SeasonMapType.CityStronghold then
    if DataCenter.SeasonWeatherManager:IsOpen() then
      self:OnSeasonWeatherInfoUpdate()
    end
  elseif seasonType == SeasonMapType.Snow then
    local SnowStormWorldEffect = require("DataCenter.ScreenEffectManager.SnowStormWorldEffect")
    data = SnowStormWorldEffect.New()
    table.insert(self.effectMap, data)
  elseif seasonType == SeasonMapType.Mummy then
    local MummyDesertEffect = require("DataCenter.ScreenEffectManager.MummyDesertEffect")
    data = MummyDesertEffect.New()
    table.insert(self.effectMap, data)
  elseif seasonType == SeasonMapType.Darkness then
    local sunrise = DataCenter.BloodyNightDataManager:IsSunrise()
    if not sunrise then
      local ScreenEffectBloodyNightEffect = require("DataCenter.ScreenEffectManager.ScreenEffectBloodyNightEffect")
      data = ScreenEffectBloodyNightEffect.New()
      table.insert(self.effectMap, data)
      local ScreenEffectWhiteNightEffect = require("DataCenter.ScreenEffectManager.ScreenEffectWhiteNightEffect")
      data = ScreenEffectWhiteNightEffect.New()
      table.insert(self.effectMap, data)
    end
  elseif seasonType == SeasonMapType.NineNation then
    local ScreenRandomEffect = require("DataCenter.ScreenEffectManager.ScreenRandomEffect")
    data = ScreenRandomEffect.New()
    table.insert(self.effectMap, data)
  end
end

function ScreenEffectManager:__delete()
  if #self.effectMap > 0 then
    for key, value in pairs(self.effectMap) do
      value:Delete()
    end
  end
  self.effectMap = nil
  self:RemoveListener()
end

function ScreenEffectManager:AddListener()
  self:RegisterEvent(EventId.OnEnterCity, self.EnterCity)
  self:RegisterEvent(EventId.OnEnterWorld, self.EnterWorld)
  self:RegisterEvent(EventId.PveLevelEnter, self.EnterLevel)
  self:RegisterEvent(EventId.LWSeasonWeatherInfoUpdate, self.OnSeasonWeatherInfoUpdate)
end

function ScreenEffectManager:RemoveListener()
  self:UnregisterEvent(EventId.OnEnterCity)
  self:UnregisterEvent(EventId.OnEnterWorld)
  self:UnregisterEvent(EventId.PveLevelEnter)
  self:UnregisterEvent(EventId.LWSeasonWeatherInfoUpdate)
end

function ScreenEffectManager:OnEnterGame()
end

function ScreenEffectManager:EnterCity()
  if #self.effectMap > 0 then
    for key, value in pairs(self.effectMap) do
      value:OnSceneChange(ScreenEffectSceneFilter.City)
    end
  end
end

function ScreenEffectManager:EnterWorld()
  local curFilter = ScreenEffectSceneFilter.None
  if not BattleFieldUtil.InBattleField() then
    curFilter = ScreenEffectSceneFilter.World
  end
  if #self.effectMap > 0 then
    for key, value in pairs(self.effectMap) do
      value:OnSceneChange(curFilter)
    end
  end
end

function ScreenEffectManager:EnterLevel()
  if #self.effectMap > 0 then
    for key, value in pairs(self.effectMap) do
      value:OnSceneChange(ScreenEffectSceneFilter.None)
    end
  end
end

function ScreenEffectManager:OnSeasonWeatherInfoUpdate()
  if not DataCenter.SeasonWeatherManager:IsOpen() then
    return
  end
  self:UnregisterEvent(EventId.LWSeasonWeatherInfoUpdate)
  local WeatherWorldPoints = require("DataCenter.ScreenEffectManager.WeatherWorldPoints")
  local data = WeatherWorldPoints.New(self.effectMap)
  table.insert(self.effectMap, data)
  local WeatherWorldEffect = require("DataCenter.ScreenEffectManager.WeatherWorldEffect")
  data = WeatherWorldEffect.New()
  table.insert(self.effectMap, data)
  local ScreenEffectWeatherEffect = require("DataCenter.ScreenEffectManager.ScreenEffectWeatherEffect")
  data = ScreenEffectWeatherEffect.New()
  table.insert(self.effectMap, data)
  local ScreenEffectWeatherLoopEffect = require("DataCenter.ScreenEffectManager.ScreenEffectWeatherLoopEffect")
  data = ScreenEffectWeatherLoopEffect.New()
  table.insert(self.effectMap, data)
end

return ScreenEffectManager
