import {defaultValue, getGUID} from '../../../util'
import { Extent, SpatialReference } from '../../../base/geometry'
import { SubLayer } from '../baseLayer'
import { formatIGSRenderer } from '../support/Utils'
/**
 * IGS地图图片图层的子图层
 * @class IGSMapImageSubLayer
 * @moduleEX LayerModule
 * @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.id, options.LayerIndex)
    if (this.id === undefined || this.id === null || this.id === '') {
      this.id = getGUID()
    }
    this.index = defaultValue(options.index, undefined)
    this.moveIndex = defaultValue(options.moveIndex, undefined)
    this._renderer = defaultValue(options.renderer, undefined)
    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())
  }
  load() {
    if (this.layer && this.layer._mapServer) {
      const self = this
      return this.layer._mapServer
        .queryLayerInfo({
          layerId: this.id
        })
        .then((result) => {
          const _layerInfo = result.data
          if (_layerInfo) {
            if (_layerInfo.drawingInfo && _layerInfo.drawingInfo.renderer) {
              self._renderer = formatIGSRenderer(
                _layerInfo.drawingInfo.renderer
              )
            }
            self.fields = _layerInfo.fields
            self.geomType = _layerInfo.geomType
            self.index = _layerInfo.index
            self.id = _layerInfo.index
            self.maxScale = _layerInfo.maxScale
            self.minScale = _layerInfo.minScale
            self.name = _layerInfo.name
            self.extent = new Extent({
              xmin: _layerInfo.range.xmin,
              ymin: _layerInfo.range.ymin,
              xmax: _layerInfo.range.xmax,
              ymax: _layerInfo.range.ymax
            })
            self.spatialReference = _layerInfo.spatialReference
              ? new SpatialReference(_layerInfo.spatialReference)
              : new SpatialReference({ wkid: 4326 })
            self.systemLibGuid = _layerInfo.systemLibGuid
            self.systemLibName = _layerInfo.systemLibName
            self.type = _layerInfo.type
            self.url = _layerInfo.url
          }
          return new Promise((resolve) => {
            resolve(self)
          })
        })
    }
  }
}
Object.defineProperties(IGSMapImageSubLayer.prototype, {
  /**
   * 渲染样式对象
   * @readonly
   * @member {BaseRenderer} IGSMapImageSubLayer.prototype.renderer
   */
  renderer: {
    get() {
      return this._renderer
    },
    set(value) {
      this._renderer = value
      this.layer.refresh()
    }
  },
  /**
   * 要素过滤参数,类似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