    import Layer from './Layer'
    import { defaultValue } from '../../../util'
    import { Collection, Projection } from '../../../base'
    import Extent from '../../../base/geometry/Extent'

    /**
     * 影像图层基类
     * @class MapImageLayer
     * @moduleEX LayerManagerModule
     * @classdesc 影像图层基类
     * @extends Layer
     * @param {Object} options 构造参数
     * @param {Number} [options.minScale = 0] 最小缩放级别
     * @param {Number} [options.maxScale = 20] 最大缩放级别
     * @param {String} [options.tokenKey = undefined] token名
     * @param {String} [options.tokenValue = undefined] token值
     */
    class MapImageLayer extends Layer {
      constructor(options) {
        super(options)

        options = defaultValue(options, {})

        /**
         * 服务基地址
         * @readonly
         * @member {String} MapImageLayer.prototype.url
         */
        this.url = defaultValue(options.url, '')

        /**
         * 地图名称
         * @readonly
         * @member {String} MapImageLayer.prototype.mapName
         */
        this.mapName = defaultValue(options.mapName, undefined)

        /**
         * 地图文档信息
         * @readonly
         * @member {String} MapImageLayer.prototype.documentInfo
         */
        this.documentInfo = defaultValue(options.documentInfo, undefined)
        /**
         * 可提供的服务
         * @readonly
         * @member {String} MapImageLayer.prototype.capabilities
         */
        this.capabilities = []

        /**
         * 图片宽度
         * @member {Number} MapImageLayer.prototype.imageWidth
         */
        this.imageWidth = defaultValue(options.imageWidth, 256)
        /**
         * 图片高度
         * @member {Number} MapImageLayer.prototype.imageHeight
         */
        this.imageHeight = defaultValue(options.imageHeight, 256)
        /**
         * 图片格式
         * @member {String} MapImageLayer.prototype.imageFormat
         */
        this.imageFormat = defaultValue(options.imageFormat, 'png')
        /**
         * 图片中没有数据的地方是否透明
         * @member {Boolean} MapImageLayer.prototype.imageTransparency
         */
        this.imageTransparency = defaultValue(options.imageTransparency, true)
        /**
         * 所有子图层信息
         * @member {Collection} MapImageLayer.prototype.allSublayers
         */
        this.allSublayers = new Collection()

        /**
         * 是否开启动态投影
         * @member {Boolean} ArcGISMapImageLayer.prototype.dynamicProjectionEnabled
         */
        this.dynamicProjectionEnabled = defaultValue(
          options.dynamicProjectionEnabled,
          false
        )

        // 服务范围原始范围,记录着从服务元数据中获取的范围。
        this._extentSrc = options.extent
          ? Extent.fromJSON(options.extent)
          : undefined

        // 服务原始空间参照系，记录着从服务元数据中获取的空间参照系
        this._spatialReferenceSrc = defaultValue(
          options.spatialReference,
          undefined
        )

        // 子图层私有对象
        this._sublayers = defaultValue(options.sublayers, new Collection())
        // 透明度私有对象
        this._opacity = defaultValue(options.opacity, 1)
      }

      /**
       * @description 转换为json对象
       * @return {Object} json对象
       */
      toJSON() {
        const json = super.toJSON()
        json.url = this.url
        json.mapName = this.mapName
        json.documentInfo = this.documentInfo
        json.capabilities = this.capabilities
        json.imageWidth = this.imageWidth
        json.imageHeight = this.imageHeight
        json.imageFormat = this.imageFormat
        json.imageTransparency = this.imageTransparency
        json.dynamicProjectionEnabled = this.dynamicProjectionEnabled

        const _sublayers = []
        this.sublayers.forEach(function (sublayer) {
          _sublayers.push(sublayer.toJSON())
        })
        json.sublayers = _sublayers

        const _allSublayers = []
        this.allSublayers.forEach(function (sublayer) {
          _allSublayers.push(sublayer.toJSON())
        })
        json.allSublayers = _allSublayers

        return json
      }

      /**
       * @description 克隆方法
       * @return {FeatureLayer} 图层
       */
      clone() {
        return new MapImageLayer(this.toJSON())
      }
    }

    Object.defineProperties(MapImageLayer.prototype, {
      /**
       * 图层范围，从服务中读取，仅会请求在该范围内的瓦片
       * @readonly
       * @member {String} MapImageLayer.prototype.extent
       */
      extent: {
        get() {
          if (!self.spatialReference) {
            return this._extentSrc
          }
          if (self.spatialReference.equals(self._spatialReferenceSrc)) {
            return this._extentSrc
          } else {
            return Projection.project(this._extentSrc, self.spatialReference)
          }
        }
      },

      /**
       * 图层坐标系对象
       * @member Layer.prototype.spatialReference
       * @type {SpatialReference}
       */
      spatialReference: {
        get() {
          return this._spatialReferenceSrc
        },
        set(value) {
          if (this.dynamicProjectionEnabled) {
            this._spatialReferenceSrc = value

            this._fireUpdateEvent('修改图层坐标系', [
              {
                name: 'spatialReference',
                params: [this._spatialReferenceSrc],
                dataChanged: true,
                operationType: 'property'
              }
            ])
          } else {
            throw new Error('该图层不支持动态投影,无法设置空间参照系！')
          }
        }
      }
    })

    export default MapImageLayer