import Position from '../position/Position'

const { Cesium } = BC.Namespace

class Fire {
  constructor(pos, opt) {
    if (!(pos instanceof Position)) {
      pos = new Position()
    }
    var expOpt = {
      image: '/libs/resources/static/fire.png',
      startColor: [255, 0, 0, 0.7],
      endColor: [255, 0, 0, 0.3],
      startScale: 0,
      endScale: 2,
      minimumParticleLife: 1,
      maximumParticleLife: 6,
      minimumSpeed: 1,
      maximumSpeed: 4,
      imageSize: [30, 30],
      sizeInMeters: true,
      emissionRate: 4,
      lifetime: 160
    }
    this.position = pos
    this.opt = Object.assign(expOpt, opt)
    this.entity = null
    this.pointEntity = null
  }

  init() {
    if (!window.viewer) {
      console.error('地球未初始化，火加载失败')
    }
    //粒子系统的起点，发射源
    var staticPosition = Cesium.Cartesian3.fromDegrees(
      this.position.lng,
      this.position.lat,
      this.position.alt
    )
    this.pointEntity = viewer.entities.add({
      position: staticPosition
    })
    this.entity = viewer.scene.primitives.add(
      new Cesium.ParticleSystem({
        image: this.opt.image,
        startColor: Cesium.Color.fromBytes(
          this.opt.startColor[0],
          this.opt.startColor[1],
          this.opt.startColor[2],
          this.opt.startColor[3] * 255
        ),
        endColor: Cesium.Color.fromBytes(
          this.opt.endColor[0],
          this.opt.endColor[1],
          this.opt.endColor[2],
          this.opt.endColor[3] * 255
        ),
        startScale: this.opt.startScale,
        endScale: this.opt.endScale,
        //设定粒子寿命可能持续时间的最小限值(以秒为单位)，在此限值之上将随机选择粒子的实际寿命。
        minimumParticleLife: this.opt.minimumParticleLife,
        maximumParticleLife: this.opt.maximumParticleLife,
        minimumSpeed: this.opt.minimumSpeed,
        maximumSpeed: this.opt.maximumSpeed,
        imageSize: new Cesium.Cartesian2(
          this.opt.imageSize[0],
          this.opt.imageSize[1]
        ),
        sizeInMeters: this.opt.sizeInMeters,
        emissionRate: this.opt.emissionRate, //火焰大小
        lifetime: this.opt.lifetime,
        emitter: new Cesium.CircleEmitter(5.0),
        modelMatrix: this.firecomputeModelMatrix(this.pointEntity),
        emitterModelMatrix: this.firecomputeEmitterModelMatrix()
      })
    )
  }

  firecomputeModelMatrix(entity) {
    var position = Cesium.Property.getValueOrUndefined(entity.position)
    let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(position)
    return modelMatrix
  }

  firecomputeEmitterModelMatrix() {
    let hpr = Cesium.HeadingPitchRoll.fromDegrees(
      this.position.heading,
      this.position.pitch,
      this.position.roll
    )
    let trs = new Cesium.TranslationRotationScale()
    trs.translation = Cesium.Cartesian3.fromElements(2.5, 4, 1)
    trs.rotation = Cesium.Quaternion.fromHeadingPitchRoll(hpr)
    let result = Cesium.Matrix4.fromTranslationRotationScale(trs)
    return result
  }

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

  stop() {
    this.entity ? (this.entity.show = false) : ''
  }

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

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

export default Fire
