local BattleFieldAnimManager = BaseClass("BattleFieldAnimManager")
local BattleObjBuild = require("DataCenter.ActWinterStormManager.BattleAnim.BattleObjBuild")
local BattleObjWinterDrop = require("DataCenter.ActWinterStormManager.BattleAnim.BattleObjWinterDrop")

function BattleFieldAnimManager:__init()
  self.objs = {}
  self.objBuilds = {}
  self.objSkill = {}
  self.objDrops = {}
  self.dropPreSign = {}
  self.skillBornSign = {}
end

function BattleFieldAnimManager:__delete()
  self:CleanObjs()
end

function BattleFieldAnimManager:CleanObjs()
  for _, v in pairs(self.objs) do
    v:OnDestroy()
    v:Delete()
  end
  self.objs = {}
  self.objBuilds = {}
  self.objSkill = {}
  self.objDrops = {}
  self.dropPreSign = {}
  self.skillBornSign = {}
end

function BattleFieldAnimManager:UpdateObjs()
  local curTime = UITimeManager:GetInstance():GetServerTime()
  local objBuilds = DataCenter.BattleFieldAnimManager.objBuilds
  for _, v in pairs(objBuilds) do
    v:Update(curTime)
  end
  local objDrops = DataCenter.BattleFieldAnimManager.objDrops
  for _, v in pairs(objDrops) do
    v:Update(curTime)
  end
end

function BattleFieldAnimManager:PlayDrop(pointIndex)
  local obj = self.objs[pointIndex]
  if obj ~= nil and type(obj.InitDrop) == "function" then
    obj:InitDrop()
  else
    self.dropPreSign[pointIndex] = true
  end
end

function BattleFieldAnimManager:DeleteObj(pointIndex)
  local obj = self.objs[pointIndex]
  if obj ~= nil then
    obj:OnDestroy()
    obj:Delete()
  end
  self.objs[pointIndex] = nil
  self.objBuilds[pointIndex] = nil
  self.objSkill[pointIndex] = nil
  self.objDrops[pointIndex] = nil
  self.dropPreSign[pointIndex] = nil
  self.skillBornSign[pointIndex] = nil
end

function BattleFieldAnimManager:UpdateObjSkin(pointIndex, bfType)
  local obj = self.objs[pointIndex]
  if obj ~= nil then
    obj:UpdateDetail()
    obj:ReInit()
    return
  end
  local info = CS.SceneManager.World:GetPointInfo(pointIndex)
  local detailInfo = info ~= nil and info.detail or nil
  local buildId = detailInfo ~= nil and detailInfo.BuildId or 0
  if bfType == BattleFieldType.WinterStorm then
    local config = DataCenter.WinterStormTemplateManager:GetTemplate(buildId)
    if config ~= nil then
      if config:IsBuild() then
        obj = BattleObjBuild.New()
        if obj ~= nil then
          self.objBuilds[pointIndex] = obj
        end
      elseif config:IsDrop() then
        obj = BattleObjWinterDrop.New()
        if obj ~= nil then
          self.objDrops[pointIndex] = obj
        end
      end
    end
  elseif bfType == BattleFieldType.EpidemicZone and DataCenter.EpidemicBuildTemplateMgr:IsDefence(buildId) then
    local config = DataCenter.EpidemicBuildTemplateMgr:GetTemplate(buildId)
    if config ~= nil then
      obj = BattleObjBuild.New()
      if obj ~= nil then
        self.objBuilds[pointIndex] = obj
      end
    end
  end
  if obj == nil then
    return
  end
  self.objs[pointIndex] = obj
  obj:OnCreate(pointIndex, bfType)
  obj:ReInit()
  if self.dropPreSign[pointIndex] then
    self.dropPreSign[pointIndex] = nil
    self:PlayDrop(pointIndex)
  end
end

function BattleFieldAnimManager:UpdateObjSkill(uuid, skillId, gameObject)
  if IsNull(gameObject) then
    self:DeleteObj(uuid)
    return
  end
  local obj = self.objs[uuid]
  if obj ~= nil then
    obj:UpdateSkillInfo(skillId, gameObject)
    obj:ReInit()
    return
  end
  obj = BattleObjBuild.New()
  if obj ~= nil then
    self.objBuilds[uuid] = obj
  end
  if obj == nil then
    return
  end
  self.objs[uuid] = obj
  obj:OnCreateSkill(uuid, skillId, gameObject)
  obj:ReInit()
  local ownerUid = obj.ownerUid
  if ownerUid ~= nil then
    self.objSkill[ownerUid] = uuid
    local info = self.skillBornSign[ownerUid]
    if info ~= nil then
      obj:PlayAnim(info[1], info[2])
      self.skillBornSign[ownerUid] = nil
    end
  end
end

function BattleFieldAnimManager:CleanSkillAim(ownerUid)
  local uuid = self.objSkill[ownerUid]
  local obj = self.objs[uuid]
  if obj ~= nil then
    obj:HideAllAimEff()
  end
end

function BattleFieldAnimManager:SetSkillObjTargetPid(ownerUid, skillTargetPId, findAimTime)
  local uuid = self.objSkill[ownerUid]
  local obj = self.objs[uuid]
  if obj and skillTargetPId and 0 < skillTargetPId then
    obj:UpdateSkillDetail(skillTargetPId, findAimTime)
    obj:ReInit()
  end
end

function BattleFieldAnimManager:SetSkillObjAnim(ownerUid, animStr, bRewind)
  local uuid = self.objSkill[ownerUid]
  local obj = self.objs[uuid]
  if obj ~= nil then
    obj:PlayAnim(animStr, bRewind)
    return obj.firePoint
  elseif animStr == BattleFieldObjActType.BORN and not self.skillBornSign[ownerUid] then
    self.skillBornSign[ownerUid] = {animStr, bRewind}
  end
end

return BattleFieldAnimManager
