    import { Layer } from './baseLayer'
    import { Zondy } from '../../base'
    import { defaultValue } from '../../util'
    import { LayerType, LoadStatus } from '../../base/enum'

    /**
     * 组图层，支持的图层类型有：WMS图层、WMTS图层、地图图层、瓦片图层、cesium3DTiles图层、echarts图层、
     * M3D图层、场景图层、矢量瓦片图层、mapv图层、地形图层
     * <br><br>[ES5引入方式]：<br/>
     * Zondy.Layer.GroupLayer() <br/>
     * [ES6引入方式]：<br/>
     * import { GroupLayer } from "@mapgis/webclient-common" <br/>
     * <br/>
     * @class GroupLayer
     * @moduleEX LayerModule
     * @extends Layer
     * @param {Object} options 构造参数
     * @param {Array} [options.layers = []] 子图层数组
     * @param {Boolean} [options.visible = true] 图层可见性
     * @param {Number} [options.opacity = 1] 图层透明度，0到1之间的数字
     * @example <caption><h7>组图层示例</h7></caption>
     * // 初始化图层管理容器
     * // ES5引入方式
     * const { Map ,SceneView } = Zondy
     * const { IGSMapImageLayer,WMTSLayer,GroupLayer } = Zondy.Layer
     * // ES6引入方式
     * import { Map ,SceneView, IGSMapImageLayer,WMTSLayer,GroupLayer} from "@mapgis/webclient-common"
     * const map = new Map();
     * // 初始化地图视图对象
     * const sceneView = new SceneView({
     *   // 视图id
     *   viewId: "mapgis-3d-viewer",
     *   // 图层管理容器
     *   map: map
     * });
     * // 创建一个矢量图层
     * const igsMapImageLayer = new IGSMapImageLayer({
     *   url: 'http://192.168.82.89:8089/igs/rest/services/Map/WorldJWVector/MapServer'
     * });
     * // 创建一个WMS图层
     * const wmsLayer = new WMTSLayer({
     *   url: 'http://192.168.82.89:8089/igs/rest/services/Tile/北京市/WMTSServer'
     * });
     * // 创建一个组图层
     * const groupLayer = new GroupLayer({
     *   // 添加子图层
     *   layers: [igsMapImageLayer, wmsLayer]
     * })
     * // 添加组图层
     * map.add(groupLayer);
     */
    class GroupLayer extends Layer {
      constructor(options) {
        super(options)
        options = defaultValue(options, {})

        /**
         * 图层类型
         * @member {String} GroupLayer.prototype.url
         */
        this.type = LayerType.group
        // 暂时无法修改子图层的属性
        /**
         * 子图层数组
         * @member {Array} GroupLayer.prototype.layers
         */
        this.layers = defaultValue(options.layers, [])
        /**
         * 所有子图层数组
         * @member {Array} GroupLayer.prototype.allLayers
         */
        this.allLayers = []
      }

      /**
       * 从igs获取图层信息的方法
       */
      load() {
        const self = this
        return new Promise((resolve) => {
          for (let i = 0; i < self.layers.length; i++) {
            self.layers[i].load()
            self.allLayers.push(self.layers[i])
          }
          const interval = setInterval(function () {
            let loadedAll = true
            for (let i = 0; i < self.layers.length; i++) {
              if (!(self.layers[i].loadStatus === 'loaded')) {
                loadedAll = false
                break
              }
            }
            if (loadedAll) {
              clearInterval(interval)
              self.loadStatus = LoadStatus.loaded
              resolve(self)
            }
          }, 10)
        })
      }

      /**
       * 删除子图层
       * @param {Object} layer 图层对象
       * */
      remove(layer) {
        this._fireUpdateEvent('删除子图层', [
          {
            params: [layer],
            dataChanged: true,
            name: 'removeSubLayer',
            operationType: 'method'
          }
        ])
        for (let i = 0; i < this.layers.length; i++) {
          if (this.layers[i].id === layer.id) {
            this.layers.splice(i, 1)
            this.allLayers.splice(i, 1)
          }
        }
      }

      /**
       * 添加子图层
       * @param {Object} layer 图层对象
       * */
      add(layer) {
        this.layers.push(layer)
        this.allLayers.push(layer)
        this._fireUpdateEvent('添加子图层', [
          {
            params: [layer],
            dataChanged: true,
            name: 'addSubLayer',
            operationType: 'method'
          }
        ])
      }

      /**
       * 获取组图层中的所有子图层数组
       * @return {Array} 所有子图层数组
       * */
      getFlatLayers() {
        const allLayers = []
        for (let i = 0; i < this.allLayers.length; i++) {
          if (this.allLayers[i].type !== LayerType.group) {
            allLayers.push(this.allLayers[i])
          }
        }
        return allLayers
      }

      /**
       * 通过图层id获取子图层
       * @param {String} layerId 子图层id
       * @return {Layer} 子图层对象
       * */
      findLayerById(layerId) {
        let subLayer = undefined
        for (let i = 0; i < this.layers.length; i++) {
          if (this.layers[i].id === layerId) {
            subLayer = this.layers[i]
            break
          }
        }
        return subLayer
      }

      /**
       * 移除所有子图层
       * */
      removeAll() {
        this._fireUpdateEvent('删除所有子图层', [
          {
            params: [],
            dataChanged: true,
            name: 'removeAllSubLayer',
            operationType: 'method'
          }
        ])
        this.layers = []
        this.allLayers = []
      }

      /**
       * 将图层转为json对象
       * @return {Object} josn对象
       * */
      toJSON() {
        const json = super.toJSON()
        this.layers = this.layers.map((layer) => layer.clone())
        return JSON.parse(JSON.stringify(json))
      }

      /**
       * 克隆并返回一个新图层
       * @return {GroupLayer} 新的组图层对象
       * */
      clone() {
        return new GroupLayer(this.toJSON())
      }

      /**
       * @function setMap
       * @description 设置图层管理容器
       */
      setMap(map) {
        super.setMap(map)
        for (let i = 0; i < this.layers.length; i++) {
          this.layers[i].setMap(map)
        }
      }
    }

    Zondy.Layer.GroupLayer = GroupLayer
    export default GroupLayer