local BuildEffectManager = BaseClass("BuildEffectManager")
local BuildEffectObj = require("DataCenter.BuildEffectManager.BuildEffectObj")
local TWModelObj = require("DataCenter.BuildEffectManager.TWModelObj")
local LWDominatorCityBuilding = require("UI/UILWDominator/Main/City/LWDominatorCityBuilding")

function BuildEffectManager:__init()
  self.loadedConfig = false
  self.worldAllEffect = {}
  self.cityAllEffect = {}
  self.twEffect = nil
  self.dominatorEffect = nil
  self:AddListener()
end

function BuildEffectManager:__delete()
  for i, effect in pairs(self.worldAllEffect) do
    if effect then
      effect:Delete()
      effect = nil
    end
  end
  for i, effect in pairs(self.cityAllEffect) do
    if effect then
      effect:Delete()
      effect = nil
    end
  end
  if self.twEffect then
    self.twEffect:Delete()
    self.twEffect = nil
  end
  if self.dominatorEffect then
    self.dominatorEffect:Delete()
    self.dominatorEffect = nil
  end
  self:RemoveListener()
end

function BuildEffectManager:Startup()
end

function BuildEffectManager:AddListener()
  EventManager:GetInstance():AddListener(EventId.WORLD_BUILD_IN_VIEW, self.OnWorldBuildInView)
  EventManager:GetInstance():AddListener(EventId.CheckDomeOpen, self.OnWorldBuildInView)
  EventManager:GetInstance():AddListener(EventId.WORLD_BUILD_OUT_VIEW, self.OnWorldBuildOutView)
  EventManager:GetInstance():AddListener(EventId.UserSkinUpdate, self.OnUserSkinUpdate)
  EventManager:GetInstance():AddListener(EventId.BUILD_IN_VIEW, self.OnCityBuildInView)
  EventManager:GetInstance():AddListener(EventId.BUILD_OUT_VIEW, self.OnCityBuildOutView)
  EventManager:GetInstance():AddListener(EventId.DominatorAppearanceUpdate, self.OnDominatorAppearanceUpdate)
end

function BuildEffectManager:RemoveListener()
  EventManager:GetInstance():RemoveListener(EventId.WORLD_BUILD_IN_VIEW, self.OnWorldBuildInView)
  EventManager:GetInstance():RemoveListener(EventId.CheckDomeOpen, self.OnWorldBuildInView)
  EventManager:GetInstance():RemoveListener(EventId.WORLD_BUILD_OUT_VIEW, self.OnWorldBuildOutView)
  EventManager:GetInstance():RemoveListener(EventId.UserSkinUpdate, self.OnUserSkinUpdate)
  EventManager:GetInstance():RemoveListener(EventId.BUILD_IN_VIEW, self.OnCityBuildInView)
  EventManager:GetInstance():RemoveListener(EventId.BUILD_OUT_VIEW, self.OnCityBuildOutView)
  EventManager:GetInstance():RemoveListener(EventId.DominatorAppearanceUpdate, self.OnDominatorAppearanceUpdate)
end

function BuildEffectManager:GetWorldBuildModelByBUid(bUuid)
  if not CS.SceneManager.World then
    return
  end
  return CS.SceneManager.World:GetObjectByUuid(bUuid)
end

local GetBuildModelByPointId = function(pointId)
  if not CS.SceneManager.World then
    return
  end
  return CS.SceneManager.World:GetBuildingByPoint(pointId)
end

function BuildEffectManager.OnCityBuildOutView(bUuid)
  local self = DataCenter.BuildEffectManager
  self.OnBuildOutView(bUuid, self.cityAllEffect)
end

function BuildEffectManager.OnWorldBuildInView(bUuid)
  local self = DataCenter.BuildEffectManager
  local info = CS.SceneManager.World:GetPointInfoByUuid(bUuid)
  if not info then
    return
  end
  self:OnWorldBaseRefresh(info)
end

function BuildEffectManager:OnWorldBaseRefresh(info)
  local cityModel, worldPoint
  local bUuid = info.uuid
  if info ~= nil and info.PointType == WorldPointType.PlayerBuilding then
    local curTime = UITimeManager:GetInstance():GetServerTime()
    local aosEndTime = info.aosEndTime or 0
    if aosEndTime ~= 0 and curTime <= aosEndTime then
      return
    end
    if info:IsFrozen() then
      return
    end
  end
  worldPoint = self:GetWorldBuildModelByBUid(bUuid)
  if not worldPoint then
    return
  end
  cityModel = worldPoint:GetGameObject()
  if IsNull(cityModel) then
    return
  end
  local effectObj = self.worldAllEffect[bUuid]
  if effectObj then
    effectObj:SetData(bUuid, info, cityModel, worldPoint)
  else
    self.worldAllEffect[bUuid] = BuildEffectObj:New()
    self.worldAllEffect[bUuid]:SetData(bUuid, info, cityModel, worldPoint)
  end
end

function BuildEffectManager.OnWorldBuildOutView(bUuid)
  local self = DataCenter.BuildEffectManager
  self.OnBuildOutView(bUuid, self.worldAllEffect)
  if self.twEffect and self.twEffect.bUuid == bUuid then
    self.twEffect:Delete()
    self.twEffect = nil
  end
  if self.dominatorEffect and self.dominatorEffect.bUuid == bUuid then
    self.dominatorEffect:Delete()
    self.dominatorEffect = nil
  end
end

function BuildEffectManager.OnUserSkinUpdate(type)
  if type == DecorationType.DecorationType_Main_Effect or type == DecorationType.DecorationType_Main_City then
    local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.FUN_BUILD_MAIN)
    local self = DataCenter.BuildEffectManager
    self.OnCityBuildInView(buildData.uuid)
  end
  if type == DecorationType.DecorationType_TacticalWeapon then
    local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.LW_BUILD_TACTICAL_CENTER)
    if buildData == nil then
      return
    end
    local self = DataCenter.BuildEffectManager
    self.OnCityBuildInView(buildData.uuid)
  end
end

function BuildEffectManager.OnCityBuildInView(bUuid)
  local self = DataCenter.BuildEffectManager
  local buildData = DataCenter.BuildManager:GetBuildingDataByUuid(bUuid)
  local buildId
  if buildData then
    buildId = buildData.itemId
  end
  if buildId == BuildingTypes.FUN_BUILD_MAIN then
    if buildData:IsUpgradeFinish() then
      return
    end
    local mainEffectId = DataCenter.DecorationDataManager:GetCurrentSkinByType(DecorationType.DecorationType_Main_Effect)
    local template = DataCenter.DecorationTemplateManager:GetTemplate(mainEffectId)
    if template ~= nil then
      local cityObj = GetBuildModelByPointId(buildData.pointId)
      if cityObj == nil then
        return
      end
      local effectObj = self.cityAllEffect[bUuid]
      if effectObj then
        effectObj:SetData(bUuid, {effectId = mainEffectId}, cityObj)
      else
        self.cityAllEffect[bUuid] = BuildEffectObj:New()
        self.cityAllEffect[bUuid]:SetData(bUuid, {effectId = mainEffectId}, cityObj)
      end
    end
  elseif buildId == BuildingTypes.LW_BUILD_TACTICAL_CENTER then
    local templateId = DataCenter.TacticalWeaponManager:GetSelfWeaponAppearance()
    if templateId then
      local buildObj = GetBuildModelByPointId(buildData.pointId)
      if buildObj == nil then
        return
      end
      local effectObj = self.twEffect
      if effectObj then
        effectObj:SetData(bUuid, templateId, buildObj)
      else
        self.twEffect = TWModelObj:New()
        self.twEffect:SetData(bUuid, templateId, buildObj)
      end
    end
  elseif buildId == BuildingTypes.LW_BUILD_DOMINATOR_MAIN then
    local buildObj = GetBuildModelByPointId(buildData.pointId)
    if buildObj == nil then
      return
    end
    local effectObj = self.dominatorEffect
    if effectObj then
      effectObj:SetData(bUuid, buildObj)
    else
      self.dominatorEffect = LWDominatorCityBuilding:New()
      self.dominatorEffect:SetData(bUuid, buildObj)
    end
  elseif buildId == BuildingTypes.LW_BUILD_SEASON4_LIGHTHOUSE then
    DataCenter.SeasonPowerWorkerManager:SwitchBuildAnim(buildId, bUuid, buildData)
  elseif buildId == BuildingTypes.LW_BUILD_SEASON4_POWER_STATION1 or buildId == BuildingTypes.LW_BUILD_SEASON4_POWER_STATION2 or buildId == BuildingTypes.LW_BUILD_SEASON4_POWER_STATION3 or buildId == BuildingTypes.LW_BUILD_SEASON4_POWER_STATION4 then
    DataCenter.SeasonPowerWorkerManager:SwitchBuildAnim(buildId, bUuid, buildData)
  end
end

function BuildEffectManager.OnBuildOutView(bUuid, dic)
  if dic[bUuid] then
    dic[bUuid]:Delete()
    dic[bUuid] = nil
  end
end

function BuildEffectManager.OnDominatorAppearanceUpdate()
  local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.LW_BUILD_DOMINATOR_MAIN)
  if buildData == nil then
    return
  end
  local self = DataCenter.BuildEffectManager
  self.OnCityBuildInView(buildData.uuid)
end

return BuildEffectManager
