local this = class("mapStore", G_BaseStore)
this.event = {
  setTarget = "mapStore_setTarget",
  setMapMark = "setMapMark",
  refreshTracing = "refreshTracing",
  refreshNodeTrace = "refreshNodeTrace",
  refreshSpecialPetNode = "refreshSpecialPetNode",
  refreshMythicalPet = "refreshMythicalPet",
  refreshCollection = "refreshCollection",
  refreshCollectionTrace = "refreshCollectionTrace",
  refreshCollectionTraceItem = "refreshCollectionTraceItem"
}
this:importPartialClass(require(L_R.store .. "map.mapState"))
this:importPartialClass(require(L_R.store .. "map.mapAction"))

function this:ctor()
  this.super.ctor(self)
end

function this:rsp_syncWorldMapMarkList(data)
  if not table.isEmpty(data.marks) then
    for i, v in pairs(data.marks) do
      local info = self:getMark(v.map_id, v.guid)
      info.mapId = v.map_id
      info.markId = v.mark_id
      info.pos = string.toVector3(v.pos)
      info.guid = v.guid
      info.notes = string.isEmpty(v.notes) and L_WordsTpl:getValue("ui_signPiont") or v.notes
      info.pos_x = v.pos_x
      info.pos_y = v.pos_y
      info.pos_z = v.pos_z
      self.markData[v.map_id][v.guid] = info
    end
    self:call(self.event.setMapMark, false)
  end
  if not math.isEmpty(data.trace) then
    self.dicTracing[data.trace] = true
    self:onRefreshTracing(data.trace)
    self:call(self.event.refreshTracing)
  end
  if not table.isEmpty(data.objs) then
    self.discoveredObjs = self.discoveredObjs or {}
    for _, v in pairs(data.objs) do
      if v.map_id and v.obj_id then
        self.discoveredObjs[v.map_id] = self.discoveredObjs[v.map_id] or {}
        self.discoveredObjs[v.map_id][v.obj_id] = 1
      end
    end
  end
  if not table.isEmpty(data.special_monsters) then
    if #data.special_monsters ~= #self.specialMonsters then
      self.specialMonsters = data.special_monsters
      self:call(self.event.refreshSpecialPetNode)
      return
    end
    for _, v in ipairs(data.special_monsters) do
      for _, v1 in ipairs(self.specialMonsters) do
        if v1.map_id ~= v.map_id or v1.obj_id ~= v.obj_id or v1.special_type ~= v.special_type then
          self.specialMonsters = data.special_monsters
          self:call(self.event.refreshSpecialPetNode)
          return
        end
      end
    end
  elseif #self.specialMonsters > 0 then
    local curTracedNode = L_MapStore:getCurNodeTargetTracing()
    if not table.isEmpty(curTracedNode) and curTracedNode.nodeType == L_MapConst.mapSievingType.specialPet then
      L_MapStore:disposeNodeTarget()
    end
    self.specialMonsters = {}
    self:call(self.event.refreshSpecialPetNode)
  end
  if not table.isEmpty(data.collect_ids) then
    for _, v in ipairs(data.collect_ids) do
      if not self.collectionDict[v] then
        self.collectionDict[v] = true
      end
    end
    self:call(self.event.refreshCollection)
  end
end

function this:rsp_syncWorldMapMarkAdd(data)
  if not table.isEmpty(data) then
    local info, bNew = self:getMark(data.map_id, data.guid)
    info.mapId = data.map_id
    info.markId = data.mark_id
    info.pos = string.toVector3(data.pos)
    info.guid = data.guid
    info.notes = string.isEmpty(data.notes) and L_WordsTpl:getValue("ui_signPiont") or data.notes
    info.pos_x = data.pos_x
    info.pos_y = data.pos_y
    info.pos_z = data.pos_z
    self.markData[data.map_id][data.guid] = info
    self:call(self.event.setMapMark, bNew, data.guid)
    if data.mark_id == L_Const.mapNavPointId then
      self:req_worldMapMarkTracePointSet(data.guid)
    end
  end
end

function this:rsp_syncWorldMapMarkDel(guid, mapId)
  self:onRefreshTracing(guid, true)
  if self.markData[mapId] then
    self.markData[mapId][guid] = nil
    self:call(self.event.setMapMark, false)
  end
  self:call(self.event.refreshTracing)
end

function this:rsp_syncWorldMapMarkTracePointSet(guid)
  for i, v in pairs(self.dicTracing) do
    self.dicTracing[i] = false
  end
  self.dicTracing[guid] = true
  self:call(self.event.refreshTracing)
  self:onRefreshTracing(guid)
end

function this:rsp_syncWorldMapMarkTraceDel(guid)
  self.dicTracing[guid] = nil
  self:call(self.event.refreshTracing)
  self:onRefreshTracing(guid, true)
end

function this:onRefreshTracing(guid, bDel)
  local areaId = L_AreaStore:getMapAreaId()
  local info = self:getMarkBymapInfo(areaId, guid)
  if info and info.markId == L_Const.mapNavPointId then
    if info.pos_x and info.pos_y and info.pos_z and self:getTracing(guid) then
      if bDel then
        L_SceneGuideManager:endPathGuide()
      else
        L_SceneGuideManager:startPathGuideByPos(C_Vector3(info.pos_x, info.pos_y, info.pos_z))
      end
    else
      L_SceneGuideManager:endPathGuide()
    end
  end
end

function this:setFilterType(type)
  self.data.filterType = type
end

function this:removeDeepGuide()
  local areaId = L_AreaStore:getMapAreaId()
  local marks = this:getMarkBymapId(areaId)
  if not marks then
    return
  end
  for k, v in pairs(marks) do
    if v.markId == L_Const.mapNavPointId then
      self:req_worldMapMarkTraceDel(v.guid)
      return
    end
  end
end

function this:setNodeTargetTracing(sceneId, node)
  self.nodeTargetInfo = {
    sceneId = sceneId,
    nodeType = node.type,
    id = node.id,
    icon = node.iconPath,
    pos = node.configPos,
    iconBg = node.iconBg or "",
    name = node.name,
    desc = node.desc
  }
  self:call(self.event.refreshNodeTrace, self.nodeTargetInfo)
end

function this:disposeNodeTarget()
  self.nodeTargetInfo = {}
  self:call(self.event.refreshNodeTrace, self.nodeTargetInfo)
end

function this:setObjDiscovered(mapId, guid)
  self.discoveredObjs = self.discoveredObjs or {}
  self.discoveredObjs[mapId] = self.discoveredObjs[mapId] or {}
  self.discoveredObjs[mapId][guid] = 1
end

function this:setMythicalPet(pet)
  for i = 1, #self.mythicalPets do
    local v = self.mythicalPets[i]
    if self.nodeTargetInfo.id == v.id then
      self:disposeNodeTarget()
    end
    self.mythicalPets[i] = nil
  end
  if pet then
    table.insert(self.mythicalPets, pet)
  end
  self:call(self.event.refreshMythicalPet)
end

function this:setMapCollectionTrace(mapCollectionId)
  self.curTraceCollectionId = mapCollectionId
  self:call(self.event.refreshCollectionTrace)
end

function this:refreshCollectionItem(worldMapId)
  self:call(self.event.refreshCollectionTraceItem, worldMapId)
end

return this
