local this = class("mapNpcNode", require("ui.manager.map.node.mapNode"))

function this:ctor(type)
  this.super.ctor(self, type)
  self.tmpPos = nil
end

function this:generate(parent, pool)
  this.super.generate(self, parent, pool)
  self.tmpPos = self.configPos
end

function this:checkShow(mapData)
  local pos = self:getNpcRealPosition()
  local isShow = this.super.checkShow(self, mapData) and self:checkPosIsValid(pos)
  return isShow
end

function this:getNpcRealPosition()
  local npcPos = C_WorldHelper.GetWorldNpcBornPos(self.id, self.sceneId)
  local pos = L_Vector3.new(npcPos.x, npcPos.y, npcPos.z)
  return pos
end

function this:checkPosIsValid(pos)
  if table.isEmpty(pos) or pos.x == 0 and pos.y == 0 and pos.z == 0 then
    return false
  end
  return true
end

function this:refreshPosByMapData(mapData)
  if this.super.checkShow(self, mapData) == false then
    return
  end
  local pos = self:getNpcRealPosition()
  if not self:checkPosIsValid(pos) then
    self:setShow(false)
    if not table.isEmpty(self.tmpPos) then
      self.configPos = self.tmpPos
    end
    return
  end
  self.configPos = pos
  if self.isShow == false then
    self:setShow(true)
  end
  local l2w = mapData:worldToLocal(pos)
  L_Vector3.setAnchored(self.trans, l2w)
  L_Vector3.setSize(self.trans, 1 / mapData.imgScale)
end

return this
