import { MapLayerType } from '../export/BasicClass'
import { MapCoordTrans } from '../export/MapCoordTrans'
/**
 * @class GDMapEditor
 * @classdesc 高德地图编辑接口类
 */
export default class GDMapEditor {
  constructor(map) {
    this.map = map
    this.layerType = null
    this.overlays = []
    this.mousetool = new window.AMap.MouseTool(map)
    this.coordTrans = new MapCoordTrans()
    this.drawComplete = null
  }
  /**
   * 开始新建指定类型要素
   * @param {MapLayerType} layerType
   */
  startEditing(layerType) {
    this.layerType = layerType
    switch (layerType) {
      case MapLayerType.Point:
        this.mousetool.marker() //使用鼠标工具，在地图上画标记点
        break
      case MapLayerType.Line:
        this.mousetool.polyline()
        break
      case MapLayerType.Polygon:
        this.mousetool.polygon()
        break
    }
    var that = this
    this.map.setDefaultCursor('crosshair')
    this.drawComplete = function (e) {
      that.overlays.push({ overlay: e.obj, type: layerType })
    }
    this.mousetool.on('draw', this.drawComplete)
  }
  /**
   * 清除绘制要素
   */
  clearOverlays() {
    var overlays = []
    this.overlays.forEach((item) => {
      overlays.push(item.overlay)
    })
    this.map.remove(overlays)
  }
  /**
   * 停止编辑
   */
  stopEditing() {
    this.mousetool.off('draw', this.drawComplete)
    this.mousetool.close()
    this.map.setDefaultCursor('default')
    this.overlays = []
  }
  /**
   * 获取编辑要素
   * @returns 编辑要素数组
   */
  getOverlays() {
    return this.overlays
  }
  /**
   * 获取编辑要素类型
   * @returns MapLayerType
   */
  getLayerTypes() {
    return this.layerType
  }
  /**
   * 保存新建要素到指定图层名图层中
   * @param {string} layername 图层名
   */
  saveEditing(layername) {
    var overlays = this.getOverlays()
    var type = this.getLayerTypes()
    var that = this
    var postions = []
    overlays.filter(function (item) {
      if (type == item.type) {
        if (type == MapLayerType.Point) {
          var pos = item.overlay.getPosition()
          var pt84 = that.coordTrans.gaodeToWGS84([[pos.lng, pos.lat]])
          postions.push({ lng: pt84[0][0], lat: pt84[0][1] })
        } else if (type === MapLayerType.Polygon || type === MapLayerType.Line) {
          var path = item.overlay.getPath()
          var llpos = path.map(function (p) {
            var p84 = that.coordTrans.gaodeToWGS84([[p.lng, p.lat]])
            return { lng: p84[0][0], lat: p84[0][1] }
          })
          postions.push(llpos)
        }
      }
    })
    return { layer: layername, postion: postions }
  }
  /**
   * 开启编辑图层
   * @param {[marker｜polyline｜polygon]} elements 图层内要素集合
   * @param {MapLayerType} type 图层类型
   */
  enableLayerEditing(elements, type) {
    for (var i = 0; i < elements.length; i++) {
      if (type == MapLayerType.Point) {
        elements[i].setDraggable(true)
      } else if (type == MapLayerType.Polygon || type == MapLayerType.Line) {
        var peditor = new window.AMap.PolyEditor(this.map, elements[i])
        peditor.open()
        this.polyEditors.push(peditor)
      }
    }
  }
  /**
   * 停止编辑图层
   * @param {[marker｜polyline｜polygon]} elements 图层内要素集合
   * @param {MapLayerType} type 图层类型
   */
  disableEditing(elements, type) {
    for (var i = 0; i < elements.length; i++) {
      if (type == MapLayerType.Point) {
        elements[i].setDraggable(false)
      } else if (type == MapLayerType.Polygon || type == MapLayerType.Line) {
        this.polyEditors[i].close()
      }
    }
  }
}
