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

class TerrainCacheLayerView extends LayerView {
  /**
   * 添加图层视图
   * @param {LayerEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onAdd(event) {
    const layer = this.layer
    const innerView = this.innerView
    const that = this
    return new Promise((reslove) => {
      TerrainCacheLayerUtil.addTerrainCacheLayer(layer, innerView, (v) => {
        this.innerLayer = v
        reslove(that)
      })
    })
  }

  /**
   * 移除图层视图
   * @param {LayerEvent} event 图层事件
   * @return {Promise<LayerView>} 图层视图
   */
  onRemove(event) {
    TerrainCacheLayerUtil.removeLayer(this.layer, 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':
              this.innerLayer.show = this.layer.visible
              break
            // 更新透明度
            case 'opacity':
              TerrainCacheLayerUtil.setOpacity(this.layer, this.innerView)
              break
            default:
          }
        }
      }
    }
  }
}
export default TerrainCacheLayerView
