import { MapOverLayer } from './MapOverLayer'
/**
 * ESRI图层类，存放于maplayers数据中
 * @private
 */
export class EsriMapOverLayer extends MapOverLayer {
  //this.element =>ESRI.Graphicslayer
  /**
   * 注册事件
   * @param {string} eventType 事件类型
   * @param {function} callback 回调函数
   */
  on(eventType, callback) {
    var eEvt = eventType
    if (eventType == 'mouseover') {
      eEvt = 'mouse-over'
    } else if (eventType == 'mouseout') {
      eEvt = 'mouse-out'
    }

    this.callFunc = function (e) {
      // callback(e)
      var geo = e.graphic.geometry
      var lonlat = []
      if (geo.type == 'point') {
        lonlat = [geo.x, geo.y]
      } else if (geo.type === 'polygon') {
        var path = geo.rings
        for (var i = 0; i < path.length; i++) {
          for (var j = 0; j < path[i].length; j++) {
            lonlat.push(path[i][j])
          }
        }
      } else if (geo.type === 'polyline') {
        var path = geo.paths
        for (var i = 0; i < path.length; i++) {
          for (var j = 0; j < path[i].length; j++) {
            lonlat.push(path[i][j])
          }
        }
      }

      callback({
        attr: e.graphic.attributes,
        lonlat: lonlat,
        object: e.graphic,
        mapsEvent: e
      })
    }

    super.addEvent(this.element.on(eEvt, this.callFunc), eventType)
  }
  /**
   * 根据事件类型移除事件
   * @param {string} eventType 事件类性
   */
  off(eventType) {
    var inx = this.events.findIndex((item) => item.type === eventType)
    if (inx > -1) {
      this.events[inx].event.remove()
      this.events.splice(inx, 1)
    }
  }
}
