// const model = 'static/SampleData/models/CesiumDrone/CesiumDrone.glb'
// const routeLine = [
//   {
//     longitude: 115.5310955104305, // 起始点经度
//     latitude: 29.45941132782301, // 起始点维度
//     height: 464.3558799552955, // 起始点高度
//     time: 0// 起始时间
//   },
//   {
//     longitude: 115.53130228598145,
//     latitude: 29.459378517206638,
//     height: 466.1145881293518,
//     time: 10
//   },
//   {
//     longitude: 115.5313953769011,
//     latitude: 29.459311296391423,
//     height: 467.48996257505485,
//     time: 20
//   },
//   {
//     longitude: 115.53149832517173,
//     latitude: 29.459228656016336,
//     height: 469.9064236522573,
//     time: 30
//   }
// ]
// const data = {
//   routeLine,
//   model

// }
export default class ModelMove {
  constructor(viewer, data) {
    this.viewer = viewer
    this.routeLine = data.routeLine
    this.model = data.model
    const totalTime = data.routeLine.slice(-1)[0].time
    this.start = Cesium.JulianDate.fromDate(new Date(2023, 2, 6))
    this.stop = Cesium.JulianDate.addSeconds(
      this.start,
      totalTime,
      new Cesium.JulianDate()
    )
    this.entity = null
    this.isPause = false
  }

  move() {
    // 设置始时钟始时间
    this.viewer.clock.startTime = this.start.clone()
    // 设置时钟当前时间
    this.viewer.clock.currentTime = this.start.clone()
    // 设置始终停止时间
    this.viewer.clock.stopTime = this.stop.clone()
    // 时间速率，数字越大时间过的越快
    this.viewer.clock.multiplier = 1
    // 时间轴
    this.viewer.timeline.zoomTo(this.start, this.stop)
    this.viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP
    const property = this.computeTrack()
    // 添加模型
    const droneEntity = this.viewer.entities.add({
      name: 'drone',
      // 和时间轴关联
      availability: new Cesium.TimeIntervalCollection([
        new Cesium.TimeInterval({
          start: this.start,
          stop: this.stop
        })
      ]),
      position: property,
      // 根据所提供的速度计算模型的朝向
      orientation: new Cesium.VelocityOrientationProperty(property),
      // 模型数据
      model: {
        id: this.id,
        name: 'drone',
        uri: this.model,
        minimumPixelSize: 32
      },
      path: {
        resolution: 1,
        material: new Cesium.PolylineGlowMaterialProperty({
          glowPower: 0.1,
          color: Cesium.Color.YELLOW
        }),
        width: 10
      },
      viewFrom: new Cesium.Cartesian3(-100, 20, 50)// 观察位置的偏移量
    })
    this.entity = droneEntity
    this.viewer.trackedEntity = droneEntity
    // this.viewer.camera.lookUp(1000)
  }

  computeTrack() {
    const source = this.routeLine
    // 取样位置 相当于一个集合
    const property = new Cesium.SampledPositionProperty()
    for (let i = 0; i < source.length; i++) {
      const time = Cesium.JulianDate.addSeconds(
        this.start,
        source[i].time,
        new Cesium.JulianDate()
      )
      const position = Cesium.Cartesian3.fromDegrees(
        source[i].longitude,
        source[i].latitude,
        source[i].height
      )
      // 添加位置，和时间对应
      property.addSample(time, position)
    }
    return property
  }

  pause() {
    this.isPause = true
    this.viewer.clock.shouldAnimate = false
  }
  play() {
    this.isPause = false
    this.viewer.clock.shouldAnimate = true
  }

  resetPosition() {
    this.viewer.clock.currentTime = this.viewer.clock.startTime
    this.viewer.clock.shouldAnimate = true
  }

  setPositon(time) {
    const positionTime = Cesium.JulianDate.addSeconds(
      this.start,
      time,
      new Cesium.JulianDate()
    )
    this.viewer.clock.currentTime = positionTime
  }

  destroy() {
    if (this.entity) {
      this.viewer.entities.remove(this.entity)
    }
  }

  entityVisible(visible) {
    if (this.entity) {
      this.entity.show = visible
    }
  }
}
