    import { Zondy } from '../../../base'
    import { SubLayer } from '../baseLayer'
    import { defaultValue, getGUID, toJSON } from '../../../util'
    import { Extent, Point } from '../../../base/geometry'

    /**
     * IGS的SceneLayer子图层
     * @class IGSSceneSubLayer
     * @moduleEX LayerModule
     * @extends SubLayer
     * @param {Object} options 构造参数
     * @param {Object} [options.extendOptions] 初始化图层的额外参数，三维引擎专有的初始化参数请传入此对象
     */
    class IGSSceneSubLayer extends SubLayer {
      constructor(options) {
        super(options)
        options = defaultValue(options, {})
        /**
         * 子图层的id
         * @member {String} IGSSceneSubLayer.prototype.id
         */
        this.id = defaultValue(options.layerIndex, getGUID())
        /**
         * 子图层的索引
         * @member {Number} IGSSceneSubLayer.prototype.layerIndex
         */
        this.layerIndex = defaultValue(options.layerIndex, 0)
        this.layerRenderIndex = defaultValue(options.layerRenderIndex, 0)

        /**
         * 子图层类型
         * @member {String} IGSSceneSubLayer.prototype.type
         */
        this.type = defaultValue(options.layerType, undefined)

        /**
         * 子图层渲染类型
         * @member {String} IGSSceneSubLayer.prototype.renderType
         */
        this.renderType = defaultValue(options.layerRenderType, undefined)

        /**
         * 开始级别
         * @member {Number} IGSSceneSubLayer.prototype.beginLevel
         */
        this.beginLevel = defaultValue(options.beginLevel, -1)

        /**
         * 结束级别
         * @member {Number} IGSSceneSubLayer.prototype.endLevel
         */
        this.endLevel = defaultValue(options.endLevel, -1)

        /**
         * 网格划分的源点
         * @member {Point} IGSSceneSubLayer.prototype.originPoint
         */
        if (options.originalPoints) {
          this.originPoint = new Point({
            coordinates: [options.originalPoints.x, options.originalPoints.y]
          })
        } else {
          this.originPoint = undefined
        }

        /**
         * 最大网格大小
         * @member {Number} IGSSceneSubLayer.prototype.maxFrameSize
         */
        if (options.frameSizes) {
          this.maxFrameSize = options.frameSizes[0]
        } else {
          this.maxFrameSize = undefined
        }

        /**
         * 初始化图层的额外参数，三维引擎专有的初始化参数请传入此对象
         * @member {Object} IGSSceneSubLayer.prototype.extendOptions
         */
        this.extendOptions = defaultValue(options.extendOptions, {})

        /**
         * 子图层服务地址
         * @member {String} IGSSceneSubLayer.prototype.url
         */
        this.url = defaultValue(options.url || options.gdbpUrl, undefined)

        /**
         * 子图层服范围(二维)
         * @member {Extent} IGSSceneSubLayer.prototype.range
         */
        if (options.range) {
          this.range = new Extent({
            xmin: options.range.xMin,
            ymin: options.range.yMin,
            xmax: options.range.xMax,
            ymax: options.range.yMax
          })
        } else {
          this.range = undefined
        }

        /**
         * 子图层服范围(三维)
         * @member {Extent} IGSSceneSubLayer.prototype.range3D
         */
        if (options.range3D) {
          this.range3D = new Extent({
            xmin: options.range3D.xMin,
            ymin: options.range3D.yMin,
            xmax: options.range3D.xMax,
            ymax: options.range3D.yMax,
            zmin: options.range3D.zMin,
            zmax: options.range3D.zMax
          })
        } else {
          this.range3D = undefined
        }

        /**
         * 子图层所属的场景服务图层
         * @member {Layer} SubLayer.prototype.layer
         */
        this.layer = defaultValue(options.layer, undefined)
        if (
          options.children &&
          options.children instanceof Array &&
          options.children.length > 0
        ) {
          for (let i = 0; i < options.children.length; i++) {
            options.children[i].layer = this.layer
            this.sublayers.push(new IGSSceneSubLayer(options.children[i]))
          }
        }
        this.elevationScale = defaultValue(options.elevationScale, undefined)
        this.requestVertexNormals = defaultValue(
          options.requestVertexNormals,
          false
        )
        this.labelLayer = defaultValue(options.labelLayer, undefined)
        this.layerName = defaultValue(options.layerName, undefined)
      }

      /**
       * 将对象转为json对象
       * @return {Object} json对象
       * */
      toJSON() {
        const _sublayers = []
        for (let i = 0; i < this.sublayers.length; i++) {
          _sublayers.push(toJSON(this.sublayers[i], IGSSceneSubLayer))
        }

        const json = {
          id: this.id,
          sublayers: this.sublayers,
          title: this.title,
          visible: this.visible,
          layerIndex: this.layerIndex,
          type: this.type,
          renderType: this.renderType,
          beginLevel: this.beginLevel,
          endLevel: this.endLevel,
          originPoint: this.originPoint,
          maxFrameSize: this.maxFrameSize,
          extendOptions: this.extendOptions
        }

        return json
      }
    }

    /**
     * 通过json对象初始化该对象
     * @param {Object} json json对象
     * @memberof IGSSceneSubLayer
     */
    IGSSceneSubLayer.fromJSON = function (json) {
      return new IGSSceneSubLayer(json)
    }

    Zondy.Layer.IGSSceneSubLayer = IGSSceneSubLayer
    export default IGSSceneSubLayer