import * as Cesium from 'cesium'
import {
  MOCK_PERSONNEL_POINTS,
  MOCK_DEVICE_POINTS,
  ANIMATION_PATHS,
  MOCK_VIDEO_URL,
  MARKER_ICON_SIZE,
} from '../constants'

/**
 * 模拟数据服务
 *
 * 职责：
 * 1. 提供类型安全的模拟数据访问
 * 2. 将原始数据转换为 Cesium 实体配置
 * 3. 便于后续替换为真实 API
 *
 * @returns {Object} 模拟数据 API
 */
export function useMockData() {
  /**
   * 获取模拟人员点位数据
   * @returns {Array} 人员点位数组
   */
  const getPersonnelPoints = () => {
    return MOCK_PERSONNEL_POINTS.map((point) => ({
      ...point,
      location: `目前为止距离现场${point.distance}公里`,
    }))
  }

  /**
   * 获取模拟设备点位数据
   * @returns {Array} 设备点位数组
   */
  const getDevicePoints = () => {
    return MOCK_DEVICE_POINTS.map((point) => ({
      ...point,
      location: `目前为止距离现场${point.distance}公里`,
    }))
  }

  /**
   * 获取所有模拟点位数据（人员 + 设备）
   * @returns {Array} 所有点位数组
   */
  const getAllMockPoints = () => {
    return [...getPersonnelPoints(), ...getDevicePoints()]
  }

  /**
   * 创建人员实体配置
   * @param {Object} point - 人员点位数据
   * @param {string} iconUrl - 图标 URL
   * @returns {Object} Cesium 实体配置
   */
  const createPersonnelEntityConfig = (point, iconUrl) => {
    return {
      position: Cesium.Cartesian3.fromDegrees(point.lon, point.lat),
      billboard: {
        image: iconUrl,
        width: MARKER_ICON_SIZE.width,
        height: MARKER_ICON_SIZE.height,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
      },
      properties: {
        type: 'soldier',
        name: point.name,
        department: point.department,
        location: point.location,
        estimatedArrival: point.estimatedArrival,
      },
    }
  }

  /**
   * 创建设备实体配置
   * @param {Object} point - 设备点位数据
   * @param {string} iconUrl - 图标 URL
   * @returns {Object} Cesium 实体配置
   */
  const createDeviceEntityConfig = (point, iconUrl) => {
    return {
      position: Cesium.Cartesian3.fromDegrees(point.lon, point.lat),
      billboard: {
        image: iconUrl,
        width: MARKER_ICON_SIZE.width,
        height: MARKER_ICON_SIZE.height,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
      },
      properties: {
        type: 'device',
        name: point.name,
        deviceType: point.deviceType,
        location: point.location,
        estimatedArrival: point.estimatedArrival,
      },
    }
  }

  /**
   * 获取动画路径数据
   * @param {string} pathId - 路径 ID ('path1', 'path2', 'path3')
   * @returns {Object} 路径数据
   */
  const getAnimationPath = (pathId) => {
    return ANIMATION_PATHS[pathId]
  }

  /**
   * 获取所有动画路径
   * @returns {Object} 所有路径数据
   */
  const getAllAnimationPaths = () => {
    return ANIMATION_PATHS
  }

  /**
   * 创建路径起点标记配置
   * @param {Object} path - 路径数据
   * @param {string} iconUrl - 图标 URL
   * @returns {Object} Cesium 实体配置
   */
  const createPathStartMarkerConfig = (path, iconUrl) => {
    const { startPoint, metadata } = path

    return {
      position: new Cesium.Cartesian3(startPoint.x, startPoint.y, startPoint.z),
      billboard: {
        image: iconUrl,
        width: MARKER_ICON_SIZE.width,
        height: MARKER_ICON_SIZE.height,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
      },
      properties: {
        type: metadata.type,
        name: metadata.name,
        department: metadata.department || metadata.deviceType,
        location: '待命中',
        estimatedArrival: '待启动',
        isPathStartMarker: true,
        pathId: path.id,
      },
    }
  }

  /**
   * 获取视频 URL
   * @returns {string} 视频 URL
   */
  const getVideoUrl = () => {
    return MOCK_VIDEO_URL
  }

  return {
    // 数据获取
    getPersonnelPoints,
    getDevicePoints,
    getAllMockPoints,
    getAnimationPath,
    getAllAnimationPaths,
    getVideoUrl,

    // 实体配置创建
    createPersonnelEntityConfig,
    createDeviceEntityConfig,
    createPathStartMarkerConfig,
  }
}
