    import { Layer } from '../baseLayer'
    import { WMTSStyle, Zondy } from '../../../base'
    import { defaultValue } from '../../../util'
    import { SpatialReference } from '../../../base/geometry'
    import { getEPSGFromTileMatrixSetId } from '../support/Utils'
    import Extent from '../../../base/geometry/Extent'

    /**
     * IGS地图服务图层
     * @class WMTSSubLayer
     * @moduleEX LayerModule
     * @classdesc IGS地图图层
     * @extends Layer
     * @param {Object} options 构造参数
     * @param {String} [options.description = 'IGS WMTS子图层'] 描述信息
     * @param {String} [options.imageFormat = 'image/png'] 图像格式
     * @param {Array<String>} [options.imageFormats = undefined] 图像格式组
     * @param {String} [options.styleId = undefined] 样式id
     * @param {Array<WMTSStyle>} [options.styles = undefined] 样式组
     * @param {Array<TileMatrixSet>} [options.tileMatrixSets = undefined] 要请求的图块矩阵集
     * @param {String} [options.tileMatrixSetId = undefined] 要请求的图块矩阵集id
     * @param {Extent} [options.extent = undefined] 图层范围
     * @param {Array<String>} [options.resourceURLTemplates = undefined] 资源地址模板
     */

    class WMTSSubLayer extends Layer {
      constructor(options) {
        super(options)
        options = defaultValue(options, {})
        /**
         * 描述信息
         * @member {String} WMTSSubLayer.prototype.description
         */
        this.description = 'IGS WMTS子图层'
        /**
         * 图像格式
         * @member {String} WMTSSubLayer.prototype.imageFormat
         */
        this.imageFormat = defaultValue(options.imageFormat, 'image/png')
        this.identifier = defaultValue(options.identifier, '')
        this.title = defaultValue(options.title, '')
        /**
         * 图像格式组
         * @member {Array<String>} WMTSSubLayer.prototype.imageFormats
         */
        this.imageFormats = defaultValue(options.imageFormats, undefined)
        /**
         * 样式id, 默认为样式中第一项的 ID
         * @member {String} WMTSSubLayer.prototype.styleId
         */
        this.styleId = defaultValue(options.styleId, undefined)
        /**
         * 样式组
         * @member {Array<WMTSStyle>} WMTSSubLayer.prototype.styles
         */
        this.styles = defaultValue(options.styles, undefined)
        /**
         * 要请求的图块矩阵集
         * @member {Array<TileMatrixSet>} WMTSSubLayer.prototype.tileMatrixSets
         */
        this.tileMatrixSets = defaultValue(options.tileMatrixSets, undefined)
        /**
         * 要请求的图块矩阵集id
         * @member {String} WMTSSubLayer.prototype.tileMatrixSetId
         */
        this.tileMatrixSetId = defaultValue(options.tileMatrixSetId, undefined)
        /**
         * 图层范围
         * @member {Extent} WMTSSubLayer.prototype.extent
         */
        this.extent = defaultValue(options.extent, undefined)
        /**
         * 资源地址模板
         * @member {Array<String>} WMTSSubLayer.prototype.resourceURLTemplates
         */
        this.resourceURLTemplates = defaultValue(
          options.resourceURLTemplates,
          undefined
        )
        /**
         * 图层坐标系
         * @member {SpatialReference} WMTSSubLayer.prototype.spatialReference
         */
        let _tileMatrixSetId
        if (this.tileMatrixSetId === 'c') {
          _tileMatrixSetId = 'EPSG:4326'
        } else if (this.tileMatrixSetId === 'w') {
          _tileMatrixSetId = 'EPSG:900913'
        } else {
          _tileMatrixSetId = this.tileMatrixSetId
        }
        this.spatialReference = new SpatialReference(
          getEPSGFromTileMatrixSetId(_tileMatrixSetId)
        )
      }

      /**
       * @function WMTSSubLayer.prototype.toJSON
       * @description 将JS对象转换为JSON格式
       * @returns {Object} 子图层的实例化JSON
       */
      toJSON() {
        const json = super.toJSON()
        json.description = this.description
        json.imageFormat = this.imageFormat
        json.identifier = this.identifier
        json.title = this.title
        json.imageFormats = this.imageFormats
        json.styleId = this.styleId
        json.tileMatrixSets = this.tileMatrixSets
        json.tileMatrixSetId = this.tileMatrixSetId
        json.extent =
          this.extent instanceof Extent ? this.extent.toJSON() : this.extent
        json.resourceURLTemplates = this.resourceURLTemplates
        json.spatialReference =
          this.spatialReference instanceof SpatialReference
            ? this.spatialReference.toJSON()
            : this.spatialReference

        const _styles = []
        if (this.styles) {
          for (let i = 0; i < this.styles.length; i++) {
            _styles.push(
              this.styles[i] instanceof WMTSStyle
                ? this.styles[i].toJSON()
                : this.styles[i]
            )
          }
        }
        json.styles = _styles

        return json
      }

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

    /**
     * @function WMTSSubLayer.prototype.fromJSON
     * @description 将JSON格式的子图层参数转换为JS对象
     * @param {Object} json 子图层的实例化JSON
     */
    WMTSSubLayer.fromJSON = function (json) {
      json = defaultValue(json, {})

      return new WMTSSubLayer(json)
    }

    Zondy.Layer.WMTSSubLayer = WMTSSubLayer
    export default WMTSSubLayer