    import {cloneObject, defaultValue} from '../../../util'
    import {Extent, SpatialReference} from '../../../base/geometry'
    import {SubLayer} from '../baseLayer'
    import BaseRenderer from '../../renderer/BaseRenderer';

    /**
     * IGS地图图片图层的子图层
     * @class IGSMapImageSubLayer
     * @moduleEX LayerManagerModule
     * @extends SubLayer
     * @param {Object} options 构造参数
     * @param {String} [options.url] gdbp路径地址
     * @param {BaseRenderer} [options.renderer] 渲染样式对象，支持如下渲染样式：<br/>
     * [1、单值渲染]{@link UniqueValueRenderer}<br/>
     * [2、分段渲染]{@link ClassBreakRenderer}
     * @param {String} [options.definitionExpression = null] 要素过滤参数，类似sql语句
     * @param {String} [options.name] 图层名称
     * @param {SpatialReference} [options.spatialReference] 图层坐标系
     * @param {Boolean} [options.visible = true] 图层可见性
     * @param {IGSMapImageLayer} [options.layer] 图层的父图层对象
     * @param {String} [options.id] 图层id
     *
     * @example <caption><h7 id='definitionExpression'>过滤要显示的要素 - 初始化时设置</h7></caption>
     * // ES5引入方式
     * const { Map, MapView } = Zondy
     * const { IGSMapImageLayer } = Zondy.Layer
     * // ES6引入方式
     * import { Map, MapView, IGSMapImageLayer  } from "@mapgis/webclient-common"
     * // 初始化地图图片图层
     * const igsMapImageLayer = new IGSMapImageLayer({
     *   url: '服务基地址',
     *   // 设置子图层的要素过滤参数
     *   sublayers: [
     *     {
     *       id: "子图层id",
     *       definitionExpression: "类sql的查询语句"
     *     }
     *   ]
     * });
     * map.add(igsMapImageLayer);
     */
    class IGSMapImageSubLayer extends SubLayer {
      constructor(options) {
        super(options)
        this.url = defaultValue(options.url, '')
        this.name = defaultValue(options.name, '')
        this.type = defaultValue(options.type, '')
        this.fields = defaultValue(options.fields, [])
        this.geomType = defaultValue(options.geomType || options.GeomType, '')
        this.maxScale = defaultValue(options.maxScale, 0)
        this.minScale = defaultValue(options.minScale, 0)
        if (options.range) {
          this.extent = new Extent({
            xmin: options.range.xmin,
            ymin: options.range.ymin,
            xmax: options.range.xmax,
            ymax: options.range.ymax
          })
        } else {
          this.extent = new Extent({
            xmin: -180,
            ymin: -90,
            xmax: 180,
            ymax: 90
          })
        }
        this.spatialReference = options.spatialReference
          ? new SpatialReference(options.spatialReference)
          : new SpatialReference({ wkid: 4326 })
        this.supportedMethods = defaultValue(options.supportedMethods, [])
        this.systemLibGuid = defaultValue(options.systemLibGuid, undefined)
        this.systemLibName = defaultValue(options.systemLibName, undefined)
        this.id = defaultValue(
          options.index || options.id || options.LayerIndex,
          undefined
        )
        this.index = defaultValue(options.index, undefined)
        this.moveIndex = defaultValue(options.moveIndex, undefined)
        this._renderer = BaseRenderer.fromJSON(options.renderer)
        this.layer = defaultValue(options.layer, undefined)
        this._definitionExpression = defaultValue(
          options.definitionExpression,
          undefined
        )
        if (options.children && options.children instanceof Array) {
          for (let i = 0; i < options.children.length; i++) {
            options.children[i].layer = this
            this.sublayers.add(new IGSMapImageSubLayer(options.children[i]))
          }
        }
      }

      /**
       * 将图层转为json对象
       * @return {Object} josn对象
       * */
      toJSON() {
        const json = super.toJSON()
        json.url = this.url
        json.name = this.name
        json.type = this.type
        json.fields = this.fields
        json.geomType = this.geomType
        json.maxScale = this.maxScale
        json.minScale = this.minScale
        json.extent = this.extent.toJSON()
        json.spatialReference = this.spatialReference.toJSON()
        json.supportedMethods = this.supportedMethods
        json.systemLibGuid = this.systemLibGuid
        json.systemLibName = this.systemLibName
        json.index = this.index
        json.moveIndex = this.moveIndex

        return json
      }

      /**
       * 克隆图层对象
       * @return {IGSMapImageSubLayer} 克隆后的图层对象
       */
      clone() {
        return new IGSMapImageSubLayer(this.toJSON())
      }
    }

    Object.defineProperties(IGSMapImageSubLayer.prototype, {
      /**
       * 渲染样式对象
       * @readonly
       * @member {BaseRenderer} IGSMapImageSubLayer.prototype.renderer
       */
      renderer: {
        get() {
          return this._renderer
        },
        set(value) {
          this._renderer = value
          // 更新子图层
          this.layer.setSubLayer(this)
        }
      },
      /**
       * 要素过滤参数，类似sql语句
       * @member {String} IGSMapImageSubLayer.prototype.definitionExpression
       */
      definitionExpression: {
        get() {
          return this._definitionExpression
        },
        set(value) {
          if (value) {
            this._definitionExpression = value
            // 更新主图层
            this.layer.refresh(this)
          }
        }
      }
    })
    export default IGSMapImageSubLayer