import request from '@/utils/request'

// ==================== 地图列表API ====================

/**
 * 获取地图列表
 * 后端接口: GET /v1/map/list
 * 返回格式: { status: true, maps: ['demo'], states: ['available'] }
 */
export function getMapList(deviceId) {
  return request({
    url: '/v1/map/list',
    method: 'get',
    params: { deviceId }
  })
}

/**
 * 获取当前使用中的地图
 * 后端接口: GET /v1/map/using
 * 返回格式: { status: true, name: '地图名' }
 */
export function getCurrentMap(deviceId) {
  return request({
    url: '/v1/map/using',
    method: 'get',
    params: { deviceId }
  })
}

/**
 * 获取地图缩略图
 * 后端接口: GET /v1/map/thumbnail?map=地图名
 * 返回格式: 图片二进制数据
 */
export function getMapThumbnail(mapName) {
  return request({
    url: '/v1/map/thumbnail',
    method: 'get',
    params: { map: mapName }
  })
}

/**
 * 重命名地图
 * 后端接口: POST /v1/map/rename
 * 请求格式: { map: '旧名称', rename: '新名称' }
 * 返回格式: { status: true }
 */
export function renameMap(data) {
  return request({
    url: '/v1/map/rename',
    method: 'post',
    data
  })
}

/**
 * 删除地图
 * 后端接口: 需要确认删除接口路径
 */
export function deleteMap(data) {
  return request({
    url: '/v1/map/delete',
    method: 'delete',
    data
  })
}

/**
 * 获取地图组件列表
 */
export function getMapComponents(mapName) {
  return request({
    url: '/v1/map/components',
    method: 'get',
    params: { map: mapName }
  })
}

/**
 * 压缩导出地图
 */
export function compressMapExport(data) {
  return request({
    url: '/v1/map/export/compress',
    method: 'post',
    data,
    timeout: 120000
  })
}

/**
 * 下载地图导出包
 */
export function downloadMapExport(mapName) {
  return request({
    url: '/v1/map/export',
    method: 'get',
    params: { map: mapName },
    responseType: 'blob',
    timeout: 60000
  })
}

/**
 * 导入地图
 */
export function importMap(formData) {
  return request({
    url: '/v1/map/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ==================== 路网数据API ====================

/**
 * 获取路网GeoJSON数据
 * 后端接口: GET /v1/roadmap/geojson?map=地图名
 * 返回格式: { status: true, data: {...} }
 */
export function getRoadMapGeoJson(mapName) {
  return request({
    url: '/v1/roadmap/geojson',
    method: 'get',
    params: { map: mapName }
  })
}

/**
 * 保存路网GeoJSON数据
 * 后端接口: POST /v1/roadmap/geojson
 */
export function saveRoadMapGeoJson(data) {
  return request({
    url: '/v1/roadmap/geojson',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// ==================== 任务路线API ====================

/**
 * 获取任务路线列表
 */
export function listPath(mapName) {
  return request({
    url: '/robot/map/path/list',
    method: 'get',
    params: { map: mapName }
  })
}

/**
 * 获取任务路线详情
 */
export function getPath(mapName, pathName) {
  return request({
    url: '/robot/map/path',
    method: 'get',
    params: { map: mapName, path: pathName }
  })
}

/**
 * 添加任务路线
 */
export function addPath(data) {
  return request({
    url: '/robot/map/path',
    method: 'post',
    data
  })
}

/**
 * 删除任务路线
 */
export function deletePath(data) {
  return request({
    url: '/robot/map/path',
    method: 'delete',
    data
  })
}

// ==================== 标定API ====================

/**
 * 获取标定历史
 */
export function getCalibrationHistory(mapName) {
  return request({
    url: '/robot/map/calibration/history',
    method: 'get',
    params: { map: mapName }
  })
}

// ==================== VSLAM API ====================

/**
 * 获取VSLAM统计信息
 */
export function getVSlamStatistics(mapName) {
  return request({
    url: '/robot/map/vslam/statistics',
    method: 'get',
    params: { map: mapName }
  })
}

/**
 * 获取关键帧点云
 */
export function getKeyframeCloud(mapName, idx) {
  return request({
    url: `/robot/map/vslam/keyframe/cloud?map=${mapName}&idx=${idx}`,
    method: 'get',
    responseType: 'arraybuffer'
  })
}

/**
 * 获取关键帧变换矩阵
 */
export function getKeyframeTrans(mapName, idx) {
  return request({
    url: `/robot/map/vslam/keyframe/trans?map=${mapName}&idx=${idx}`,
    method: 'get',
    responseType: 'arraybuffer'
  })
}

/**
 * 获取闭环详情
 */
export function getClosureDetails(mapName, idx) {
  return request({
    url: '/robot/map/vslam/closure/details',
    method: 'get',
    params: { map: mapName, idx: idx }
  })
}

// ==================== 传感器API ====================

/**
 * 获取点云数据
 */
export function getPointcloud() {
  return request({
    url: '/robot/map/sensor/pointcloud',
    method: 'get',
    responseType: 'arraybuffer'
  })
}

// ==================== 地图瓦片API ====================

/**
 * 获取瓦片地图参数
 * 后端接口: GET /v1/tilemap/details?map=地图名
 */
export function getTilemapDetails(mapName) {
  return request({
    url: '/v1/tilemap/details',
    method: 'get',
    params: { map: mapName }
  })
}

/**
 * 获取瓦片图片
 * 后端接口: GET /v1/tilemap/tile/{mapName}/{zoom}/{x}/{y}
 */
export function getTilemapTile(mapName, zoom, x, y) {
  return request({
    url: `/v1/tilemap/tile/${mapName}/${zoom}/${x}/${y}`,
    method: 'get',
    responseType: 'blob'
  })
}

// ==================== 设备状态API ====================

/**
 * 获取机器人实时位姿(从Redis缓存)
 */
export function getRobotPose(deviceId) {
  return request({
    url: '/robot/map/robot/pose',
    method: 'get',
    params: { deviceId }
  })
}

/**
 * 获取任务实时信息(从Redis缓存)
 */
export function getTaskRealtimeInfo(deviceId) {
  return request({
    url: '/robot/map/robot/task',
    method: 'get',
    params: { deviceId }
  })
}
