
class FlyManager {
  constructor (viewer) {
    this.viewer = viewer
    this.currentSite = 0
    this.routeLine = null
    this.isLoop = false
    this.status = 'init'
  }

  /* 初始化飞行浏览 */
  initFly (routeLine) {
    this.status = 'init'
    this.currentSite = 0
    this.routeLine = routeLine

    const scene = this.viewer.scene
    scene.screenSpaceCameraController.enableRotate = false
    scene.screenSpaceCameraController.enableTranslate = false
    scene.screenSpaceCameraController.enableZoom = false
    scene.screenSpaceCameraController.enableTilt = false
    scene.screenSpaceCameraController.enableLook = false

    const viewer = this.viewer
    viewer.camera.setView({
    //   destination: Cesium.Cartesian3(this.routeLine[0].longitude, this.routeLine[0].latitude, this.routeLine[0].height),
      destination: Cesium.Cartesian3.fromDegrees(routeLine[0].longitude, routeLine[0].latitude, routeLine[0].height),
      orientation: {
        heading: this.routeLine[0].heading,
        pitch: this.routeLine[0].pitch,
        roll: this.routeLine[0].roll
      }
    })

    this.fly()
  }

  /* 飞行浏览具体 */
  fly () {
    if (this.status === 'stop') {
      this.initFly(this.routeLine)
    }
    this.status = 'fly'
    if (this.currentSite >= this.routeLine.length - 1) {
      if (this.isLoop) {
        this.initFly(this.routeLine)
      } else {
        this.stopFly()
      }
    } else {
      let i = this.currentSite + 1
      if (i < 1) {
        this.currentSite = 0
        i = 1
      }
      this.viewer.camera.flyTo({
        // 设置位置
        // destination: Cesium.Cartesian3(this.routeLine[i].longitude, this.routeLine[i].latitude, this.routeLine[i].height),
        destination: Cesium.Cartesian3.fromDegrees(this.routeLine[i].longitude, this.routeLine[i].latitude, this.routeLine[i].height),
        orientation: {
          // 方向
          heading: this.routeLine[this.currentSite].heading,
          // 倾斜角度
          pitch: this.routeLine[this.currentSite].pitch,
          roll: 0
        },
        maximumHeight: this.routeLine[this.currentSite].height * 1,
        // 设置飞行持续时间，默认会根据距离来计算
        duration: 4,
        complete: () => {
          this.currentSite = this.currentSite + 1
          console.log('complete', this.routeLine[this.currentSite].height)
          this.fly()
        },
        cancel: () => {
          // this.currentSite--
        },
        easingFunction: Cesium.EasingFunction.LINEAR_NONE
      })
    }
  }

  /* 直播飞行，按点飞 */
  //   liveFly (nextSite) {
  //     this.viewer.camera.flyTo({
  //       // 设置位置
  //       destination: Cesium.Cartesian3.fromDegrees(nextSite.longitude, nextSite.latitude, nextSite.height),
  //       orientation: {
  //         // 方向
  //         heading: nextSite.heading,
  //         // 倾斜角度
  //         pitch: nextSite.pitch,
  //         roll: nextSite.roll
  //       },
  //       // 设置飞行持续时间，默认会根据距离来计算
  //       duration: 4,
  //       complete: () => {
  //         this.liveFly()
  //       }
  //     })
  //   }

  pauseFly () {
    if (this.status !== 'fly') return
    this.status = 'pause'
    this.viewer.camera.cancelFlight()
  }

  /* 停止飞行浏览 */
  stopFly () {
    this.status = 'stop'
    this.currentSite = 0
    const scene = this.viewer.scene
    scene.screenSpaceCameraController.enableRotate = true
    scene.screenSpaceCameraController.enableTranslate = true
    scene.screenSpaceCameraController.enableZoom = true
    scene.screenSpaceCameraController.enableTilt = true
    scene.screenSpaceCameraController.enableLook = true
  }
}

export default FlyManager
