import * as Cesium from 'cesium'
import { ref } from 'vue'
import { ANIMATION_PATHS } from '../constants'

/**
 * 路径线管理
 *
 * 职责：
 * 1. 绘制应急人员和设备的移动路径线
 * 2. 管理路径线实体的生命周期
 * 3. 清理路径线资源
 *
 * @returns {Object} 路径线管理 API
 */
export function usePathLines() {
  // 路径线实体数组
  const pathLineEntities = ref([])

  /**
   * 绘制所有路径线
   * @param {Cesium.Viewer} viewer - Cesium viewer 实例
   * @param {Object} options - 可选配置
   * @param {string} options.color - 路径线颜色（默认红色）
   * @param {number} options.width - 路径线宽度（默认 5）
   * @param {boolean} options.clampToGround - 是否贴地（默认 true）
   */
  const drawAllPathLines = (viewer, options = {}) => {
    if (!viewer) {
      console.warn('[usePathLines] viewer 为空')
      return
    }

    const { color = Cesium.Color.RED, width = 5, clampToGround = true } = options

    // 清除旧的路径线
    clearPathLines(viewer)

    // 绘制所有路径
    Object.values(ANIMATION_PATHS).forEach((path) => {
      drawPathLine(viewer, path.waypoints, { color, width, clampToGround })
    })

    console.log(`[usePathLines] 已绘制 ${pathLineEntities.value.length} 条路径线`)
  }

  /**
   * 绘制单条路径线
   * @param {Cesium.Viewer} viewer - Cesium viewer 实例
   * @param {Array} waypoints - 路径点数组
   * @param {Object} options - 样式配置
   */
  const drawPathLine = (viewer, waypoints, options) => {
    const { color, width, clampToGround } = options

    // 转换坐标点
    const positions = waypoints.map(
      (coord) => new Cesium.Cartesian3(coord.x, coord.y, coord.z)
    )

    // 创建路径线实体
    const pathEntity = viewer.entities.add({
      polyline: {
        positions: positions,
        width: width,
        material: color,
        clampToGround: clampToGround,
      },
    })

    pathLineEntities.value.push(pathEntity)
  }

  /**
   * 绘制指定路径的路径线
   * @param {Cesium.Viewer} viewer - Cesium viewer 实例
   * @param {string} pathId - 路径 ID ('path1', 'path2', 'path3')
   * @param {Object} options - 样式配置
   */
  const drawPathLineById = (viewer, pathId, options = {}) => {
    if (!viewer) {
      console.warn('[usePathLines] viewer 为空')
      return
    }

    const path = ANIMATION_PATHS[pathId]
    if (!path) {
      console.warn(`[usePathLines] 路径 ${pathId} 不存在`)
      return
    }

    const { color = Cesium.Color.RED, width = 5, clampToGround = true } = options

    drawPathLine(viewer, path.waypoints, { color, width, clampToGround })
  }

  /**
   * 清除所有路径线
   * @param {Cesium.Viewer} viewer - Cesium viewer 实例
   */
  const clearPathLines = (viewer) => {
    if (!viewer) return

    pathLineEntities.value.forEach((entity) => {
      if (entity) {
        viewer.entities.remove(entity)
      }
    })

    pathLineEntities.value = []
    console.log('[usePathLines] 所有路径线已清除')
  }

  /**
   * 显示所有路径线
   */
  const showPathLines = () => {
    pathLineEntities.value.forEach((entity) => {
      if (entity) {
        entity.show = true
      }
    })
  }

  /**
   * 隐藏所有路径线
   */
  const hidePathLines = () => {
    pathLineEntities.value.forEach((entity) => {
      if (entity) {
        entity.show = false
      }
    })
  }

  return {
    pathLineEntities,
    drawAllPathLines,
    drawPathLineById,
    clearPathLines,
    showPathLines,
    hidePathLines,
  }
}
