local FakeCollectGarbageMarchData = BaseClass("FakeCollectGarbageMarchData")
local retry_gap_time = 10000
local FakeMarchState = {
  FAKE_MARCH_STATE_NULL = 0,
  FAKE_MARCH_TO_GARBAGE = 1,
  FAKE_MARCH_COLLECT_GARBAGE = 2,
  FAKE_MARCH_GO_HOME = 3,
  FAKE_MARCH_ARRIVE_HOME = 4
}
local __init = function(self)
  self.startIndex = 0
  self.endIndex = 0
  self.startTime = 0
  self.GarbageStartTime = 0
  self.backHomeTime = 0
  self.arriveHomeTime = 0
  self.curState = FakeMarchState.FAKE_MARCH_STATE_NULL
end
local __delete = function(self)
  self.startIndex = nil
  self.endIndex = nil
  self.startTime = nil
  self.GarbageStartTime = nil
  self.backHomeTime = nil
  self.arriveHomeTime = nil
end
local SetStartAndEndIndex = function(self, pointIndex, startIndex, endIndex)
  self.pointIndex = pointIndex
  self.startIndex = startIndex
  self.endIndex = endIndex
  local data = CS.SceneManager.World:GetPointInfo(self.pointIndex)
  if data then
    self.pointUid = data.uuid
    self.uuid = data.uuid
  end
  local detectEventData = DataCenter.RadarCenterDataManager:GetNotFinishDetectEventInfoByPointId(self.pointIndex)
  if detectEventData ~= nil then
    self.pointUid = detectEventData.uuid
    self.uuid = detectEventData.uuid
  end
end
local UpdateState = function(self, curTime)
  if self.curState == FakeMarchState.FAKE_MARCH_STATE_NULL then
    self:StartMarch()
  elseif self.curState == FakeMarchState.FAKE_MARCH_TO_GARBAGE and curTime >= self.GarbageStartTime then
    self:CheckAndStartCollectGarbage(curTime)
  elseif self.curState == FakeMarchState.FAKE_MARCH_COLLECT_GARBAGE and curTime >= self.backHomeTime then
    self:CheckAndGoBack(curTime)
  elseif self.curState == FakeMarchState.FAKE_MARCH_GO_HOME and curTime >= self.arriveHomeTime then
    self:CheckBackHome(curTime)
  end
  self:CheckPickEnd()
end
local StartMarch = function(self)
  if self.curState == FakeMarchState.FAKE_MARCH_STATE_NULL then
    local curTime = UITimeManager:GetInstance():GetServerTime()
    self.curState = FakeMarchState.FAKE_MARCH_TO_GARBAGE
    self.startTime = math.ceil(curTime)
    local marchTime = self:CalculateMarchTime()
    self.GarbageStartTime = math.ceil(curTime + marchTime)
    self.backHomeTime = math.ceil(self.GarbageStartTime + self:CalculateCollectGarbageTime())
    self.arriveHomeTime = math.ceil(self.backHomeTime + marchTime)
    SFSNetwork.SendMessage(MsgDefines.StartPickGarbage, self.pointUid)
    if CS.SceneManager:IsInWorld() then
      CS.SceneManager.World:AddFakeSampleMarchData(self.startIndex, self.endIndex, self.startTime, self.GarbageStartTime, MarchTargetType.SAMPLE)
    end
  end
end
local CheckAndStartCollectGarbage = function(self, curTime)
  if self.curState ~= FakeMarchState.FAKE_MARCH_COLLECT_GARBAGE and curTime >= self.GarbageStartTime then
    self.curState = FakeMarchState.FAKE_MARCH_COLLECT_GARBAGE
    if CS.SceneManager:IsInWorld() then
      CS.SceneManager.World:UpdateFakeSampleMarchDataWhenStartPick(self.endIndex, self.backHomeTime)
    end
  end
end
local CheckAndGoBack = function(self, curTime)
  if self.curState ~= FakeMarchState.FAKE_MARCH_GO_HOME and curTime >= self.backHomeTime then
    self.curState = FakeMarchState.FAKE_MARCH_GO_HOME
    if CS.SceneManager:IsInWorld() then
      CS.SceneManager.World:UpdateFakeSampleMarchDataWhenBack(self.endIndex, self.backHomeTime, self.arriveHomeTime + 1000)
    end
  end
end
local CheckPickEnd = function(self)
  if self.curState == FakeMarchState.FAKE_MARCH_GO_HOME or self.curState == FakeMarchState.FAKE_MARCH_ARRIVE_HOME then
    local now = UITimeManager:GetInstance():GetServerTime()
    if self.uuid ~= nil and (self.callTime == nil or now - self.callTime > retry_gap_time) then
      self:SendPickEndMessage()
    end
  end
end
local SendPickEndMessage = function(self)
  self.callTime = UITimeManager:GetInstance():GetServerTime()
  SFSNetwork.SendMessage(MsgDefines.FinishSampling, self.uuid)
end
local CheckBackHome = function(self, curTime)
  if self.curState == FakeMarchState.FAKE_MARCH_GO_HOME and curTime >= self.arriveHomeTime then
    self.curState = FakeMarchState.FAKE_MARCH_ARRIVE_HOME
    self:Remove()
  end
end
local CalculateMarchTime = function(self)
  local startPt = SceneUtils.IndexToTilePos(self.startIndex)
  local endPt = SceneUtils.IndexToTilePos(self.endIndex)
  local dis = Vector2.Distance(startPt, endPt)
  local speed = LuaEntry.DataConfig:TryGetNum("armyspeed", "k2")
  local time = dis * 1000 / speed
  return time
end
local CalculateCollectGarbageTime = function(self)
  local info = CS.SceneManager.World:GetPointInfo(self.pointIndex)
  if info == nil or string.IsNullOrEmpty(info.pointIndex) then
    return 1
  end
  local template = DataCenter.DetectEventTemplateManager:GetDetectEventTemplate(info.eventId)
  if template == nil then
    return 1
  end
  if string.IsNullOrEmpty(template.para) then
    return 1
  end
  return toInt(template.para) * 1000
end
local Remove = function(self)
  if CS.SceneManager:IsInWorld() then
    CS.SceneManager.World:RemoveFakeSampleMarchData(self.endIndex)
  end
end
local NeedRemove = function(self)
  if self.curState == FakeMarchState.FAKE_MARCH_ARRIVE_HOME and DataCenter.FakeCollectGarbageMarchManager:NeedRemove(self.pointIndex) then
    return true
  end
  return false
end
local IsEventDoing = function(self)
  return self.curState == FakeMarchState.FAKE_MARCH_TO_GARBAGE or self.curState == FakeMarchState.FAKE_MARCH_COLLECT_GARBAGE
end
local DoWhenBackToWorld = function(self)
  if CS.SceneManager.World:ExistMarch(self.endIndex) then
    return
  end
  local pointInfo = CS.SceneManager.World:GetPointInfo(self.endIndex)
  if pointInfo == nil then
    return
  end
  if self.curState == FakeMarchState.FAKE_MARCH_TO_GARBAGE then
    CS.SceneManager.World:RemoveFakeSampleMarchData(self.endIndex)
    CS.SceneManager.World:AddFakeSampleMarchData(self.startIndex, self.endIndex, self.startTime, self.GarbageStartTime, MarchTargetType.SAMPLE)
  else
    self.curState = FakeMarchState.FAKE_MARCH_ARRIVE_HOME
    self:UpdateState(UITimeManager:GetInstance():GetServerTime())
  end
end
FakeCollectGarbageMarchData.__init = __init
FakeCollectGarbageMarchData.__delete = __delete
FakeCollectGarbageMarchData.SetStartAndEndIndex = SetStartAndEndIndex
FakeCollectGarbageMarchData.StartMarch = StartMarch
FakeCollectGarbageMarchData.CheckAndStartCollectGarbage = CheckAndStartCollectGarbage
FakeCollectGarbageMarchData.CheckAndGoBack = CheckAndGoBack
FakeCollectGarbageMarchData.CalculateMarchTime = CalculateMarchTime
FakeCollectGarbageMarchData.CalculateCollectGarbageTime = CalculateCollectGarbageTime
FakeCollectGarbageMarchData.UpdateState = UpdateState
FakeCollectGarbageMarchData.Remove = Remove
FakeCollectGarbageMarchData.CheckBackHome = CheckBackHome
FakeCollectGarbageMarchData.CheckPickEnd = CheckPickEnd
FakeCollectGarbageMarchData.SendPickEndMessage = SendPickEndMessage
FakeCollectGarbageMarchData.NeedRemove = NeedRemove
FakeCollectGarbageMarchData.IsEventDoing = IsEventDoing
FakeCollectGarbageMarchData.DoWhenBackToWorld = DoWhenBackToWorld
return FakeCollectGarbageMarchData
