import { MapOverLayer } from './MapOverLayer'
import { MapCoordTrans } from '../export/MapCoordTrans'
/**
 * 高德图层类，存放于maplayers数据中
 * @private
 */
export class GDMapOverLayer extends MapOverLayer {
  //this.element =>AMap.OverlayGroup
  constructor(name, element, visible, type) {
    super(name, element, visible, type)
    this.coordTrans = new MapCoordTrans()
  }
  /**
   * 注册事件
   * @param {string} eventType 事件类型
   * @param {function} callback 回调函数
   */
  on(eventType, callback) {
    var that = this
    var callFunc = function (e) {
      // callback(e)
      var p84 = that.coordTrans.gaodeToWGS84([[e.lnglat.getLng(), e.lnglat.getLat()]])

      callback({
        attr: e.target.getExtData(),
        lonlat: p84[0],
        object: e.target,
        mapsEvent: e
      })
    }
    this.element.on(eventType, callFunc)
    super.addEvent(callFunc, eventType)
  }
  /**
   * 根据事件类型移除事件
   * @param {string} eventType 事件类性
   */
  off(eventType) {
    var inx = this.events.findIndex((item) => item.type === eventType)
    if (inx > -1) {
      this.element.off(eventType, this.events[inx].event)
      this.events.splice(inx, 1)
    }
  }
}
