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

    /**
     * 影像图层基类
     * @class MapImageLayer
     * @moduleEX LayerModule
     * @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 = []
        /**
         * 渲染模式，分为瓦片渲染'tile'和图像渲染'image'
         * @member {String} MapImageLayer.prototype.renderMode
         */
        this.renderMode = defaultValue(options.renderMode, 'tile')
        /**
         * 瓦片化显示时，瓦片宽度
         * @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
        json.spatialReference = toJSON(this.spatialReference, SpatialReference)
        json.extent = toJSON(this.extent, Extent)

        const _sublayers = []
        this.sublayers.forEach(function (sublayer) {
          _sublayers.push(
            sublayer instanceof SubLayer ? sublayer.toJSON() : sublayer
          )
        })
        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) {
          // dynamicProjectionEnabled参数暂不会影响MapImageLayer的空间参考系
          this._spatialReferenceSrc = value
        }
      }
    })

    export default MapImageLayer