import {
  addM3DLayer,
  getM3DOptions,
  zoomToM3DLayer
} from './support/utils'

class IGSM3DLayerUtil {}

/**
 * 添加M3D图层
 * @param {Object} layer 图层基本对象
 * @param {View} innerView Cesium的view对象
 * @param {SceneView} sceneView plugin的SceneView对象
 * @param {Function} callback 图层加载成功的回调函数
 * @param {Function} errorCallback 图层加载失败的回调函数
 * */
IGSM3DLayerUtil.addM3DLayer = function (
  layer,
  innerView,
  sceneView,
  callback,
  errorCallback
) {
  const m3dOption = getM3DOptions(layer)
  m3dOption.url = layer.url
  if (layer.url.indexOf('/igs/rest/services/') > -1) {
    // 确定是igs服务
    m3dOption.igserver = true
  } else {
    // 文件服务
    m3dOption.igserver = false
  }
  addM3DLayer(
    m3dOption,
    innerView,
    sceneView,
    function (tileSet) {
      // 请求完毕后自动跳转
      if (layer.autoReset) {
        zoomToM3DLayer(tileSet.boundingSphere, m3dOption, innerView)
      }
      callback(tileSet)
    },
    errorCallback
  )
}

/**
 * 移除图层
 * @param {Object} innerLayer cesium实际图层对象
 * @param {View} innerView cesium的viewer对象
 * @param {SceneView} sceneView plugin的sceneView对象
 * */
IGSM3DLayerUtil.removeLayer = function (innerLayer, innerView, sceneView) {
  sceneView._layersMap.remove(innerLayer.id)
  innerView.scene.primitives.removeAndDestroy(innerLayer)
}

export default IGSM3DLayerUtil
