local FakeCollectGarbageMarchManager = BaseClass("FakeCollectGarbageMarchManager")
local FakeCollectGarbageMarchData = require("DataCenter.RadarCenterDataManager.FakeCollectGarbageMarchData")
local __init = function(self)
  self.allMarches = {}
  
  function self.timer_action(temp)
    self:CheckAndRefreshMarches()
  end
  
  self:AddListener()
end
local __delete = function(self)
  self.allMarches = nil
  self:RemoveListener()
end
local AddListener = function(self)
  EventManager:GetInstance():AddListenerWithSelf(EventId.OnEnterWorld, self.DoWhenBackToWorld, self)
end
local RemoveListener = function(self)
  EventManager:GetInstance():RemoveListener2(EventId.OnEnterWorld, self.DoWhenBackToWorld, self)
end
local AddMarchIndex = function(self, pointId, eventId_)
  if self.allMarches[pointId] == nil then
    local data
    if eventId_ then
      local config = DataCenter.DetectEventTemplateManager:GetDetectEventTemplate(eventId_)
      if config and config.type == DetectEventType.SEASON_VISITOR then
        local FakeCollectGarbageMarchDataVisitor = require("DataCenter.RadarCenterDataManager.FakeCollectGarbageMarchData_Visitor")
        data = FakeCollectGarbageMarchDataVisitor.New()
      end
    end
    data = data or FakeCollectGarbageMarchData.New()
    self.allMarches[pointId] = data
    local startPt = LuaEntry.Player:GetMainWorldPos()
    local endPt = pointId
    data:SetStartAndEndIndex(pointId, startPt, endPt)
  end
  self:AddTimer()
  self:CheckAndRefreshMarches()
end
local RemoveMarchIndex = function(self, pointId)
  if self.allMarches[pointId] ~= nil then
    self.allMarches[pointId]:Remove()
  end
  self.allMarches[pointId] = nil
  if table.count(self.allMarches) == 0 then
    self:RemoveTimer()
  end
end
local AddTimer = function(self)
  if self.timer == nil then
    self.timer = TimerManager:GetInstance():GetTimer(1, self.timer_action, self, false, false, false)
    self.timer:Start()
  end
end
local RemoveTimer = function(self)
  if self.timer ~= nil then
    self.timer:Stop()
    self.timer = nil
  end
end
local CheckAndRefreshMarches = function(self)
  local removeList = {}
  table.walk(self.allMarches, function(k, v)
    if v:NeedRemove() then
      table.insert(removeList, k)
    else
      local now = UITimeManager:GetInstance():GetServerTime()
      v:UpdateState(now)
    end
  end)
  table.walk(removeList, function(_, v)
    self:RemoveMarchIndex(v)
  end)
  if self.rebuildFlag == true and CS.SceneManager:IsInWorld() then
    if table.count(self.allMarches) == 0 then
      self.rebuildFlag = false
    else
      table.walk(self.allMarches, function(_, v)
        local pointInfo = CS.SceneManager.World:GetPointInfo(v.endIndex)
        if pointInfo ~= nil then
          v:DoWhenBackToWorld()
          self.rebuildFlag = false
        end
      end)
    end
  end
end
local StartMarch = function(self, uid)
  self:AddMarchIndex(uid)
  self.allMarches[uid]:StartMarch()
end
local RemoveAllDisappearEvent = function(self)
  local needDeletes = {}
  table.walk(self.allMarches, function(k, _)
    if self:NeedRemove(k) then
      table.insert(needDeletes, k)
    end
  end)
  table.walk(needDeletes, function(_, v)
    self:RemoveMarchIndex(v)
  end)
end
local IsEventDoing = function(self, pointId)
  if self.allMarches[pointId] ~= nil then
    return self.allMarches[pointId]:IsEventDoing()
  end
  return false
end
local NeedRemove = function(self, index)
  if not CS.SceneManager:IsInWorld() then
    return false
  end
  local info = CS.SceneManager.World:GetPointInfo(index)
  if info ~= nil and (info.PointType == WorldPointType.SAMPLE_POINT_NEW or info.PointType == WorldPointType.GARBAGE) then
    return false
  end
  local detectEventData = DataCenter.RadarCenterDataManager:GetDetectEventInfoByPointId(index)
  if info == nil and detectEventData ~= nil then
    return false
  end
  return true
end
local DoWhenBackToWorld = function(self)
  self.rebuildFlag = true
end
FakeCollectGarbageMarchManager.__init = __init
FakeCollectGarbageMarchManager.__delete = __delete
FakeCollectGarbageMarchManager.AddMarchIndex = AddMarchIndex
FakeCollectGarbageMarchManager.RemoveMarchIndex = RemoveMarchIndex
FakeCollectGarbageMarchManager.AddTimer = AddTimer
FakeCollectGarbageMarchManager.RemoveTimer = RemoveTimer
FakeCollectGarbageMarchManager.CheckAndRefreshMarches = CheckAndRefreshMarches
FakeCollectGarbageMarchManager.RemoveAllDisappearEvent = RemoveAllDisappearEvent
FakeCollectGarbageMarchManager.StartMarch = StartMarch
FakeCollectGarbageMarchManager.IsEventDoing = IsEventDoing
FakeCollectGarbageMarchManager.NeedRemove = NeedRemove
FakeCollectGarbageMarchManager.DoWhenBackToWorld = DoWhenBackToWorld
FakeCollectGarbageMarchManager.AddListener = AddListener
FakeCollectGarbageMarchManager.RemoveListener = RemoveListener
return FakeCollectGarbageMarchManager
