import * as esriLoader from 'esri-loader'
import { MapLayerType } from '../export/BasicClass'
/**
 * @class EsriMapEditor
 * @classdesc ESRI地图编辑接口类
 */
export default class EsriMapEditor {
  constructor(map) {
    this.map = map
    this.overlays = []
    this.polyEditors = []
  }
  /**
   * 开始新建指定类型要素
   * @param {MapLayerType} layerType
   */
  async startEditing(layerType) {
    this.layerType = layerType
    await esriLoader
      .loadModules([
        'esri/symbols/SimpleMarkerSymbol',
        'esri/symbols/SimpleLineSymbol',
        'esri/symbols/SimpleFillSymbol',
        'esri/graphic',
        'esri/geometry/Point',
        'esri/geometry/Polyline',
        'esri/geometry/Polygon',
        'esri/toolbars/draw',
        'dojo/domReady!'
      ])
      .then(([SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, Graphic, Point, Polyline, Polygon, Draw]) => {
        this.draw = new Draw(this.map)
        switch (layerType) {
          case MapLayerType.Point:
            this.draw.activate(Draw['MULTI_POINT'])
            break
          case MapLayerType.Line:
            this.draw.activate(Draw['POLYLINE'])
            break
          case MapLayerType.Polygon:
            this.draw.activate(Draw['POLYGON'])
            break
        }

        var that = this
        //事件触发方式，双击
        this.draw.on('draw-complete', function (evt) {
          var symbol
          switch (evt.geometry.type) {
            case 'multipoint':
              symbol = new SimpleMarkerSymbol()
              for (var i = 0; i < evt.geometry.points.length; i++) {
                var geo = evt.geometry.points[i]
                var g = new Graphic(new Point(geo[0], geo[1]), symbol)
                that.overlays.push(g)
              }
              break
            case 'polyline':
              symbol = new SimpleLineSymbol()
              var polyline = new Polyline(evt.geometry.paths)
              var g = new Graphic(polyline)
              that.overlays.push(g)
              break
            case 'polygon':
              symbol = new SimpleFillSymbol()
              var polygon = new Polygon(evt.geometry.rings)
              var g = new Graphic(polygon)
              that.overlays.push(g)
              break
          }
          var graphic = new Graphic(evt.geometry, symbol)
          this.map.graphics.add(graphic)
        })
      })
  }
  /**
   * 保存新建要素到指定图层名图层中
   * @param {string} layername 图层名
   */
  saveEditing(layername) {
    var type = this.getLayerTypes()
    var elems = this.getOverlays()

    var postions = elems.map(function (item) {
      if (type == MapLayerType.Point) {
        var pos = item.geometry
        return { lng: pos.x, lat: pos.y }
      } else if (type === MapLayerType.Polygon) {
        var path = item.geometry.rings
        var llpos = []
        for (var i = 0; i < path.length; i++) {
          for (var j = 0; j < path[i].length; j++) {
            llpos.push({ lng: path[i][j][0], lat: path[i][j][1] })
          }
        }
        return llpos
      } else if (type === MapLayerType.Line) {
        var path = item.geometry.paths
        var llpos = []
        for (var i = 0; i < path.length; i++) {
          for (var j = 0; j < path[i].length; j++) {
            llpos.push({ lng: path[i][j][0], lat: path[i][j][1] })
          }
        }
        return llpos
      }
    })
    return { layer: layername, postion: postions }
  }
  /**
   * 开启编辑图层
   * @param {[marker｜polyline｜polygon]} elements 图层内要素集合
   * @param {MapLayerType} type 图层类型
   */
  enableLayerEditing(elements, type) {
    esriLoader.loadModules(['esri/toolbars/edit']).then(([Edit]) => {
      var tool
      if (type == MapLayerType.Point) {
        tool = Edit.MOVE
      } else if (type == MapLayerType.Polygon || type == MapLayerType.Line) {
        tool = Edit.EDIT_VERTICES
      }

      for (var i = 0; i < elements.length; i++) {
        var edit = new Edit(this.map)
        edit.activate(tool, elements[i], { allowAddVertices: true, allowDeleteVertices: true })
        this.polyEditors.push(edit)
      }
    })
  }
  /**
   * 停止编辑图层
   */
  disableEditing() {
    for (var i = 0; i < this.polyEditors.length; i++) {
      this.polyEditors[i].deactivate()
    }
  }
  /**
   * 清除绘制要素
   */
  clearOverlays() {
    for (var i = 0; i < this.overlays.length; i++) {
      this.map.graphics.remove(this.overlays[i])
    }
  }
  /**
   * 停止编辑
   */
  stopEditing() {
    if (this.draw) {
      this.draw.finishDrawing()
      this.draw.deactivate()
      this.overlays = []
      this.polyEditors = []
    }
  }
  /**
   * 获取编辑要素
   * @returns 编辑要素数组
   */
  getOverlays() {
    this.draw.finishDrawing()
    return this.overlays
  }
  /**
   * 获取编辑要素类型
   * @returns MapLayerType
   */
  getLayerTypes() {
    return this.layerType
  }
}
