import { LayerView } from '@mapgis/webclient-common'
import { IGSSceneLayerUtil } from '../view/utils'

class IGSSceneLayerView extends LayerView {
  /**
   * 添加图层视图
   * @param {LayerEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onAdd(event) {
    const layer = this.layer
    const innerView = this.innerView
    const self = this
    return new Promise((reslove) => {
      this.innerLayer = []
      IGSSceneLayerUtil.addSceneLayer(
        layer,
        innerView,
        self.view,
        (tileSets) => {
          self.innerLayer = tileSets
          reslove(self)
        }
      )
    })
  }

  /**
   * 移除图层视图
   * @param {LayerEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onRemove(event) {
    IGSSceneLayerUtil.removeLayer(this.innerLayer, this.view, this.innerView)
    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.innerLayer.length; j++) {
                this.innerLayer[j].show = this.layer.visible
              }
              break
            // 更新透明度
            case 'opacity':
              const sceneLayer = this.innerView.scene.layers.getLayer(
                this.layer.layerIndex
              )
              sceneLayer.translucency = this.layer.opacity
              break
            default:
              break
          }
        }
      }
    }
    return Promise.resolve(this)
  }
}
export default IGSSceneLayerView
