    import Zondy from '../Zondy'
    import { Log, defaultValue } from '../../util'

    /**
     * 空间参考系
     * <br><br>[ES5引入方式]：<br/>
     * Zondy.SpatialReference() <br/>
     * [ES6引入方式]：<br/>
     * import { SpatialReference } from "@mapgis/webclient-common" <br/>
     * <br/>
     * @class SpatialReference
     * @moduleEX BaseModule
     */
    class SpatialReference {
      constructor(options) {
        /**
         * 投影坐标系的id
         * @member {Number} SpatialReference.prototype.wkid
         * @default 4326
         */
        this.wkid = defaultValue(options.wkid, 4326)
        /**
         * 投影坐标系的描述
         * @member {String} SpatialReference.prototype.wkt
         * @default WGS84
         */
        this.wkt = defaultValue(options.wkt, 'WGS84')
        const instance = {
          4326: 'WGS84',
          3857: 'Web墨卡托'
        }
        /**
         * 是否是地理坐标系
         * @member {Boolean} SpatialReference.prototype.isGeographic
         * @default true
         */
        this.isGeographic = true
        /**
         * 是否是WGS84相关坐标系
         * @member {Boolean} SpatialReference.prototype.isWGS84
         * @default true
         */
        this.isWGS84 = true
        /**
         * 是否是Web墨卡托坐标系
         * @member {Boolean} SpatialReference.prototype.isWebMercator
         * @default false
         */
        this.isWebMercator = false
        /**
         * 坐标系否支持环绕国际日期线，当参考系是Web Mercator或WGS84时，该值为true
         * @member {Boolean} SpatialReference.prototype.isWrappable
         * @default true
         */
        this.isWrappable = true
        /**
         * 一个便捷的Web墨卡托对象实力，只有当类型为Web墨卡托时才有
         * @member {Boolean} SpatialReference.prototype.WebMercator
         */
        this.WebMercator = undefined

        /**
         * 一个便捷的WGS84对象实力，只有当类型为WGS84时才有
         * @member {Boolean} SpatialReference.prototype.WGS84
         */
        this.WGS84 = undefined
        if (typeof options === 'string') {
          const req = /^(EPSG|epsg)(:)?\d+$/
          const reqWkid = /\d+$/
          const wkidStr = options.match(reqWkid)
          if (req.test(options) && wkidStr) {
            this.wkid = parseInt(wkidStr[0])
            if (instance[this.wkid]) {
              this.wkt = instance[this.wkid]
            } else {
              this.wkt = undefined
            }
          } else {
            // 不是标准格式，则直接使用名字
            this.wkid = undefined
            this.wkt = options
          }
        } else if (typeof options === 'object') {
          if (options['wkid']) {
            let wkid = options['wkid']
            if (wkid) {
              if (typeof wkid === 'string') {
                wkid = parseInt(wkid)
              }
              this.wkid = wkid
            }
          }
          if (options['wkt']) {
            this.wkt = options['wkt']
          } else {
            this.wkt = undefined
          }
        } else {
          Log.error('传入参数错误!')
        }

        if ([4326, 4490, 4610, 4214].indexOf(this.wkid) < 0) {
          this.isGeographic = false
          this.isWGS84 = false
        }

        if ([102113, 102100, 900913, 3857].indexOf(this.wkid) > -1) {
          this.isWebMercator = true
        }

        if (
          [4326, 4490, 4610, 4214, 102113, 102100, 900913, 3857].indexOf(
            this.wkid
          ) < 0
        ) {
          this.isWrappable = false
        }

        if (this.isWebMercator) {
          this.WebMercator = {
            wkid: this.wkid,
            wkt: this.wkt
          }
        }

        if (this.isWGS84) {
          this.WGS84 = {
            wkid: this.wkid,
            wkt: this.wkt
          }
        }
      }

      /**
       * 克隆几何对象
       * @return {Geometry} 克隆后的几何对象
       */
      clone() {
        return new SpatialReference(this)
      }

      /**
       * 将JSON里的数据导入到几何中
       * @param {Object} json JSON对象
       */
      static fromJSON(json) {
        json = defaultValue(json, {})
        return new SpatialReference(json)
      }

      /**
       * 导出为json对象
       * @return {Object} json对象
       */
      toJSON() {
        return {
          wkid: this.wkid,
          wkt: this.wkt,
          isGeographic: this.isGeographic,
          isWGS84: this.isWGS84,
          isWebMercator: this.isWebMercator,
          isWrappable: this.isWrappable,
          WebMercator: this.WebMercator,
          WGS84: this.WGS84
        }
      }

      /**
       * 导出为字符串
       * @return {String} 坐标系字符串
       */
      toString() {
        let wkt = ''

        switch (this.wkid) {
          case 4326:
            wkt = 'EPSG:4326'
            break
          case 3857:
            wkt = 'EPSG:3857'
            break
          default:
            wkt = this.wkt
        }

        return wkt
      }

      /**
       * 判断坐标系对象的wkid和wkt的值是否相等
       * @param {SpatialReference | Object} spatialReference 坐标系对象
       * @return {Boolean} wkid和wkt的值是否相等
       * */
      equals(spatialReference) {
        if (!(spatialReference instanceof SpatialReference)) {
          if (
            !(
              spatialReference instanceof Object &&
              spatialReference.hasOwnProperty('wkid') &&
              spatialReference.hasOwnProperty('wkt')
            )
          ) {
            throw new Error('spatialReference的格式不正确！')
          }
        }

        if (
          this.wkid === spatialReference.wkid &&
          this.wkt === spatialReference.wkt
        ) {
          return true
        }

        return false
      }
    }

    Zondy.SpatialReference = SpatialReference
    export default SpatialReference