import { MapLayerType } from '../export/BasicClass'
import '../css/drawingmanager.css'
import { MapCoordTrans } from '../export/MapCoordTrans'

/**
 * @class BDMapEditor
 * @classdesc 百度地图编辑接口类
 */
export default class BDMapEditor {
  constructor(map) {
    var styleOptions = {
      strokeColor: '#5E87DB', // 边线颜色。
      fillColor: '#5E87DB', // 填充颜色。当参数为空时，圆形将没有填充效果。
      strokeWeight: 2, // 边线的宽度，以像素为单位。
      strokeOpacity: 1, // 边线透明度，取值范围0 - 1。
      fillOpacity: 0.2 // 填充的透明度，取值范围0 - 1。
    }
    var labelOptions = {
      borderRadius: '2px',
      background: '#FFFBCC',
      border: '1px solid #E1E1E1',
      color: '#703A04',
      fontSize: '12px',
      letterSpacing: '0',
      padding: '5px'
    }
    // 实例化鼠标绘制工具
    this.drawingManager = new window.BMapGLLib.DrawingManager(map, {
      enableDrawingTool: false, // 是否显示工具栏
      enableSorption: true, // 是否开启边界吸附功能
      sorptionDistance: 20, // 边界吸附距离
      enableGpc: true, // 是否开启延边裁剪功能
      enableLimit: false, // 是否开启超限提示
      polylineOptions: styleOptions, // 线的样式
      polygonOptions: styleOptions, // 多边形的样式
      rectangleOptions: styleOptions, // 矩形的样式
      labelOptions: labelOptions // label的样式
    })
    this.drawingManager.disableCalculate() // 绘制是否进行测距(画线时候)、测面(画圆、多边形、矩形)
    this.bdoverlays = []
    this.coordTrans = new MapCoordTrans()
  }
  /**
   * 开始新建指定类型要素
   * @param {MapLayerType} layerType
   */
  startEditing(layerType) {
    switch (layerType) {
      case MapLayerType.Point:
        this.drawingManager.setDrawingMode(BMAP_DRAWING_MARKER)
        break
      case MapLayerType.Line:
        this.drawingManager.setDrawingMode(BMAP_DRAWING_POLYLINE)
        break
      case MapLayerType.Polygon:
        this.drawingManager.setDrawingMode(BMAP_DRAWING_POLYGON)
        break
    }
    this.drawingManager.open()

    var that = this
    this.drawingManager.addEventListener('overlaycomplete', function (e, overlay) {
      that.drawingManager.addOverlayData(e.overlay)
      that.bdoverlays.push(e.overlay)
    })
  }
  /**
   * 保存新建要素到指定图层名图层中
   * @param {string} layername 图层名
   */
  saveEditing(layername) {
    var type = this.getLayerTypes()
    var that = this
    var postions = this.bdoverlays.map(function (item) {
      if (type == MapLayerType.Point) {
        var pos = item.getPosition()
        var pt84 = that.coordTrans.baiduToWGS84([[pos.lng, pos.lat]])
        return { lng: pt84[0][0], lat: pt84[0][1] }
      } else if (type === MapLayerType.Polygon || type === MapLayerType.Line) {
        var path = item.getPath()
        var llpos = path.map(function (p) {
          var p84 = that.coordTrans.baiduToWGS84([[p.lng, p.lat]])
          return { lng: p84[0][0], lat: p84[0][1] }
        })
        return llpos
      }
    })
    return { layer: layername, postion: postions }
  }
  /**
   * 清除绘制要素
   */
  clearOverlays() {
    this.drawingManager.clearOverlays()
  }
  /**
   * 停止编辑
   */
  stopEditing() {
    this.drawingManager.close()
    this.drawingManager.setOverlaysData([])
    this.bdoverlays = []
  }
  /**
   * 获取编辑要素
   * @returns 编辑要素数组
   */
  getOverlays() {
    // return this.drawingManager.getOverlays()
    return this.bdoverlays
  }
  /**
   * 获取编辑要素类型
   * @returns MapLayerType
   */
  getLayerTypes() {
    var type
    var drawingType = this.drawingManager.getDrawingMode()
    switch (drawingType) {
      case BMAP_DRAWING_MARKER:
        type = MapLayerType.Point
        break
      case BMAP_DRAWING_POLYLINE:
        type = MapLayerType.Line
        break
      case BMAP_DRAWING_POLYGON:
        type = MapLayerType.Polygon
        break
    }
    return type
  }
}
