local this = class("sceneGuideManager")

function this:initialize()
  L_EntityManager:addListener(L_EntityManager.event.destroyEntity, self.listenerDestroyEntity, self)
  L_EntityManager:addListener(L_EntityManager.event.destroyAllEntity, self.listenerDestroyAllEntity, self)
  L_SceneManager:addListener(L_SceneManager.event.switchSceneEnd, self.listenerWoldStart, self)
  L_SceneManager:addListener(L_SceneManager.event.suspendCurScene, self.CloseArrow, self)
  self.guidePathEntity = require("ui.manager.guide.sceneGuidePathEntity").new()
  self:reset()
end

function this:reset()
  self.startSceneGuide = false
  self.allowGuide = false
  self.hasPlayer = false
  self.hasEntity = false
  self.targetPathIndex = 1
end

function this:clear()
  self.arrow = nil
  self.arrowObj = nil
  self:reset()
  self.guidePathEntity:clear()
end

function this:setPlayer()
  local player = L_PlayerManager:getMainControlEntity()
  self.hasPlayer = player and player:isValid() and player.gameObject.activeInHierarchy
end

function this:listenerCreateEntity(id, entity)
  if self.entityId == id then
    self:setPlayer()
    self.hasEntity = entity:isValid()
    if not (self.allowGuide and self.startSceneGuide and self.hasPlayer) or not self.hasEntity then
      if self:isArrowExist() then
        self:CloseArrow()
      end
      return
    end
    self:genPath(entity:getTransform().position)
  end
end

function this:listenerWoldStart()
  printf("\229\164\167\228\184\150\231\149\140", "npc\233\128\187\232\190\145\229\136\160\233\153\164\239\188\140\232\175\183\228\188\152\229\140\150\230\173\164\228\187\163\231\160\129\233\128\187\232\190\145")
  self.allowGuide = true
  self:setPlayer()
end

function this:listenerDestroyEntity(entity)
  if self.entityId == entity:getId() then
    self.hasEntity = false
    if self:isArrowExist() then
      self:CloseArrow()
    end
  end
end

function this:listenerDestroyAllEntity()
  self.hasEntity = false
  self.hasPlayer = false
  if self:isArrowExist() then
    self:CloseArrow()
  end
end

function this:startPathGuideByPos(pos)
  self.guidePathEntity:clean()
  self.guidePathEntity:genPathGuide(pos, true)
end

function this:genPath(pos)
  self.pathPos = {}
  table.insert(self.pathPos, {targetPos = pos})
end

function this:endPathGuide()
  self.startSceneGuide = false
  self.guidePathEntity:clean()
  if self:isArrowExist() then
    self:CloseArrow()
  end
end

function this:isArrowExist()
  if self.arrowObj ~= nil and not self.arrowObj:IsNull() and self.arrow ~= nil then
    return true
  end
  return false
end

function this:CloseArrow()
end

function this:removeDeepGuides()
  L_TaskStore:disposeTraceTask()
  L_MapStore:removeDeepGuide()
end

return this
