local AllianceCityTipManager = BaseClass("AllianceCityTipManager")
local AllianceCityTip = require("DataCenter.AllianceCityTip.AllianceCityTip")
local ResourceManager = CS.GameEntry.Resource
local Fire_Prefab = {
  "Assets/Main/Prefabs/World/Eff_dafuw_fire_da.prefab",
  "Assets/Main/Prefabs/World/Eff_dafuw_fire_fanwei.prefab",
  "Assets/Main/Prefabs/World/Eff_dafuw_fire_zhong.prefab",
  "Assets/Main/Prefabs/World/Eff_daditu_zhucheng_fire.prefab"
}
local __init = function(self)
  self.lodCache = 1
  self.bloodyNightFireEffect = nil
  self.protectTimeDict = {}
  self.tipDict = {}
  self.notShowTipFlag = {}
  self.fireReqs = {}
  self.gunnerAtkObj = {}
  self.gunnerAtkReq = {}
  self.zoneInView = {}
  self:AddListeners()
end
local __delete = function(self)
  if self.bloodyNightFireEffect then
    self.bloodyNightFireEffect:Delete()
    self.bloodyNightFireEffect = nil
  end
  if self.fireReqs then
    for _, v in pairs(self.fireReqs) do
      for _, req in pairs(v) do
        req:Destroy()
      end
    end
  end
  self.zoneInView = {}
  self.fireReqs = {}
  if self.tipDict ~= nil then
    for _, t in pairs(self.tipDict) do
      if t.tip ~= nil then
        t.tip:OnDestroy()
      end
      if t.req ~= nil then
        t.req:Destroy()
      end
    end
    self.tipDict = nil
  end
  self.notShowTipFlag = nil
  for i, v in ipairs(self.gunnerAtkReq) do
    v:Destroy()
  end
  self.gunnerAtkReq = nil
  self.gunnerAtkObj = nil
  self:RemoveListeners()
end
local Startup = function(self)
end
local RemoveAllAllianceCityTip = function(self)
  if self.tipDict ~= nil then
    for _, t in pairs(self.tipDict) do
      if t.tip ~= nil then
        t.tip:OnDestroy()
      end
      if t.req ~= nil then
        t.req:Destroy()
      end
    end
    self.tipDict = {}
  end
  if self.bloodyNightFireEffect then
    self.bloodyNightFireEffect:Delete()
    self.bloodyNightFireEffect = nil
  end
  self.zoneInView = {}
end
local AddListeners = function(self)
  EventManager:GetInstance():AddListener(EventId.ActNuclearScoreUpdate, self.OnNuclearScoreUpdate)
  EventManager:GetInstance():AddListener(EventId.CityDomeShow, self.OnPointDateUpdate)
  EventManager:GetInstance():AddListener(EventId.UPDATE_CITY_POINTS_DATA, self.OnPointDateUpdate)
  EventManager:GetInstance():AddListener(EventId.ChangeCameraLod, self.ChangeCameraLodSignal)
  EventManager:GetInstance():AddListener(EventId.AllianceCityInView, self.AllianceCityInViewSignal)
  EventManager:GetInstance():AddListener(EventId.AllianceCityOutView, self.AllianceCityOutViewSignal)
  EventManager:GetInstance():AddListener(EventId.WorldCityOwnerInfoChanged, self.AllianceCityNameChange)
  EventManager:GetInstance():AddListener(EventId.OnActBossDataRefresh, self.ShowActBossIconShow)
  EventManager:GetInstance():AddListener(EventId.DeclareWar, self.AllianceCityRefreshDeclare)
  EventManager:GetInstance():AddListener(EventId.KingOccupyProgressRefresh, self.OnKingOccupyProgressRefresh)
  EventManager:GetInstance():AddListener(EventId.GovernmentPresidentRefresh, self.OnGovernmentPresidentRefresh)
  EventManager:GetInstance():AddListener(EventId.WorldAllianceCityDetail, self.OnWorldAllianceCityDetail)
  EventManager:GetInstance():AddListener(EventId.StrongholdBattleStateUpdate, self.OnStrongholdBattleStateUpdate)
  EventManager:GetInstance():AddListener(EventId.UpdateAllServerTradeInfo, self.OnTradePointDateUpdate)
  EventManager:GetInstance():AddListener(EventId.CityGhostCreateFinish, self.OnCityGhostUpdate)
  EventManager:GetInstance():AddListener(EventId.CityGhostHideFinish, self.OnCityGhostUpdate)
  EventManager:GetInstance():AddListener(EventId.BloodyNightActivityRefresh, self.OnBloodyNightUpdate)
  EventManager:GetInstance():AddListener(EventId.CityBattleS1RestUpdateFirstInfo, self.OnCityBattleS1RestUpdateFirstInfo)
  EventManager:GetInstance():AddListener(EventId.ShowCityGuideNode, self.ShowCityGuideNode)
end
local RemoveListeners = function(self)
  EventManager:GetInstance():RemoveListener(EventId.ActNuclearScoreUpdate, self.OnNuclearScoreUpdate)
  EventManager:GetInstance():RemoveListener(EventId.CityDomeShow, self.OnPointDateUpdate)
  EventManager:GetInstance():RemoveListener(EventId.UPDATE_CITY_POINTS_DATA, self.OnPointDateUpdate)
  EventManager:GetInstance():RemoveListener(EventId.ChangeCameraLod, self.ChangeCameraLodSignal)
  EventManager:GetInstance():RemoveListener(EventId.AllianceCityInView, self.AllianceCityInViewSignal)
  EventManager:GetInstance():RemoveListener(EventId.AllianceCityOutView, self.AllianceCityOutViewSignal)
  EventManager:GetInstance():RemoveListener(EventId.WorldCityOwnerInfoChanged, self.AllianceCityNameChange)
  EventManager:GetInstance():RemoveListener(EventId.OnActBossDataRefresh, self.ShowActBossIconShow)
  EventManager:GetInstance():RemoveListener(EventId.DeclareWar, self.AllianceCityRefreshDeclare)
  EventManager:GetInstance():RemoveListener(EventId.KingOccupyProgressRefresh, self.OnKingOccupyProgressRefresh)
  EventManager:GetInstance():RemoveListener(EventId.GovernmentPresidentRefresh, self.OnGovernmentPresidentRefresh)
  EventManager:GetInstance():RemoveListener(EventId.WorldAllianceCityDetail, self.OnWorldAllianceCityDetail)
  EventManager:GetInstance():RemoveListener(EventId.StrongholdBattleStateUpdate, self.OnStrongholdBattleStateUpdate)
  EventManager:GetInstance():RemoveListener(EventId.UpdateAllServerTradeInfo, self.OnTradePointDateUpdate)
  EventManager:GetInstance():RemoveListener(EventId.CityGhostCreateFinish, self.OnCityGhostUpdate)
  EventManager:GetInstance():RemoveListener(EventId.CityGhostHideFinish, self.OnCityGhostUpdate)
  EventManager:GetInstance():RemoveListener(EventId.BloodyNightActivityRefresh, self.OnBloodyNightUpdate)
  EventManager:GetInstance():RemoveListener(EventId.CityBattleS1RestUpdateFirstInfo, self.OnCityBattleS1RestUpdateFirstInfo)
  EventManager:GetInstance():RemoveListener(EventId.ShowCityGuideNode, self.ShowCityGuideNode)
end

function AllianceCityTipManager.OnCityGhostUpdate(cityId)
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict ~= nil then
    local theCityTip = tipDict[toInt(cityId)]
    if theCityTip ~= nil and theCityTip.tip ~= nil then
      theCityTip.tip:TryUpdatePosition()
    end
  end
end

function AllianceCityTipManager.OnNuclearScoreUpdate()
  if SeasonUtil.GetSeasonType() == SeasonMapType.Snow and SceneUtils.GetIsInWorld() then
    local theWorld = CS.SceneManager.World
    if theWorld == nil then
      return
    end
    local pointInfo = theWorld:GetPointInfo(THRONE_POINT_ID)
    if pointInfo == nil then
      return
    end
    local obj = theWorld:GetObjectByPoint(THRONE_POINT_ID)
    if obj == nil then
      return
    end
    local gameObject = obj:GetGameObject()
    if gameObject == nil then
      return
    end
    local eff_path = "Model/S2saiji_hefanyin/hefanyin/Eff_build_s_hefanyin"
    local effectRoot = gameObject.transform:Find(eff_path)
    if effectRoot ~= nil then
      local serverId = LuaEntry.Player:GetCurServerId()
      local data = SeasonUtil.GetSeasonInfo(serverId)
      if data and data:InHaltMode() then
        effectRoot.gameObject:SetActive(true)
      elseif data and data:ServerInReady() and data:InNormalMode() and DataCenter.SeasonNuclearPowerPlantDataManager:BuildNuclearPowerBtnOpen() then
        local max = DataCenter.SeasonNuclearPowerPlantDataManager:GetScoreMax()
        local actNuclearScore = DataCenter.WorldAllianceCityDataManager:GetThroneNuclearScore(serverId)
        effectRoot.gameObject:SetActive(max <= actNuclearScore and LuaEntry.Player:AtHomeNow())
      else
        effectRoot.gameObject:SetActive(false)
      end
    end
  end
end

function AllianceCityTipManager:OnStrongholdBattleStateUpdate()
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict then
    for k, v in pairs(tipDict) do
      if v.tip ~= nil then
        v.tip:OnStrongholdBattleStateUpdate()
      end
    end
  end
end

function AllianceCityTipManager.ShowCityGuideNode(cityId)
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict ~= nil then
    for k, v in pairs(tipDict) do
      if v ~= nil and v.meta ~= nil and v.meta.id == cityId then
        if v.tip ~= nil then
          v.tip:ShowCityGuideNode()
        else
        end
        return
      end
    end
  end
end

function AllianceCityTipManager.OnPointDateUpdate(pointIndex)
  ProfilerUtil.BeginSample("AllianceCityTipManager.OnPointDateUpdate")
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict then
    for k, v in pairs(tipDict) do
      if v.tip ~= nil then
        if pointIndex == nil then
          v.tip:OnPointDateUpdate()
        elseif v.tip.pointId == pointIndex then
          v.tip:OnPointDateUpdate()
        end
      end
    end
  end
  if pointIndex == THRONE_POINT_ID and SeasonUtil.GetSeasonType() == SeasonMapType.Snow then
    DataCenter.AllianceCityTipManager:OnNuclearScoreUpdate()
  end
  ProfilerUtil.EndSample()
end

function AllianceCityTipManager.OnTradePointDateUpdate()
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict then
    for k, v in pairs(tipDict) do
      if v.tip ~= nil and v.tip.data and v.tip.data:IsTradingStation() then
        v.tip:OnPointDateUpdate()
      end
    end
  end
end

function AllianceCityTipManager:OnWorldAllianceCityDetail()
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict ~= nil then
    for k, v in pairs(tipDict) do
      if v.tip ~= nil then
        v.tip:OnWorldAllianceCityDetail()
      end
    end
  end
end

function AllianceCityTipManager:OnGovernmentPresidentRefresh()
  DataCenter.AllianceCityTipManager:OnKingOccupyProgressRefresh()
  DataCenter.AllianceCityTipManager:AllianceCityNameChange()
end

function AllianceCityTipManager:OnKingOccupyProgressRefresh()
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict ~= nil then
    for k, v in pairs(tipDict) do
      if v ~= nil and v.meta ~= nil and v.tip ~= nil and v.type == WorldAllianceCityType.King then
        v.tip:OnKingOccupyProgressRefresh()
      end
    end
  end
end

local ChangeCameraLodSignal = function(lod)
  DataCenter.AllianceCityTipManager:UpdateLod(lod)
end
local ShowActBossIconShow = function(data)
  DataCenter.AllianceCityTipManager:CheckAllianceCityTipsShowBoss()
end
local CheckAllianceCityTipsShowBoss = function(self)
  for k, v in pairs(self.tipDict) do
    if v.tip ~= nil then
      v.tip:CheckBossData()
    end
  end
end
local UpdateTipLod = function(self, lod)
  for k, v in pairs(self.tipDict) do
    if v.tip ~= nil then
      v.tip:CheckLod(lod)
    end
  end
end
local UpdateLod = function(self, lod)
  if self.lodCache ~= lod then
    if self.lodCache == 8 then
      self.lodCache = lod
      for cityId, shown in pairs(self.zoneInView) do
        self:ShowTip(cityId, shown)
      end
    end
    self.lodCache = lod
    if 7 <= lod then
      self:SendRequest()
    end
    self:UpdateTipLod(lod)
  end
  if self.bloodyNightFireEffect then
    self.bloodyNightFireEffect:UpdateLod(lod)
  end
end
local SendRequest = function(self)
  local curTime = UITimeManager:GetInstance():GetServerTime()
  local dataList = DataCenter.ActivityListDataManager:GetActivityDataByType(EnumActivity.WorldBoss.Type)
  if 0 < #dataList then
    local data = dataList[1]
    if curTime >= data.startTime and curTime <= data.endTime then
      SFSNetwork.SendMessage(MsgDefines.UserGetActBossMarch)
    end
  end
end
local AllianceCityNameChange = function(data)
  DataCenter.AllianceCityTipManager:RefreshAllianceCityName()
end
local RefreshAllianceCityName = function(self)
  for k, v in pairs(self.tipDict) do
    if v.tip ~= nil then
      v.tip:SetName(k)
      v.tip:SetAllianceColor(k)
    end
  end
end
local SetCanShowFlag = function(self, id, canShow)
  if canShow then
    if self.notShowTipFlag[id] ~= nil then
      self.notShowTipFlag[id] = nil
      DataCenter.AllianceCityTipManager:ShowTip(id, true)
    end
  elseif self.notShowTipFlag[id] == nil then
    self.notShowTipFlag[id] = true
    DataCenter.AllianceCityTipManager:ShowTip(id, false)
  end
end
local AllianceCityInViewSignal = function(id)
  local curServerId = LuaEntry.Player:GetCurServerId()
  local pThis = DataCenter.AllianceCityTipManager
  if SeasonUtil.IsKingCity(id, curServerId) then
    local seasonInfo = SeasonUtil.GetSeasonInfo(curServerId)
    local template = DataCenter.AllianceCityTemplateManager:GetTemplate(id, curServerId)
    local serverId = template:GetCurServerId()
    local seasonType = SeasonMapType.Nothing
    local seasonMode = 0
    if seasonInfo ~= nil then
      seasonMode = seasonInfo.mode
      seasonType = seasonInfo:GetServerType(false)
    end
    local batteryList = DataCenter.AllianceCityTemplateManager:GetThroneCityBatteryList(serverId)
    if seasonType == SeasonMapType.Darkness then
      if seasonMode == 1 then
        for _, v in ipairs(batteryList) do
          if v ~= nil and v.type == WorldAllianceCityType.Canon and v.size == 9 then
            pThis:ShowTip(v.id, true, v)
          end
        end
      else
        for _, v in ipairs(batteryList) do
          if v ~= nil and v.type == WorldAllianceCityType.Canon and v.size == 3 then
            pThis:ShowTip(v.id, true, v)
          end
        end
      end
    else
      for _, v in ipairs(batteryList) do
        if v ~= nil and v.type == WorldAllianceCityType.Canon then
          pThis:ShowTip(v.id, true, v)
        end
      end
    end
    local missileList = DataCenter.AllianceCityTemplateManager:GetMissileFactoryList(serverId)
    for _, v in ipairs(missileList) do
      if v ~= nil and v.type == WorldAllianceCityType.MissileFactory then
        pThis:ShowTip(v.id, true, v)
      end
    end
    if seasonMode == 1 and seasonType == SeasonMapType.Snow and DataCenter.SeasonNuclearPowerPlantDataManager:BuildNuclearPowerBtnOpen() then
      SFSNetwork.SendMessage(MsgDefines.NuclearServerScoreView, serverId)
    end
  end
  pThis.zoneInView[id] = true
  pThis:ShowTip(id, true)
  AllianceCityTipManager.OnBloodyNightUpdate()
end

function AllianceCityTipManager.OnBloodyNightUpdate()
  local pThis = DataCenter.AllianceCityTipManager
  if pThis.isInSeason == nil or pThis.seasonType == nil then
    pThis.isInSeason = SeasonUtil.IsInSeason(true)
    pThis.seasonType = SeasonUtil.GetSeasonType()
  end
  if pThis.bloodyNightFireEffect == nil and pThis.isInSeason and pThis.seasonType == SeasonMapType.Darkness and SceneUtils.GetIsInWorld() then
    local isBloodyNight = DataCenter.BloodyNightDataManager:IsBloodyNight()
    if isBloodyNight then
      local theWorld = CS.SceneManager.World
      if theWorld and theWorld.DynamicObjNode then
        local theAsyncNode = require("UI.LWSeason4.Component.BloodyNightFireEffect")
        local effectPath = "Assets/Main/SeasonRes/S4/Prefabs/Effect/BloodyNight/Eff_ljw_s4_xueye_scene_loop.prefab"
        pThis.bloodyNightFireEffect = theAsyncNode.New("bloodyNightFire", theWorld.DynamicObjNode.transform, effectPath)
      end
    end
  end
end

local AllianceCityOutViewSignal = function(id)
  local pThis = DataCenter.AllianceCityTipManager
  local curServerId = LuaEntry.Player:GetCurServerId()
  if SeasonUtil.IsKingCity(id, curServerId) then
    local template = DataCenter.AllianceCityTemplateManager:GetTemplate(id, curServerId)
    local serverId = template:GetCurServerId()
    local batteryList = DataCenter.AllianceCityTemplateManager:GetThroneCityBatteryList(serverId)
    for _, v in ipairs(batteryList) do
      if v ~= nil and v.type == WorldAllianceCityType.Canon then
        DataCenter.AllianceCityTipManager:ShowTip(v.id, false, v)
      end
    end
    local missileList = DataCenter.AllianceCityTemplateManager:GetMissileFactoryList(serverId)
    for _, v in ipairs(missileList) do
      if v ~= nil and v.type == WorldAllianceCityType.MissileFactory then
        DataCenter.AllianceCityTipManager:ShowTip(v.id, false, v)
      end
    end
  end
  pThis.zoneInView[id] = false
  DataCenter.AllianceCityTipManager:ShowTip(id, false)
end
local AllianceCityRefreshDeclare = function()
  DataCenter.AllianceCityTipManager:RefreshDeclare()
end
local RefreshDeclare = function(self)
  for k, v in pairs(self.tipDict) do
    if v.tip ~= nil then
      v.tip:CheckCityDeclare()
    end
  end
end
local ShowTip = function(self, id, show, data)
  if show and self.tipDict[id] == nil and self.notShowTipFlag[id] == nil then
    self:CreateTip(id, data)
  elseif not show and self.tipDict[id] ~= nil then
    self:DestroyTip(id)
  end
end
local CreateTip = function(self, id, data)
  if BattleFieldUtil.InBattleField() then
    return
  end
  local serverId = LuaEntry.Player:GetCurServerId()
  local isBattle = SeasonUtil.CityIsInBattle(serverId, id)
  if not isBattle and self.lodCache ~= nil and self.lodCache > 7 and not SeasonUtil.IsKingCity(id, serverId) and not SeasonUtil.IsOutpost(id, serverId) then
    return
  end
  local check_data = DataCenter.AllianceCityTemplateManager:GetTemplate(id, serverId)
  if check_data == nil or check_data.type == nil then
    return
  end
  local city_type = check_data.type
  local request = ResourceManager:InstantiateAsync(UIAssets.AllianceCityTip)
  request:completed("+", function()
    if request.isError then
      return
    end
    if CS.SceneManager.World == nil or not CS.SceneManager:IsInWorld() then
      request:Destroy()
      return
    end
    local template = check_data
    if template then
      local go = request.gameObject
      go:SetActive(true)
      go.transform:SetParent(CS.SceneManager.World.DynamicObjNode)
      if template.type == WorldAllianceCityType.Stronghold then
        go.transform:Set_localScale(ResetScale.x, ResetScale.y, ResetScale.z)
      else
        go.transform:Set_localScale(ResetScale.x * 0.75, ResetScale.y * 0.75, ResetScale.z * 0.75)
      end
      if CS.CommonUtils.IsDebug() and CS.UnityEngine.Application.isEditor then
        go.name = "AllianceCityTip_" .. id
      end
      local autoFace = go:GetComponent(typeof(CS.AutoFaceToCamera))
      local adjustScales = go:GetComponent(typeof(CS.AutoAdjustScale))
      local adjustLod = go:GetComponent(typeof(CS.AutoAdjustLod))
      if not IsNull(autoFace) then
        autoFace.enabled = true
      end
      if not IsNull(adjustScales) then
        adjustScales.enabled = true
      end
      if not IsNull(adjustLod) then
        adjustLod.enabled = true
      end
      local tip = AllianceCityTip.New()
      tip:OnCreate(request)
      tip:SetLod(self.lodCache)
      tip:SetData(template, self.lodCache)
      self.tipDict[id] = {
        tip = tip,
        req = request,
        meta = template,
        type = city_type
      }
    end
  end)
  self.tipDict[id] = {
    tip = nil,
    req = request,
    meta = check_data,
    type = city_type
  }
end
local DestroyTip = function(self, id)
  local t = self.tipDict[id]
  if t ~= nil then
    if t.tip ~= nil then
      t.tip:OnDestroy()
    end
    if t.req ~= nil then
      t.req:Destroy()
    end
  end
  self.tipDict[id] = nil
end

function AllianceCityTipManager:AllianceCityRefreshFire(cityId, transform)
  local template = DataCenter.AllianceCityTemplateManager:GetTemplate(cityId, LuaEntry.Player:GetCurServerId())
  if not template then
    return
  end
  local info = template:GetPointInfo()
  if not info then
    return
  end
  local allianceCityPointInfo = PBController.ParsePbFromBytes(info.extraInfo, "protobuf.AllianceCityPointInfo")
  if allianceCityPointInfo == nil then
    return
  end
  if template:IsThroneCity() then
    local curTime = UITimeManager:GetInstance():GetServerSeconds()
    local timeOpen = allianceCityPointInfo.openTime
    local timeEnd = allianceCityPointInfo.protectTime
    if curTime < timeOpen or curTime >= timeEnd then
      return
    end
  end
  local maxDurability = template.wall
  local durability = allianceCityPointInfo.durability
  local lastDurabilityTime = allianceCityPointInfo.lastDurabilityTime or 0
  local cityRecoverSpeed = template.wall_recover or 0
  local curTime = UITimeManager:GetInstance():GetServerSeconds()
  local addNum = (curTime - lastDurabilityTime) * tonumber(cityRecoverSpeed)
  local realDurabilityNum = durability + math.max(addNum, 0)
  local curDurability = math.min(realDurabilityNum, maxDurability)
  local fireNum, maxRadius = DataCenter.WorldAllianceCityDataManager:GetFireNumByBlood(curDurability, maxDurability)
  if not self.fireReqs[cityId] then
    self.fireReqs[cityId] = {}
  end
  if #self.fireReqs[cityId] == fireNum then
    return
  end
  local parent = transform:Find("Model")
  if IsNull(parent) then
    return
  end
  if fireNum > #self.fireReqs[cityId] then
    for i = #self.fireReqs[cityId] + 1, fireNum do
      local path = Fire_Prefab[math.random(4)]
      self.fireReqs[cityId][i] = ResourceManager:InstantiateAsync(path)
      self.fireReqs[cityId][i]:completed("+", function(req)
        local go = req.gameObject
        if IsNull(go) then
          return
        end
        go:SetActive(true)
        local trans = go.transform
        trans:SetParent(parent)
        trans:Set_localScale(ResetScale.x, ResetScale.y, ResetScale.z)
        local x = maxRadius * math.random() - maxRadius * 0.5
        local y = maxRadius * math.random() - maxRadius * 0.5
        local newX = 0.7 * x - 0.7 * y
        local newY = 0.7 * x + 0.7 * y
        trans:Set_localPosition(newX, newY, newY)
      end)
    end
  elseif fireNum < #self.fireReqs[cityId] then
    for i = #self.fireReqs[cityId], fireNum + 1, -1 do
      self.fireReqs[cityId][i]:Destroy()
      table.remove(self.fireReqs[cityId], i)
    end
  end
end

function AllianceCityTipManager:AllianceCityRemoveFire(cityId)
  if self.fireReqs[cityId] then
    for _, v in pairs(self.fireReqs[cityId]) do
      v:Destroy()
    end
    self.fireReqs[cityId] = {}
  end
end

function AllianceCityTipManager:ShowBloodQueenGunnerAttackEffect(cityId, transform)
  if self.gunnerAtkObj[cityId] == nil then
    if self.gunnerAtkReq[cityId] == nil then
      local path = "Assets/_Art_LastWar/Effect/Prefab/Common/D_lanpao_hit_big.prefab"
      local parent = transform:Find("Model")
      if IsNull(parent) then
        return
      end
      local request = ResourceManager:InstantiateAsync(path)
      request:completed("+", function(req)
        local go = req.gameObject
        if IsNull(go) then
          return
        end
        local trans = go.transform
        trans:SetParent(parent)
        local scale = 3.3
        trans:Set_localScale(scale, scale, scale)
        trans:Set_localPosition(0, 0, 0)
        self.gunnerAtkObj[cityId] = go
      end)
      self.gunnerAtkReq[cityId] = request
    end
  else
    self.gunnerAtkObj[cityId]:SetActive(false)
    self.gunnerAtkObj[cityId]:SetActive(true)
  end
end

function AllianceCityTipManager:RemoveBloodQueenGunnerAttackEffect(cityId)
  if self.gunnerAtkReq[cityId] then
    self.gunnerAtkReq[cityId]:Destroy()
    self.gunnerAtkReq[cityId] = nil
  end
  if self.gunnerAtkObj[cityId] then
    self.gunnerAtkObj[cityId] = nil
  end
end

function AllianceCityTipManager:ShowBloodQueenButcherExplodeTip(totalDamage, pointId)
  local pointInfo = CS.SceneManager.World:GetPointInfo(pointId)
  if pointInfo == nil then
    return
  end
  local cityId = pointInfo.cityId or pointInfo.CityId
  if cityId == nil then
    return
  end
  if self.tipDict and self.tipDict[cityId] then
    local tip = self.tipDict[cityId].tip
    if tip then
      tip:ShowBloodQueenButcherExplodeTip(totalDamage)
    end
  end
end

function AllianceCityTipManager:ShowBloodQueenHpChangeTip(totalDamage, cityId)
  if self.tipDict and self.tipDict[cityId] then
    local tip = self.tipDict[cityId].tip
    if tip then
      tip:ShowBloodQueenHpChangeTip(totalDamage)
    end
  end
end

function AllianceCityTipManager:UpdateProtectedTime(serverId, protectDict)
  local data = self.protectTimeDict[serverId]
  if data == nil then
    data = protectDict
    self.protectTimeDict[serverId] = data
  else
    for k, v in pairs(protectDict) do
      data[k] = v
    end
  end
  for k, v in pairs(self.tipDict) do
    if v.tip ~= nil and v.tip.cityId ~= nil then
      local openTime = data[v.tip.cityId]
      if openTime then
        v.tip:UpdateProtectedTime(openTime)
      end
    end
  end
end

function AllianceCityTipManager:SetProtectedTime(serverId, cityId, protectTime)
  local protectDict = self.protectTimeDict[serverId]
  if protectDict then
    protectDict[cityId] = protectTime
  else
    local data = {}
    data[cityId] = protectTime
    self.protectTimeDict[serverId] = data
  end
end

function AllianceCityTipManager:GetProtectedTime(serverId, cityId)
  local protectDict = self.protectTimeDict[serverId]
  if protectDict then
    return protectDict[cityId] or 0
  end
  return 0
end

function AllianceCityTipManager:OnCityBattleS1RestUpdateFirstInfo()
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict then
    for k, v in pairs(tipDict) do
      if v.tip ~= nil and v.tip.data and v.tip.data:IsCity() then
        v.tip:OnPointDateUpdate()
      end
    end
  end
end

function AllianceCityTipManager:OnCitySkinColorSettingChanged()
  local tipDict = DataCenter.AllianceCityTipManager.tipDict
  if tipDict then
    for k, v in pairs(tipDict) do
      if v.tip ~= nil then
        v.tip:OnCitySkinColorSettingChanged()
      end
    end
  end
end

AllianceCityTipManager.__init = __init
AllianceCityTipManager.__delete = __delete
AllianceCityTipManager.Startup = Startup
AllianceCityTipManager.RemoveAllAllianceCityTip = RemoveAllAllianceCityTip
AllianceCityTipManager.AddListeners = AddListeners
AllianceCityTipManager.RemoveListeners = RemoveListeners
AllianceCityTipManager.ChangeCameraLodSignal = ChangeCameraLodSignal
AllianceCityTipManager.AllianceCityInViewSignal = AllianceCityInViewSignal
AllianceCityTipManager.AllianceCityOutViewSignal = AllianceCityOutViewSignal
AllianceCityTipManager.ShowTip = ShowTip
AllianceCityTipManager.CreateTip = CreateTip
AllianceCityTipManager.DestroyTip = DestroyTip
AllianceCityTipManager.SetCanShowFlag = SetCanShowFlag
AllianceCityTipManager.RefreshAllianceCityName = RefreshAllianceCityName
AllianceCityTipManager.AllianceCityNameChange = AllianceCityNameChange
AllianceCityTipManager.ShowActBossIconShow = ShowActBossIconShow
AllianceCityTipManager.CheckAllianceCityTipsShowBoss = CheckAllianceCityTipsShowBoss
AllianceCityTipManager.UpdateTipLod = UpdateTipLod
AllianceCityTipManager.SendRequest = SendRequest
AllianceCityTipManager.UpdateLod = UpdateLod
AllianceCityTipManager.AllianceCityRefreshDeclare = AllianceCityRefreshDeclare
AllianceCityTipManager.RefreshDeclare = RefreshDeclare
return AllianceCityTipManager
