local BuildDestroyEffectManager = BaseClass("BuildDestroyEffectManager", Singleton)
local ResourceManager = CS.GameEntry.Resource
local BuildDestroyEffect = require("Scene.BuildDestroyEffect.BuildDestroyEffect")
local __init = function(self)
  self.allEffect = {}
  self.OnCreateEffect = {}
  self:AddListener()
end
local __delete = function(self)
  for k, v in pairs(self.allEffect) do
    local request = v.request
    v.script:OnDestroy()
    request:Destroy()
  end
  for k, v in pairs(self.OnCreateEffect) do
    if v ~= nil then
      v:Destroy()
    end
  end
  self.allEffect = nil
  self.OnCreateEffect = nil
  self:RemoveListener()
end
local AddListener = function(self)
  EventManager:GetInstance():AddListener(EventId.ShowBuildDestroyEffect, self.BuildInViewSignal)
  EventManager:GetInstance():AddListener(EventId.BUILD_OUT_VIEW, self.BuildOutViewSignal)
end
local RemoveListener = function(self)
  EventManager:GetInstance():RemoveListener(EventId.ShowBuildDestroyEffect, self.BuildInViewSignal)
  EventManager:GetInstance():RemoveListener(EventId.BUILD_OUT_VIEW, self.BuildOutViewSignal)
end
local ShowBuildDestroyEffect = function(self, bUuid, posIndex, tileX, tileY)
  local param = {}
  param.bUuid = bUuid
  param.tileX = tileX
  param.tileY = tileY
  param.posIndex = posIndex
  if self.allEffect[bUuid] == nil and self.OnCreateEffect[bUuid] == nil then
    local modelName = "Assets/_Art/Effect/prefab/scene/Common/VFX_jianzhufeixu_" .. tile .. "X" .. tile .. ".prefab"
    local request = ResourceManager:InstantiateAsync(modelName)
    local par = {}
    par.request = request
    self.OnCreateEffect[bUuid] = request
    request:completed("+", function()
      self.OnCreateEffect[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 effect = BuildDestroyEffect.New()
      effect:OnCreate(request)
      effect:ReInit(param)
      par.script = effect
      self.allEffect[bUuid] = par
    end)
  end
end
local RemoveOneEffect = function(self, bUuid)
  local temp = self.allEffect[bUuid]
  if temp ~= nil then
    local request = temp.request
    temp.script:OnDestroy()
    request:Destroy()
    self.allEffect[bUuid] = nil
  end
  if self.OnCreateEffect[bUuid] ~= nil then
    local request = self.OnCreateEffect[bUuid]
    request:Destroy()
    self.OnCreateEffect[bUuid] = nil
  end
end
local BuildInViewSignal = function(uuid)
  BuildDestroyEffectManager:GetInstance():CheckShowEffect(tonumber(uuid))
end
local BuildOutViewSignal = function(uuid)
  BuildDestroyEffectManager:GetInstance():RemoveOneEffect(tonumber(uuid))
end
local CheckShowEffect = function(self, bUuid)
  local info = CS.SceneManager.World:GetPointInfoByUuid(bUuid)
  if info ~= nil then
    cast(info, typeof(CS.BuildPointInfo))
    if info.destroyStartTime > 0 then
      local buildTemplate = DataCenter.BuildTemplateManager:GetBuildingDesTemplate(info.itemId)
      if buildTemplate ~= nil then
        self:ShowBuildDestroyEffect(bUuid, info.mainIndex, buildTemplate.tileX, buildTemplate.tileY)
      end
    else
      self:RemoveOneEffect(bUuid)
    end
  end
end
BuildDestroyEffectManager.__init = __init
BuildDestroyEffectManager.__delete = __delete
BuildDestroyEffectManager.AddListener = AddListener
BuildDestroyEffectManager.RemoveListener = RemoveListener
BuildDestroyEffectManager.BuildInViewSignal = BuildInViewSignal
BuildDestroyEffectManager.BuildOutViewSignal = BuildOutViewSignal
BuildDestroyEffectManager.RemoveOneEffect = RemoveOneEffect
BuildDestroyEffectManager.CheckShowEffect = CheckShowEffect
BuildDestroyEffectManager.ShowBuildDestroyEffect = ShowBuildDestroyEffect
return BuildDestroyEffectManager
