import {
  getCesiumLayerIndex,
  getTilingSchemeBySpatialReference
} from './support/utils'

class IGSTileLayerUtil {}

/**
 * 添加瓦片图层
 * @private
 * @param {Layer} layer 基本图层对象
 * @param {Viewer} innerView Cesium的视图对象
 * @return {Object} 创建好的Cesium图层对象
 * */
IGSTileLayerUtil.addIGSTileLayer = function (layer, innerView) {
  const tilingScheme = getTilingSchemeBySpatialReference(
    layer.spatialReference,
    layer.extent,
    layer.tileInfo
  )

  const options = {
    // 服务基地址
    url: layer.url,
    // 瓦片宽度
    tileWidth: layer.tileInfo.cols,
    // 瓦片高度
    tileHeight: layer.tileInfo.cols,
    // 地图瓦片的切分方式，经纬度、Web墨卡托或自定义投影
    tilingScheme
  }
  // 获取图层index
  const index = getCesiumLayerIndex(layer, innerView)
  // 加载瓦片图层
  const igsTileLayer = innerView.imageryLayers.addImageryProvider(
    new Cesium.MapGISTileServerImageProvider(options),
    index
  )

  igsTileLayer.id = layer.id
  igsTileLayer._realIndex = layer._realIndex + 1
  return igsTileLayer
}

/**
 * 移除图层
 * @param {Object} layer 图层对象
 * @param {Viewer} innerView Cesium的视图对象
 * */
IGSTileLayerUtil.removeLayer = function (layer, innerView) {
  innerView.imageryLayers.remove(layer, true)
}

export default IGSTileLayerUtil
