/* eslint-disable no-undef */
// const layerManager = {
//   add3DTilesetByUrl (url) {
//     const tileset = new Cesium.Cesium3DTileset({ url: '../scripts/Cesium/b/tileset.json' })
//     scene.primitives.add(tileset)
//     viewer.zoomTo(tileset)
//   }
// }
// export default layerManager

class LayerManager {
  constructor(viewer) {
    this.viewer = viewer
    this.tilesetList = []
  }

  async add3DTilesetByUrl(url, name) {
    // const tileset = new Cesium.Cesium3DTileset({ url: url })
    const tileset = await Cesium.Cesium3DTileset.fromUrl(url, {
      maximumScreenSpaceError: 16,
      skipLevelOfDetail: true,
      preferLeaves: true,
      maximumMemoryUsage: 512
    })
    // const tileset = Cesium.Cesium3DTileset.fromUrl(url)
    this.viewer.scene.primitives.add(tileset)
    // const boundingSphere = tileset.boundingSphere
    // const cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center)// 获取到倾斜数据中心点的经纬度坐标（弧度）
    // const surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0)// 倾斜数据中心点的笛卡尔坐标
    // const positions = [Cesium.Cartographic.fromDegrees(cartographic.longitude, cartographic.latitude)]
    // const promise = Cesium.sampleTerrainMostDetailed(terrainProvider, positions)// 其中terrainProvider是当前场景使用的高程Provider
    // Cesium.when(promise, function (updatedPositions) {
    //   const terrainHeight = updatedPositions[0].height// 高程
    //   const offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, terrainHeight)// 带高程的新笛卡尔坐标
    //   const translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3())// 做差得到变换矩阵
    //   tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
    // })

    this.tilesetList.push({ name, tileset })

    this.viewer.zoomTo(tileset)
  }

  addPoint(pointList) {
    const logoUrl = '/img/monitor_icon.png'
    pointList.forEach(point => {
      const position = Cesium.Cartesian3.fromDegrees(point.longitude * 1, point.latitude * 1, point.height * 1)
      const entity = viewer.entities.add({
        id: point?.id || null,
        position: position,
        billboard: {
          image: logoUrl,
          scaleByDistance: new Cesium.NearFarScalar(1.5e2, 0.5, 7.0e6, 0.0)
        },
        props: point
      })
    })
  }
}

export default LayerManager
