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

class ArcGISTileLayerUtil {}

/**
 * 添加瓦片图层
 * @private
 * @param {Layer} layer 基本图层对象
 * @param {Viewer} innerView Cesium的视图对象
 * @return {Object} 创建好的Cesium图层对象
 * */
ArcGISTileLayerUtil.addArcGISTileLayer = function (layer, innerView) {
  const tilingScheme = getTilingSchemeBySpatialReference(
    layer.spatialReference,
    layer.extent,
    layer.tileInfo
  )
  const options = {
    // 请求高德地图瓦片的url模板,{x}、{y}、{z}分别表示瓦片的x坐标、y坐标和层级
    url: `${layer.url}/tile/{z}/{y}/{x}`,
    // 地图瓦片的切分方式,经纬度或者Web墨卡托
    tilingScheme,
    // 瓦片宽度
    tileWidth: layer.tileInfo.cols,
    // 瓦片高度
    tileHeight: layer.tileInfo.cols
  }
  const arcgisTileLayer = new Cesium.UrlTemplateImageryProvider(options)
  // 获取图层index
  const index = getCesiumLayerIndex(layer, innerView)
  innerView.imageryLayers.addImageryProvider(arcgisTileLayer, index)
  arcgisTileLayer.id = layer.id
  arcgisTileLayer._realIndex = layer._realIndex + 1
  return arcgisTileLayer
}

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

export default ArcGISTileLayerUtil
