local AllianceBuildBloodManager = BaseClass("AllianceBuildBloodManager", Singleton)
local AllianceBuildBloodTip = require("Scene.AllianceBuildBloodTip.AllianceBuildBloodTip")
local ResourceManager = CS.GameEntry.Resource
local __init = function(self)
  self.allBloodSlider = {}
  self.tempInstance = {}
  self.isInWorld = false
  self:AddListener()
end
local __delete = function(self)
  self:RemoveAllEffect()
  self.allBloodSlider = nil
  self.tempInstance = nil
  self.isInWorld = nil
  self:RemoveListener()
end
local AddListener = function(self)
  EventManager:GetInstance():AddListener(EventId.PveLevelEnter, self.RemoveAllTips)
  EventManager:GetInstance():AddListener(EventId.ShowAllianceCitySoldierBlood, self.ShowAllianceCitySoldierBloodEffect)
  EventManager:GetInstance():AddListener(EventId.HideAllianceCitySoliderBlood, self.HideAllianceCitySoliderBloodEffect)
  EventManager:GetInstance():AddListener(EventId.ChangeCameraLod, self.ChangeCameraLodSignal)
  EventManager:GetInstance():AddListener(EventId.OnEnterWorld, self.OnEnterWorld)
  EventManager:GetInstance():AddListener(EventId.OnEnterCity, self.OnEnterCity)
end
local RemoveListener = function(self)
  EventManager:GetInstance():RemoveListener(EventId.PveLevelEnter, self.RemoveAllTips)
  EventManager:GetInstance():RemoveListener(EventId.ShowAllianceCitySoldierBlood, self.ShowAllianceCitySoldierBloodEffect)
  EventManager:GetInstance():RemoveListener(EventId.HideAllianceCitySoliderBlood, self.HideAllianceCitySoliderBloodEffect)
  EventManager:GetInstance():RemoveListener(EventId.ChangeCameraLod, self.ChangeCameraLodSignal)
  EventManager:GetInstance():RemoveListener(EventId.OnEnterWorld, self.OnEnterWorld)
  EventManager:GetInstance():RemoveListener(EventId.OnEnterCity, self.OnEnterCity)
end
local RemoveAllTips = function(data)
  AllianceBuildBloodManager:GetInstance():SetIsInWorld(false)
  AllianceBuildBloodManager:GetInstance():RemoveAllEffect()
end
local OnEnterWorld = function(data)
  AllianceBuildBloodManager:GetInstance():SetIsInWorld(true)
end
local OnEnterCity = function(data)
  AllianceBuildBloodManager:GetInstance():SetIsInWorld(false)
end
local SetIsInWorld = function(self, value)
  self.isInWorld = value
end
local RemoveAllEffect = function(self)
  for k, v in pairs(self.allBloodSlider) do
    local request = v.request
    v:OnDestroy()
    request:Destroy()
  end
  self.allBloodSlider = {}
  for k, v in pairs(self.tempInstance) do
    v:Destroy()
  end
  self.tempInstance = {}
end
local OnClickAllianceCity = function(self, bUuid, curSoldierNum, maxSoldierNum, maxDura)
  if self.allBloodSlider[bUuid] == nil and self.tempInstance[bUuid] == nil then
    local info = CS.SceneManager.World:GetPointInfoByUuid(bUuid)
    if info ~= nil then
      local allianceCityPointInfo = SeasonUtil.TryParseAllianceCityPointInfo(info.PointType, info.extraInfo, info)
      if allianceCityPointInfo ~= nil then
        local serverId = info.serverId
        local pointId = info.mainIndex
        local cityId = allianceCityPointInfo.cityId
        local cityTemplate = LocalController:instance():getLine(TableName.WorldCity, cityId)
        if cityTemplate ~= nil then
          local tileX = cityTemplate.size
          local tileY = cityTemplate.size
          local maxDurability = maxDura
          local durability = allianceCityPointInfo.durability
          local lastDurabilityTime = allianceCityPointInfo.lastDurabilityTime
          local cityRecoverSpeed = cityTemplate:getValue("wall_recover")
          local curTime = UITimeManager:GetInstance():GetServerSeconds()
          local addNum = (curTime - lastDurabilityTime) * tonumber(cityRecoverSpeed)
          local realDurabilityNum = durability + math.max(addNum, 0)
          local curNum = math.min(realDurabilityNum, maxDurability)
          local icon = cityTemplate:getValue("monster_icon")
          local spliteNum = cityTemplate:getValue("hp_army")
          local totalCount = math.floor(maxSoldierNum / spliteNum)
          AllianceBuildBloodManager:GetInstance():ShowOneBloodEffect(serverId, bUuid, cityId, pointId, curNum, curNum, maxDurability, curSoldierNum, maxSoldierNum, tileX, tileY, icon, totalCount)
        end
      end
    end
  elseif self.allBloodSlider[bUuid] ~= nil then
    self.allBloodSlider[bUuid]:SetIsOnClick()
  end
end
local OnCloseAllianceCity = function(self, bUuid)
  if self.allBloodSlider[bUuid] ~= nil then
    self.allBloodSlider[bUuid]:TryRemove()
  end
end
local OnSetBuildBlood = function(self, bUuid, curBlood, maxBlood)
  if self.allBloodSlider[bUuid] ~= nil then
    local pro = math.min(curBlood / maxBlood, 1)
    self.allBloodSlider[bUuid]:SetAllianceBlood(pro)
  end
end
local ShowOneBloodEffect = function(self, serverId, bUuid, cityId, pointId, startBlood, targetBlood, maxBlood, curSoldierNum, maxSoldierNum, tileX, tileY, icon, totalCount, isOnClick, isBroken)
  if self.isInWorld == false then
    return
  end
  if self.lodCache > 2 then
    return
  end
  if self.tempInstance[bUuid] == nil and self.allBloodSlider[bUuid] == nil then
    local request = ResourceManager:InstantiateAsync(UIAssets.SceneAllianceBuildBloodTip)
    self.tempInstance[bUuid] = request
    request:completed("+", function()
      self.tempInstance[bUuid] = nil
      if request.isError then
        return
      end
      request.gameObject:SetActive(true)
      request.gameObject.transform:SetParent(CS.SceneManager.World.DynamicObjNode)
      request.gameObject.transform:Set_localScale(ResetScale.x, ResetScale.y, ResetScale.z)
      local buildBloodTip = AllianceBuildBloodTip.New()
      buildBloodTip:OnCreate(request)
      local param = {}
      param.serverId = serverId
      param.pointId = pointId
      param.cityId = cityId
      param.soliderCurValue = curSoldierNum
      param.soldierValue = maxSoldierNum
      param.maxBlood = maxBlood
      param.bUuid = bUuid
      param.tileX = tileX
      param.tileY = tileY
      param.totalCount = totalCount
      param.icon = UIUtil.GetFullPath(LoadPath.HeroIconsSmallPath, icon)
      param.request = request
      buildBloodTip:StartShow(param)
      self.allBloodSlider[bUuid] = buildBloodTip
      if isOnClick ~= nil and isOnClick == true then
        self.allBloodSlider[bUuid]:SetIsOnClick()
      end
      if startBlood == targetBlood then
        local pro = math.min(startBlood / maxBlood, 1)
        self.allBloodSlider[bUuid]:SetAllianceBlood(pro)
      else
        self.allBloodSlider[bUuid]:StartShowBlood(startBlood, targetBlood, maxBlood, isBroken)
      end
    end)
  elseif self.allBloodSlider[bUuid] ~= nil then
    if isOnClick ~= nil and isOnClick == true then
      self.allBloodSlider[bUuid]:SetIsOnClick()
    end
    self.allBloodSlider[bUuid]:RefreshSoldierData(curSoldierNum, maxSoldierNum)
    if startBlood == targetBlood then
      local pro = math.min(startBlood / maxBlood, 1)
      self.allBloodSlider[bUuid]:SetAllianceBlood(pro)
    else
      self.allBloodSlider[bUuid]:StartShowBlood(startBlood, targetBlood, maxBlood, isBroken)
    end
  end
end
local RemoveOneEffect = function(self, bUuid)
  if self.allBloodSlider[bUuid] ~= nil then
    local request = self.allBloodSlider[bUuid].request
    self.allBloodSlider[bUuid]:OnDestroy()
    if request ~= nil then
      request:Destroy()
    end
  end
  self.allBloodSlider[bUuid] = nil
  if self.tempInstance[bUuid] ~= nil then
    self.tempInstance[bUuid]:Destroy()
  end
  self.tempInstance[bUuid] = nil
end
local ShowAllianceBloodEffect = function(self, message)
  if self.isInWorld == false then
    return
  end
  if message.cityUuid ~= nil then
    local bUuid = message.cityUuid
    local info = CS.SceneManager.World:GetPointInfoByUuid(bUuid)
    if info ~= nil and info ~= nil then
      local allianceCityPointInfo = SeasonUtil.TryParseAllianceCityPointInfo(info.PointType, info.extraInfo, info)
      if allianceCityPointInfo ~= nil then
        local serverId = info.serverId
        local pointId = info.mainIndex
        local startBlood = message.oldDurability
        local targetBlood = message.nowDurability
        if targetBlood ~= startBlood then
          local cityId = allianceCityPointInfo.cityId
          local cityTemplate = LocalController:instance():getLine(TableName.WorldCity, cityId)
          if cityTemplate ~= nil then
            local maxBlood = cityTemplate:getValue("wall")
            local cityBroken = message.cityBroken
            local icon = cityTemplate:getValue("monster_icon")
            local spliteNum = cityTemplate:getValue("hp_army")
            local totalCount = 0
            AllianceBuildBloodManager:GetInstance():ShowOneBloodEffect(serverId, bUuid, cityId, pointId, startBlood, targetBlood, maxBlood, 0, 1, cityTemplate.size, cityTemplate.size, icon, totalCount, false, cityBroken)
          end
        end
      end
    end
  end
end
local ShowAllianceCitySoldierBloodEffect = function(data)
  if data ~= nil then
    local dataStr = string.split(data, ";")
    if 2 < #dataStr then
      local bUuid = tonumber(dataStr[1])
      local curBlood = tonumber(dataStr[2])
      local maxBlood = tonumber(dataStr[3])
      AllianceBuildBloodManager:GetInstance():ShowCitySoldierBlood(bUuid, curBlood, maxBlood)
    end
  end
end
local HideAllianceCitySoliderBloodEffect = function(data)
  if data ~= nil then
    AllianceBuildBloodManager:GetInstance():RemoveCitySoldierEffect(tonumber(data))
  end
end
local ShowCitySoldierBlood = function(self, bUuid, curBlood, maxBlood)
  if self.isInWorld == false then
    return
  end
  if self.allBloodSlider[bUuid] == nil and self.tempInstance[bUuid] == nil then
    local info = CS.SceneManager.World:GetPointInfoByUuid(bUuid)
    if info ~= nil then
      local allianceCityPointInfo = SeasonUtil.TryParseAllianceCityPointInfo(info.PointType, info.extraInfo, info)
      if allianceCityPointInfo ~= nil then
        local serverId = info.serverId
        local pointId = info.mainIndex
        local cityId = allianceCityPointInfo.cityId
        local cityTemplate = LocalController:instance():getLine(TableName.WorldCity, cityId)
        if cityTemplate ~= nil then
          local tileX = cityTemplate.size
          local tileY = cityTemplate.size
          local maxDurability = cityTemplate:getValue("wall")
          local durability = allianceCityPointInfo.durability
          local lastDurabilityTime = allianceCityPointInfo.lastDurabilityTime
          local cityRecoverSpeed = cityTemplate:getValue("wall_recover")
          local curTime = UITimeManager:GetInstance():GetServerSeconds()
          local addNum = (curTime - lastDurabilityTime) * tonumber(cityRecoverSpeed)
          local realDurabilityNum = durability + math.max(addNum, 0)
          local curNum = math.min(realDurabilityNum, maxDurability)
          local icon = cityTemplate:getValue("monster_icon")
          local spliteNum = cityTemplate:getValue("hp_army")
          local totalCount = math.floor(maxBlood / spliteNum)
          AllianceBuildBloodManager:GetInstance():ShowOneBloodEffect(serverId, bUuid, cityId, pointId, curNum, curNum, maxDurability, curBlood, maxBlood, tileX, tileY, icon, totalCount)
        end
      end
    end
  elseif self.allBloodSlider[bUuid] ~= nil then
    self.allBloodSlider[bUuid]:RefreshSoldierData(curBlood, maxBlood)
  end
end
local RemoveCitySoldierEffect = function(self, bUuid)
  if self.allBloodSlider[bUuid] ~= nil then
    self.allBloodSlider[bUuid]:CheckRemove()
  end
end
local ChangeCameraLodSignal = function(lod)
  AllianceBuildBloodManager:GetInstance():UpdateLod(lod)
end
local UpdateLod = function(self, lod)
  if self.lodCache ~= lod then
    self.lodCache = lod
    if self.lodCache > 2 then
      local list = table.keys(self.allBloodSlider)
      for k, v in pairs(list) do
        self:RemoveOneEffect(v)
      end
    end
  end
end
AllianceBuildBloodManager.__init = __init
AllianceBuildBloodManager.__delete = __delete
AllianceBuildBloodManager.RemoveOneEffect = RemoveOneEffect
AllianceBuildBloodManager.ShowOneBloodEffect = ShowOneBloodEffect
AllianceBuildBloodManager.OnClickAllianceCity = OnClickAllianceCity
AllianceBuildBloodManager.OnCloseAllianceCity = OnCloseAllianceCity
AllianceBuildBloodManager.OnSetBuildBlood = OnSetBuildBlood
AllianceBuildBloodManager.ShowAllianceBloodEffect = ShowAllianceBloodEffect
AllianceBuildBloodManager.ShowCitySoldierBlood = ShowCitySoldierBlood
AllianceBuildBloodManager.RemoveCitySoldierEffect = RemoveCitySoldierEffect
AllianceBuildBloodManager.ShowAllianceCitySoldierBloodEffect = ShowAllianceCitySoldierBloodEffect
AllianceBuildBloodManager.HideAllianceCitySoliderBloodEffect = HideAllianceCitySoliderBloodEffect
AllianceBuildBloodManager.AddListener = AddListener
AllianceBuildBloodManager.RemoveListener = RemoveListener
AllianceBuildBloodManager.ChangeCameraLodSignal = ChangeCameraLodSignal
AllianceBuildBloodManager.UpdateLod = UpdateLod
AllianceBuildBloodManager.RemoveAllEffect = RemoveAllEffect
AllianceBuildBloodManager.RemoveAllTips = RemoveAllTips
AllianceBuildBloodManager.OnEnterWorld = OnEnterWorld
AllianceBuildBloodManager.OnEnterCity = OnEnterCity
AllianceBuildBloodManager.SetIsInWorld = SetIsInWorld
return AllianceBuildBloodManager
