import { LayerView, LayerType } from '@mapgis/webclient-common'
import { getLayerIndex } from './support/Utils'
import GroupLayerUtil from '../view/utils/GroupLayerUtil'
import {
  Cesium3DTilesLayerUtil,
  IGSMapImageLayerUtil,
  IGSSceneLayerUtil,
  IGSVectorTileLayerUtil,
  TerrainCacheLayerUtil
} from '../view/utils'
import EchartsLayerUtil from '../view/utils/EchartsLayerUtil'
import IGSM3DLayerUtil from '../view/utils/IGSM3DLayerUtil'
import IGSTileLayerUtil from '../view/utils/IGSTileLayerUtil'
import MapVLayerUtil from '../view/utils/MapVLayerUtil'
import WMTSLayerUtil from '../view/utils/WMTSLayerUtil'
import WMSLayerUtil from '../view/utils/WMSLayerUtil'

class GroupLayerView extends LayerView {
  /**
   * 添加图层视图
   * @param {LayerEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onAdd(event) {
    const layer = this.layer
    const innerView = this.innerView
    // 获取map里的所有图层
    const items = this.view._map.layers.items
    // 获取当前图层的上一个图层的index
    const index = getLayerIndex(items, layer)
    const self = this
    return new Promise((resolve) => {
      // 确保上一个图层的资源加载完毕
      const interval = setInterval(function () {
        if (items[index].loadStatus === 'loaded') {
          // 保存所有的子图层
          self.innerLayer = GroupLayerUtil.addGroupLayer(layer, innerView)
          // 保存所有的子图层的view对象，更新子图层会用到
          self.layerViews = GroupLayerUtil.getLayerViews(
            layer,
            self.innerLayer,
            self.view
          )
          clearInterval(interval)
          resolve(self)
        }
      }, 1)
    })
  }

  /**
   * 移除图层视图
   * @param {LayerEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onRemove(event) {
    for (let i = 0; i < this.layer.layers.length; i++) {
      switch (this.layer.layers[i].type) {
        case LayerType.wms:
        case LayerType.wmts:
        case LayerType.igsTile:
        case LayerType.igsMapImage:
          this.innerView.scene.imageryLayers.remove(this.innerLayer[i])
          break
        case LayerType.echarts:
        case LayerType.mapv:
          this.innerLayer[i].remove()
          break
        case LayerType.cesium3DTiles:
          this.innerView.scene.layers.removeCesium3DTilesetLayerByID(
            this.innerLayer[i].layerIndex
          )
          break
        case LayerType.m3d:
          this.innerView.scene.layers.removeM3DLayerByID(
            this.innerLayer[i].layerIndex
          )
          break
        case LayerType.scene:
          this.innerView.scene.layers.removeSceneLayerByID(
            this.innerLayer[i].layerIndex
          )
          break
        case LayerType.terrain:
          this.innerView.scene.layers.removeSceneLayerByID(
            this.innerLayer[i].layerIndex
          )
          break
        case LayerType.igsVectorTile:
          const imageryLayers = this.innerView.imageryLayers
          imageryLayers.remove(this.innerLayer[i])
          break
        default:
      }
    }
    return Promise.resolve(this)
  }

  /**
   * 更新图层视图
   * @param {LayerViewUpdateEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onUpdate(event) {
    // 确保有updateContent
    if (event.updateContent) {
      for (let i = 0; i < event.updateContent.length; i++) {
        const updateContent = event.updateContent[i]
        // 有updateContent
        if (updateContent) {
          switch (updateContent.name) {
            // 更新主图层可见性
            case 'visible':
              for (let j = 0; j < this.layer.layers.length; j++) {
                GroupLayerUtil.setLayerVisible(
                  this.layer.layers[j],
                  this.innerLayer[j],
                  this.layer.visible
                )
              }
              break
            // 更新透明度
            case 'opacity':
              for (let j = 0; j < this.layer.layers.length; j++) {
                switch (this.layer.layers[j].type) {
                  case LayerType.echarts:
                  case LayerType.mapv:
                    this.innerLayer[j].setOpacity(this.layer.opacity)
                    break
                  case LayerType.wms:
                  case LayerType.wmts:
                  case LayerType.igsMapImage:
                  case LayerType.igsTile:
                    this.innerLayer[j].alpha = this.layer.opacity
                    break
                  case LayerType.terrain:
                    TerrainCacheLayerUtil.setOpacity(this.layer, this.innerView)
                    break
                  default:
                }
              }
              break
            // 删除子图层
            case 'removeSubLayer':
              const layer = updateContent.params[0]
              for (let j = 0; j < this.layer.layers.length; j++) {
                if (this.layer.layers[j].id === layer.id) {
                  GroupLayerUtil.removeLayer(
                    this.layer.layers[j],
                    this.innerLayer[j],
                    this.innerView
                  )
                  break
                }
              }
              break
            // 删除所有子图层
            case 'removeAllSubLayer':
              for (let j = 0; j < this.layer.layers.length; j++) {
                GroupLayerUtil.removeLayer(
                  this.layer.layers[j],
                  this.innerLayer[j],
                  this.innerView
                )
              }
              this.innerLayer = []
              break
            // 添加子图层
            case 'addSubLayer':
              const self = this
              const addLayer = updateContent.params[0]
              for (let j = 0; j < this.layer.layers.length; j++) {
                if (this.layer.layers[j].id === addLayer.id) {
                  switch (this.layer.layers[j].type) {
                    case LayerType.cesium3DTiles:
                      Cesium3DTilesLayerUtil.addCesium3DTilesLayer(
                        addLayer,
                        self.innerView,
                        function (tileSet) {
                          self.innerLayer.push(tileSet)
                        }
                      )
                      break
                    case LayerType.echarts:
                      self.innerLayer.push(
                        EchartsLayerUtil.addEchartsLayer(
                          addLayer,
                          self.innerView
                        )
                      )
                      break
                    case LayerType.m3d:
                      IGSM3DLayerUtil.addM3DLayer(
                        addLayer,
                        self.innerView,
                        function (tileSet) {
                          self.innerLayer.push(tileSet)
                        }
                      )
                      break
                    case LayerType.igsMapImage:
                      self.innerLayer.push(
                        IGSMapImageLayerUtil.addIGSMapImageLayer(
                          addLayer,
                          self.innerView
                        )
                      )
                      break
                    case LayerType.scene:
                      IGSSceneLayerUtil.addSceneLayer(
                        addLayer,
                        self.innerView,
                        function (tileSet) {
                          self.innerLayer.push(tileSet)
                        }
                      )
                      break
                    case LayerType.igsTile:
                      self.innerLayer.push(
                        IGSTileLayerUtil.addIGSTileLayer(
                          addLayer,
                          self.innerView
                        )
                      )
                      break
                    case LayerType.igsVectorTile:
                      self.innerLayer.push(
                        IGSVectorTileLayerUtil.addIGSVectorTileLayer(
                          addLayer,
                          self.innerView
                        )
                      )
                      break
                    case LayerType.mapv:
                      self.innerLayer.push(
                        MapVLayerUtil.addMapVLayer(addLayer, self.innerView)
                      )
                      break
                    case LayerType.terrain:
                      TerrainCacheLayerUtil.addTerrainCacheLayer(
                        addLayer,
                        self.innerView,
                        function (tileSet) {
                          self.innerLayer.push(tileSet)
                        }
                      )
                      break
                    case LayerType.wmts:
                      self.innerLayer.push(
                        WMTSLayerUtil.addWMTSLayer(addLayer, self.innerView)
                      )
                      break
                    case LayerType.wms:
                      self.innerLayer.push(
                        WMSLayerUtil.addWMSLayer(addLayer, self.innerView)
                      )
                      break
                    default:
                      break
                  }
                  break
                }
              }
              break
            default:
              break
          }
        }
      }
    }
    return Promise.resolve(this)
  }
}
export default GroupLayerView
