const { Cesium } = BC.Namespace

class Pipeline {
  constructor(lineArr, opt = { color: [229, 18, 18], radius: 1, angle: 15}) {
    this.lineArr = lineArr
    this.opt = opt
    this.init()
  }

  init() {
    if (!window.viewer) {
      console.error('地球未初始化，管线加载失败')
    }
    this.entity = viewer.entities.add({
      polylineVolume: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights(this.lineArr),
        shape: this.computeCircle(this.opt.radius),
        material: Cesium.Color.fromBytes(this.opt.color[0], this.opt.color[1], this.opt.color[2]),
      }
    })
  }

  computeCircle(radius) {
    var positions = [];
    for (var i = 0; i < 360; i++) {
      var radians = Cesium.Math.toRadians(i);
      positions.push(
        new Cesium.Cartesian2(
          radius * Math.cos(radians),
          radius * Math.sin(radians)
        )
      )
      i += this.opt.angle
    }
    return positions
  }

  start() {
    if (this.entity) {
      this.entity.show = true
      return
    }
    this.init()
  }

  stop() {
    if (!this.entity) return
    this.entity.show = false
  }

  uninstall() {
    if (!this.entity) return
    viewer.entities.remove(this.entity)
    this.entity = null
  }

  zoomTo() {
    viewer.zoomTo(this.entity)
  }
}

export default Pipeline
