local FakeHelperMarchManager = BaseClass("FakeHelperMarchManager")
local FakeHelperMarchData = require("DataCenter.RadarCenterDataManager.FakeHelperMarchData")
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)
  if self.allMarches[pointId] == nil then
    local data = FakeHelperMarchData.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)
        v:DoWhenBackToWorld()
        self.rebuildFlag = false
      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, v)
    if self:NeedRemove(v.eventUuid) 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, uuid)
  if not CS.SceneManager:IsInWorld() then
    return false
  end
  local detectEventData = DataCenter.RadarCenterDataManager:GetDetectEventInfo(uuid)
  if detectEventData ~= nil and (detectEventData.state == DetectEventState.DETECT_EVENT_STATE_NOT_FINISH or detectEventData.state == DetectEventState.DETECT_EVENT_STATE_FINISHED) then
    return false
  end
  return true
end
local DoWhenBackToWorld = function(self)
  self.rebuildFlag = true
end
FakeHelperMarchManager.__init = __init
FakeHelperMarchManager.__delete = __delete
FakeHelperMarchManager.AddMarchIndex = AddMarchIndex
FakeHelperMarchManager.RemoveMarchIndex = RemoveMarchIndex
FakeHelperMarchManager.AddTimer = AddTimer
FakeHelperMarchManager.RemoveTimer = RemoveTimer
FakeHelperMarchManager.CheckAndRefreshMarches = CheckAndRefreshMarches
FakeHelperMarchManager.RemoveAllDisappearEvent = RemoveAllDisappearEvent
FakeHelperMarchManager.StartMarch = StartMarch
FakeHelperMarchManager.IsEventDoing = IsEventDoing
FakeHelperMarchManager.NeedRemove = NeedRemove
FakeHelperMarchManager.DoWhenBackToWorld = DoWhenBackToWorld
FakeHelperMarchManager.AddListener = AddListener
FakeHelperMarchManager.RemoveListener = RemoveListener
return FakeHelperMarchManager
