import { LayerType } from '@mapgis/webclient-common'
import WMTSLayerUtil from './WMTSLayerUtil'
import WMSLayerUtil from './WMSLayerUtil'
import {
  Cesium3DTilesLayerUtil,
  IGSM3DLayerUtil,
  IGSMapImageLayerUtil,
  IGSSceneLayerUtil,
  IGSTileLayerUtil,
  IGSVectorTileLayerUtil,
  MapVLayerUtil,
  TerrainCacheLayerUtil
} from './index'
import EchartsLayerUtil from './EchartsLayerUtil'
import { getLayerView } from '../../document'

class GroupLayerUtil {}

GroupLayerUtil.addGroupLayer = function (layer, sceneView) {
  const layers = layer.layers
  const innerLayers = []
  for (let i = 0; i < layers.length; i++) {
    switch (layers[i].type) {
      case LayerType.wmts:
        innerLayers.push(WMTSLayerUtil.addWMTSLayer(layers[i], sceneView))
        break
      case LayerType.wms:
        innerLayers.push(WMSLayerUtil.addWMSLayer(layers[i], sceneView))
        break
      case LayerType.igsMapImage:
        innerLayers.push(
          IGSMapImageLayerUtil.addIGSMapImageLayer(layers[i], sceneView)
        )
        break
      case LayerType.igsTile:
        innerLayers.push(IGSTileLayerUtil.addIGSTileLayer(layers[i], sceneView))
        break
      case LayerType.cesium3DTiles:
        Cesium3DTilesLayerUtil.addCesium3DTilesLayer(
          layers[i],
          sceneView,
          function (tileSet) {
            innerLayers.push(tileSet)
          }
        )
        break
      case LayerType.echarts:
        innerLayers.push(EchartsLayerUtil.addEchartsLayer(layers[i], sceneView))
        break
      case LayerType.m3d:
        IGSM3DLayerUtil.addM3DLayer(layers[i], sceneView, function (tileSet) {
          innerLayers.push(tileSet)
        })
        break
      case LayerType.scene:
        IGSSceneLayerUtil.addSceneLayer(
          layers[i],
          sceneView,
          function (tileSet) {
            innerLayers.push(tileSet)
          }
        )
        break
      case LayerType.igsVectorTile:
        innerLayers.push(
          IGSVectorTileLayerUtil.addIGSVectorTileLayer(layers[i], sceneView)
        )
        break
      case LayerType.mapv:
        innerLayers.push(MapVLayerUtil.addMapVLayer(layers[i], sceneView))
        break
      case LayerType.terrain:
        TerrainCacheLayerUtil.addTerrainCacheLayer(
          layers[i],
          sceneView,
          function (tileSet) {
            innerLayers.push(tileSet)
          }
        )
        break
      default:
    }
  }

  return innerLayers
}

/**
 * 设置图层可见性
 * @param {Layer} subLayer 基础子图层对象
 * @param {Object} innerLayer 图层对象
 * @param {Boolean} visible 基础子图层可见性
 * */
GroupLayerUtil.setLayerVisible = function (subLayer, innerLayer, visible) {
  switch (subLayer.type) {
    case LayerType.echarts:
    case LayerType.mapv:
      if (visible) {
        innerLayer.show()
      } else {
        innerLayer.hide()
      }
      break
    case LayerType.wms:
    case LayerType.wmts:
    case LayerType.igsMapImage:
    case LayerType.igsTile:
    case LayerType.m3d:
    case LayerType.scene:
    case LayerType.cesium3DTiles:
    case LayerType.terrain:
      innerLayer.show = visible
      break
    default:
      break
  }
}

/**
 * 移除子图层
 * @param {Layer} layer 基础图层对象
 * @param {Object} innerLayer 视图中的图层对象
 * @param {Viewer} sceneView Cesium的视图对象
 * */
GroupLayerUtil.removeLayer = function (layer, innerLayer, sceneView) {
  switch (layer.type) {
    case LayerType.cesium3DTiles:
      Cesium3DTilesLayerUtil.removeLayer(layer, sceneView)
      break
    case LayerType.echarts:
      EchartsLayerUtil.removeLayer(innerLayer)
      break
    case LayerType.m3d:
      IGSM3DLayerUtil.removeLayer(layer, sceneView)
      break
    case LayerType.igsMapImage:
      IGSMapImageLayerUtil.removeLayer(innerLayer, sceneView)
      break
    case LayerType.scene:
      IGSSceneLayerUtil.removeLayer(layer, sceneView)
      break
    case LayerType.igsTile:
      IGSTileLayerUtil.removeLayer(innerLayer, sceneView)
      break
    case LayerType.igsVectorTile:
      IGSVectorTileLayerUtil.removeLayer(innerLayer, sceneView)
      break
    case LayerType.mapv:
      MapVLayerUtil.removeLayer(innerLayer)
      break
    case LayerType.terrain:
      TerrainCacheLayerUtil.removeLayer(layer, sceneView)
      break
    case LayerType.wmts:
      WMTSLayerUtil.removeLayer(innerLayer, sceneView)
      break
    case LayerType.wms:
      WMSLayerUtil.removeLayer(innerLayer, sceneView)
      break
    default:
      break
  }
}

/**
 * 获取所有子图层的view对象
 * @param {GroupLayer} layer 组图层基础对象
 * @param {Array} innerLayers 所有实际的子图层对象
 * @param {BaseView} baseView 逻辑视图对象
 * @return {Array} 所有子图层的view对象数组
 * */
GroupLayerUtil.getLayerViews = function (layer, innerLayers, baseView) {
  const layers = layer.layers
  const layerViews = []
  for (let i = 0; i < layers.length; i++) {
    const layerView = getLayerView(baseView, layers[i])
    for (let j = 0; j < innerLayers.length; j++) {
      if (innerLayers[j].id === layers[i].id) {
        layerView.innerLayer = innerLayers[j]
        break
      }
    }
    layerViews.push(layerView)
  }
  return layerViews
}

export default GroupLayerUtil
