import BasicAdapter from './BasicAdapter'
import { BaseMapType, MapLayerType } from '../export/BasicClass'
import { BDMapOverLayer } from '../layers/BDMapOverLayer'
import BDMapEditor from './BDMapEditor'
import {
  animationLayerName,
  infoLayerName,
  maskLayerName,
  maskLayerBoundaryName,
  movingObjectLayerName
} from './MapLocalConfig'
import { color2RGB } from './ColorTrans'
import '../css/style.css'

/**
 * @class BDMapAdapter
 * @classdesc 百度地图接口适配类
 */
export default class BDMapAdapter extends BasicAdapter {
  /**
   * 初始化地图实例
   * @param {function} callback 回调函数
   */
  initialMap(callback) {
    this.map = new window.BMapGL.Map(this.div)
    if (window.mapconfig.minZoom) {
      this.map.setMinZoom(window.mapconfig.minZoom)
    }
    if (window.mapconfig.maxZoom) {
      this.map.setMaxZoom(window.mapconfig.maxZoom)
    }
    if (window.mapconfig.viewMode == '3D' && window.mapconfig.pitch != 0) {
      this.map.setTilt(window.mapconfig.pitch)
    }
    this.setCenterAndZoom(window.mapconfig.center, window.mapconfig.zoom || 2)
    this.map.enableScrollWheelZoom()

    if (this.map.isLoaded()) {
      if (window.mapconfig.mapstyle && window.mapconfig.mapstyle != '') {
        this.map.setMapStyleV2({ styleId: window.mapconfig.mapstyle })
        this.baseMapLayers.push(BaseMapType.Default)
      } else {
        this.baseMapLayers.push(BaseMapType.VectorTiles)
      }
    }
    //初始化编辑器
    this.mapEditor = new BDMapEditor(this.map)
    callback(this.map)
  }
  /**
   * 地图居中于坐标点
   * @param {number[]} center 中心点坐标 [117.204348, 39.345704]
   */
  centerAt(center) {
    let pt = this.coordTrans.wgs84ToBaidu([center])
    this.map.setCenter(new window.BMapGL.Point(pt[0][0], pt[0][1]))
  }
  /**
   * 地图缩放至指定级别并以指定点为地图显示中心点
   * @param {number[]} center 中心点坐标 [117.204348, 39.345704]
   * @param {number} zoom 缩放等级 8
   */
  setCenterAndZoom(center, zoom) {
    let pt = this.coordTrans.wgs84ToBaidu([center])
    this.map.centerAndZoom(new window.BMapGL.Point(pt[0][0], pt[0][1]), zoom)
  }
  setMapLayers(options) {
    this._resetMapLayers()
    var mapTypes = options.mapTypes
    for (var i = 0; i < mapTypes.length; i++) {
      if (mapTypes[i] == BaseMapType.Default) {
        if (this.map.getMapType() != BMAP_NORMAL_MAP) {
          this.map.setMapType(BMAP_NORMAL_MAP)
        }
        if (window.mapconfig.mapstyle && window.mapconfig.mapstyle != '') {
          this.map.setMapStyleV2({ styleId: window.mapconfig.mapstyle })
          this.baseMapLayers.push(BaseMapType.Default)
        }
      } else if (mapTypes[i] == BaseMapType.Satellite) {
        if (this.map.getMapType() != BMAP_SATELLITE_MAP) {
          this.map.setMapType(BMAP_SATELLITE_MAP)
        }
        this.baseMapLayers.push(BaseMapType.Satellite)
      } else if (mapTypes[i] == BaseMapType.Traffic) {
        this.map.setTrafficOn()
        this.baseMapLayers.push(BaseMapType.Traffic)
      } else if (mapTypes[i] == BaseMapType.VectorTiles) {
        if (this.map.getMapType() != BMAP_NORMAL_MAP) {
          this.map.setMapType(BMAP_NORMAL_MAP)
        }
        this.baseMapLayers.push(BaseMapType.VectorTiles)
      }
    }
  }
  _resetMapLayers() {
    var ind = this.baseMapLayers.findIndex((item) => {
      return item == BaseMapType.Traffic
    })
    if (ind > -1) {
      this.map.setTrafficOff()
    }
    if (window.mapconfig.mapstyle && window.mapconfig.mapstyle != '') {
      this.map.setMapStyleV2({ styleId: '' })
    }
    this.baseMapLayers = []
  }
  /**
   * 添加点图层
   * @param {string} layername 图层名
   * @param {[{}]} geoObj 构成图层的几何集合 {name: '', attribute: [{name:,value:}], longitude:'',latitude:'',style:MapPointStyle}
   */
  addPointsLayer(layername, geoObj) {
    var markerList = this.setPoints(geoObj)
    this.mapLayers.push(new BDMapOverLayer(layername, markerList, true, MapLayerType.Point))
  }
  addPointsLayerWithTxt(layername, geoObj) {
    var markerList = this.setPointsWithText(geoObj)
    this.mapLayers.push(new BDMapOverLayer(layername, markerList, true, MapLayerType.Point))
  }
  /**
   * 添加线图层
   * @param {string} layername 图层名
   * @param {[{}]} geoObj 构成图层的几何集合 {name: '', attribute: [{name:,value:}], path:[[],[]],style:MapLineStyle}
   */
  addLinesLayer(layername, geoObj) {
    var markerList = this.setLines(geoObj)
    this.mapLayers.push(new BDMapOverLayer(layername, markerList, true, MapLayerType.Line))
  }
  /**
   * 添加面图层
   * @param {string} layername 图层名
   * @param {[{}]} geoObj 构成图层的几何集合 {name: '', attribute: [{name:,value:}], path:[[],[]],style:MapPolygonStyle}
   */
  addPolygonsLayer(layername, geoObj) {
    var markerList = this.setPolygons(geoObj)
    this.mapLayers.push(new BDMapOverLayer(layername, markerList, true, MapLayerType.Polygon))
  }
  /**
   * 添加文字图层
   * @param {string} layername 图层名
   * @param {[{}]} geoObj 构成图层的几何集合 {name: '',content: '', longitude:'',latitude:'',style:MapTextStyle}
   */
  addLabels(layername, geoObj) {
    var markerList = this.setLabels(geoObj)
    this.mapLayers.push(new BDMapOverLayer(layername, markerList, true, MapLayerType.Text))
  }
  /**
   * 添加信息窗图层
   * @param {string} layername 图层名
   * @param {[{}]} geoObj 构成图层的几何集合 {name: '',content: '', longitude:'',latitude:'',xOffset:0,yOffset:0}
   */
  addInfowindow(geoObj) {
    var markerList = this.setInfowindow(geoObj)
    var lyr = this.getLyrIndexByName(infoLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      list = list.concat(markerList)
      this.mapLayers[lyr].element = list
    } else {
      this.mapLayers.push(new BDMapOverLayer(infoLayerName, markerList, true, MapLayerType.Infowindow))
    }
  }
  /**
   * 根据数据生成点要素数组
   * @param {object[]} geoObj
   * @returns 点要素数组
   */
  setPoints(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (geoObj[i].longitude == null || geoObj[i].latitude == null) continue
      if (geoObj[i].longitude === 0 || geoObj[i].latitude === 0) continue

      let pt = this.coordTrans.wgs84ToBaidu([[geoObj[i].longitude, geoObj[i].latitude]])

      var point = new window.BMapGL.Point(pt[0][0], pt[0][1])

      var imgIcon = new window.BMapGL.Icon(
        geoObj[i].style.icon,
        new window.BMapGL.Size(geoObj[i].style.width, geoObj[i].style.height)
      )
      imgIcon.imageSize = new window.BMapGL.Size(geoObj[i].style.width, geoObj[i].style.height)
      var marker = new window.BMapGL.Marker(point, {
        icon: imgIcon,
        offset: new window.BMapGL.Size(geoObj[i].style.xOffset, geoObj[i].style.yOffset)
      })
      var att = []
      att.push({ name: geoObj[i].name })
      if (geoObj[i].attribute != undefined || geoObj[i].attribute.length > 0) {
        att = att.concat(geoObj[i].attribute)
      }
      marker.attributes = att
      this.map.addOverlay(marker)
      markerList.push(marker)
    }
    return markerList
  }
  /**
   *
   * @param {*} geoObj
   * @returns
   */
  setPointsWithText(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (geoObj[i].longitude == null || geoObj[i].latitude == null) continue
      if (geoObj[i].longitude === 0 || geoObj[i].latitude === 0) continue

      let pt = this.coordTrans.wgs84ToBaidu([[geoObj[i].longitude, geoObj[i].latitude]])

      var point = new window.BMapGL.Point(pt[0][0], pt[0][1])
      var fontColor = color2RGB(geoObj[i].txtStyle.fontColor) || '#000'
      var fontSize = geoObj[i].txtStyle.fontSize || 12
      var labelTxt = geoObj[i].text || (i + 1).toString()
      var html =
        `<div style=" position:relative;"> <img src='` +
        geoObj[i].ptStyle.icon +
        `'
          style='position:absolute; left:0px; top:0px; width:` +
        geoObj[i].ptStyle.width +
        `px; height:` +
        geoObj[i].ptStyle.height +
        `px;'>
       <p style="position:absolute;  font-size:` +
        fontSize +
        `px;  color:` +
        fontColor +
        `;  left:` +
        geoObj[i].txtStyle.xOffset +
        `px; 
        top:` +
        geoObj[i].txtStyle.yOffset +
        `px;" >` +
        labelTxt +
        `</p></div>`

      var myRichMarker = new window.BMapGLLib.RichMarker(html, point, {
        anchor: new window.BMapGL.Size(geoObj[i].ptStyle.xOffset, geoObj[i].ptStyle.yOffset)
      })
      var att = []
      att.push({ name: geoObj[i].name })
      if (geoObj[i].attribute != undefined || geoObj[i].attribute.length > 0) {
        att = att.concat(geoObj[i].attribute)
      }
      myRichMarker.attributes = att
      this.map.addOverlay(myRichMarker)
      markerList.push(myRichMarker)
    }
    return markerList
  }
  /**
   * 根据数据生成线要素数组
   * @param {*} geoObj
   * @returns 线要素数组
   */
  setLines(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (!geoObj[i].path) continue
      let style = geoObj[i].style
      let pt = this.coordTrans.wgs84ToBaidu(geoObj[i].path)
      var points = []
      for (let p = 0; p < geoObj[i].path.length; p++) {
        points.push(new window.BMapGL.Point(pt[p][0], pt[p][1]))
      }
      var line = new window.BMapGL.Polyline(points, {
        strokeColor: style.color || '#00D3FC',
        strokeWeight: style.width || 2,
        strokeOpacity: style.opacity || 0.9,
        strokeStyle: style.lineType || 'solid'
      })
      var att = []
      att.push({ name: geoObj[i].name })
      if (geoObj[i].attribute != undefined || geoObj[i].attribute.length > 0) {
        att = att.concat(geoObj[i].attribute)
      }
      line.attributes = att

      this.map.addOverlay(line)
      markerList.push(line)
    }
    return markerList
  }
  /**
   * 根据数据生成面要素数组
   * @param {*} geoObj
   * @returns 面要素数组
   */
  setPolygons(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (!geoObj[i].path) continue

      let style = geoObj[i].style
      let pt = this.coordTrans.wgs84ToBaidu(geoObj[i].path)
      var points = []
      for (let p = 0; p < geoObj[i].path.length; p++) {
        points.push(new window.BMapGL.Point(pt[p][0], pt[p][1]))
      }
      var polygon = new window.BMapGL.Polygon(points, {
        strokeColor: style.strokeColor || '#00D3FC',
        fillColor: style.fillColor || '#00B2D5',
        strokeWeight: style.strokeWeight || 2,
        strokeOpacity: style.strokeOpacity || 0.9,
        fillOpacity: style.fillOpacity || 0.5,
        strokeStyle: style.strokeType || 'solid'
      })

      var att = []
      att.push({ name: geoObj[i].name })
      if (geoObj[i].attribute != undefined || geoObj[i].attribute.length > 0) {
        att = att.concat(geoObj[i].attribute)
      }
      polygon.attributes = att

      this.map.addOverlay(polygon)
      markerList.push(polygon)
    }
    return markerList
  }
  /**
   * 根据数据生成文字要素数组
   * @param {*} geoObj
   * @returns 文字要素数组
   */
  setLabels(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (geoObj[i].longitude == null || geoObj[i].latitude == null) continue
      if (geoObj[i].longitude === 0 || geoObj[i].latitude === 0) continue

      let pt = this.coordTrans.wgs84ToBaidu([[geoObj[i].longitude, geoObj[i].latitude]])

      var marker = new window.BMapGL.Label(geoObj[i].content, {
        offset: new window.BMapGL.Size(geoObj[i].style.xOffset, geoObj[i].style.yOffset),
        position: new window.BMapGL.Point(pt[0][0], pt[0][1])
      })
      marker.setStyle({
        color: color2RGB(geoObj[i].style.fontColor) || '#000',
        fontSize: geoObj[i].style.fontSize + 'px' || '12px',
        fontWeight: geoObj[i].style.fontWeight || 'normal',
        fontFamily: geoObj[i].style.fontFamily || 'Arial',
        offset: new window.BMapGL.Size(geoObj[i].style.xOffset || 0, geoObj[i].style.yOffset || 0),
        backgroundColor: 'transparent',
        border: 'none'
      })
      marker.attributes = [{ name: geoObj[i].name }]
      this.map.addOverlay(marker)
      markerList.push(marker)
    }
    return markerList
  }
  /**
   * 根据数据生成信息窗要素数组
   * @param {*} geoObj
   * @returns 信息窗要素数组
   */
  setInfowindow(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (geoObj[i].longitude == null || geoObj[i].latitude == null) continue
      if (geoObj[i].longitude === 0 || geoObj[i].latitude === 0) continue

      let pt = this.coordTrans.wgs84ToBaidu([[geoObj[i].longitude, geoObj[i].latitude]])

      var point = new window.BMapGL.Point(pt[0][0], pt[0][1])
      var infoBox = new window.BMapGLLib.InfoBox(this.map, geoObj[i].content, {
        enableAutoPan: false,
        align: 1,
        offset: new window.BMapGL.Size(geoObj[i].xOffset || 0, geoObj[i].yOffset || 0),
        showCloseIcon: false
      })
      infoBox.attributes = [{ name: geoObj[i].name }]
      infoBox.open(point)
      markerList.push(infoBox)
    }
    return markerList
  }
  /**
   * 关闭指定信息窗
   * @param {string} name 信息窗名
   */
  closeInfowindow(name) {
    var lyr = this.getLyrByName(infoLayerName)
    if (lyr) {
      var ele = this.getElementByFieldName(lyr, name)
      ele.hide()
    }
  }
  /**
   * 打开指定信息窗
   * @param {string} name 信息窗名
   */
  openInfowindow(name) {
    var lyr = this.getLyrByName(infoLayerName)
    if (lyr) {
      var ele = this.getElementByFieldName(lyr, name)
      ele.show()
    }
  }
  /**
   * 移除指定信息窗
   * @param {string} name 信息窗名
   */
  removeInfowindow(name) {
    var lyr = this.getLyrByName(infoLayerName)
    if (lyr) {
      var ele = this.getElementByFieldName(lyr, name)
      ele.remove()
    }
  }
  /**
   * 移除指定图层
   * @param {string} layername 图层名
   */
  removeLayer(layername) {
    if (layername == undefined) {
      this.map.clearOverlays()
      super.removeMapLayers()
    } else {
      var ind = this.getLyrIndexByName(layername)
      for (let i = 0; i < this.mapLayers[ind].element.length; i++) {
        this.map.removeOverlay(this.mapLayers[ind].element[i])
      }
      super.removeMapLayer(ind)
    }
  }
  /**
   * 移除指定图层内指定属性要素
   * @param {string} layername 图层名
   * @param {string} name 要素名
   */
  removeElementByFieldName(layername, name) {
    var lyr = this.getLyrByName(layername)
    if (lyr) {
      var ele = this.getElementByFieldName(lyr, name)
      this.map.removeOverlay(ele)
    }
  }
  /**
   * 移除指定要素
   * @param {*} layername 图层名，不需要
   * @param {Marker｜Polyline｜Polygon｜Label} element 需要删除的元素
   */
  removeElement(layername = null, element) {
    this.map.removeOverlay(element)
  }
  /**
   * 获得指定图层内指定属性要素
   * @param {array} layer 要素集合
   * @param {string} name 要素名
   * @returns Marker｜Polyline｜Polygon｜Label
   */
  getElementByFieldName(layer, name) {
    for (let i = 0; i < layer.length; i++) {
      var cus = layer[i].attributes
      var ele = cus.find((item) => item.name === name)
      if (ele != undefined) return layer[i]
    }
  }
  /**
   * 根据图层名获得图层坐标数据
   * @param {string} layername 图层名
   * @returns object { layer: layername, postion: [{lng,lat},...] }
   */
  getLayerGeometryByName(layername) {
    var index = this.getLyrIndexByName(layername)
    if (index === -1) {
      return
    }
    var type = this.mapLayers[index].type
    var ele = this.mapLayers[index].element
    var that = this

    var postions = ele.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 }
  }
  /**
   * 插入要素到某一图层
   * @param {string} layername 要插入要素的图层名
   * @param {array} geoObj 构成图层的几何集合 [MapPolygon、MapPolyline、MapPoint、MapText]
   *  @param {MapLayerType} layertype 要插入要素的图层类型
   */
  insertintoLayer(layername, geoObj, lyrtype) {
    var index = this.getLyrIndexByNameandGeometry(layername, lyrtype)
    if (index == -1) return
    var mlist
    switch (lyrtype) {
      case MapLayerType.Point:
        mlist = this.setPoints(geoObj)
        break
      case MapLayerType.Line:
        mlist = this.setLines(geoObj)
        break
      case MapLayerType.Polygon:
        mlist = this.setPolygons(geoObj)
        break
      case MapLayerType.Text:
        mlist = this.setLabels(geoObj)
        break
    }
    var oriEle = this.mapLayers[index].element
    this.mapLayers[index].element = oriEle.concat(mlist)
  }
  /**
   * 添加自定义动画到指定位置
   * @param {[MapAnimation]} geoObj 构成图层的几何集合,[{name: '', longitude:'',latitude:'',content:'',xOffset:,yOffset:}]
   */
  startAnimation(geoObj) {
    var markerList = []
    for (let i = 0, len = geoObj.length; i < len; i++) {
      if (geoObj[i].longitude == null || geoObj[i].latitude == null) return
      if (geoObj[i].longitude === 0 || geoObj[i].latitude === 0) return

      let pt = this.coordTrans.wgs84ToBaidu([[geoObj[i].longitude, geoObj[i].latitude]])

      var point = new window.BMapGL.Point(pt[0][0], pt[0][1])
      var AnimationOverlay = this.initAnimationOverlay()
      var aniMarker = new AnimationOverlay(point, geoObj[i].content, geoObj[i].xOffset, geoObj[i].yOffset)
      aniMarker.attributes = [{ name: geoObj[i].name }]
      this.map.addOverlay(aniMarker)
      markerList.push(aniMarker)
    }
    var lyr = this.getLyrIndexByName(animationLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      list = list.concat(markerList)
      this.mapLayers[lyr].element = list
    } else {
      this.mapLayers.push(new BDMapOverLayer(animationLayerName, markerList, true, MapLayerType.Animation))
    }
  }
  /**
   * 根据动画名称移除动画
   * @param {string} name 动画名
   */
  removeAnimation(name) {
    this.removeElementByFieldName(animationLayerName, name)
  }
  /**
   * 获取地图中心点坐标
   * @returns 地图中心点[number,number]
   */
  getCenter() {
    if (this.map) {
      var pt = this.map.getCenter()
      return [parseFloat(pt.lng.toFixed(8)), parseFloat(pt.lat.toFixed(8))]
    }
  }
  /**
   * 获取地图最小缩放级别
   * @returns 地图最小缩放级别
   */
  getMinZoom() {
    if (this.map) {
      return this.map.deepZoom.minZoom
    }
  }
  /**
   * 获取地图最大缩放级别
   * @returns 地图最大缩放级别
   */
  getMaxZoom() {
    if (this.map) {
      return this.map.deepZoom.maxZoom
    }
  }

  /**
   * 在地图上显示指定图层名图层
   * @param {string} layername
   */
  showLayer(layername) {
    var index = this.getLyrIndexByName(layername)
    if (index == -1) return
    super.setLayerVisibility(index, true)
    for (let i = 0; i < this.mapLayers[index].element.length; i++) {
      this.mapLayers[index].element[i].show()
    }
  }
  /**
   * 在地图上隐藏指定图层名图层
   * @param {string} layername
   */
  hideLayer(layername) {
    var index = this.getLyrIndexByName(layername)
    if (index == -1) return
    super.setLayerVisibility(index, false)
    for (let i = 0; i < this.mapLayers[index].element.length; i++) {
      this.mapLayers[index].element[i].hide()
    }
  }
  setLayerMask(boundaries, lStyle) {
    var bdpt = this.coordTrans.wgs84ToBaidu(boundaries)

    var ptArr = []
    for (var i = 0; i < bdpt.length; i++) {
      var pt = new window.BMapGL.Point(bdpt[i][0], bdpt[i][1])
      ptArr.push(pt)
    }
    var mapmask = new window.BMapGL.MapMask(ptArr, {
      isBuildingMask: true,
      isPoiMask: true,
      isMapMask: true,
      showRegion: 'inside'
    })
    //添加边线
    if (lStyle) {
      var blines = [
        {
          name: '1',
          path: boundaries,
          attribute: [],
          style: lStyle
        }
      ]
      this.addLinesLayer(maskLayerBoundaryName, blines)
    }
    this.mapLayers.push(new BDMapOverLayer(maskLayerName, [mapmask], true, MapLayerType.Mask))
    this.map.addOverlay(mapmask)
  }
  removeLayerMask() {
    this.removeLayer(maskLayerName)
    this.removeLayer(maskLayerBoundaryName)
  }
  searchByKeyword(keyword) {
    var local = new window.BMapGL.LocalSearch(this.map, {
      renderOptions: { map: this.map }
    })
    local.search(keyword)
  }
  startObjectMoving(name, path, duaration = null, speed, objectStyle) {
    //84转百度
    var bdpt = this.coordTrans.wgs84ToBaidu(path)
    var point = []
    for (var i = 0; i < bdpt.length; i++) {
      point.push(new window.BMapGL.Point(bdpt[i][0], bdpt[i][1]))
    }
    var lushu = new window.BMapGLLib.LuShu(this.map, point, {
      icon: new window.BMapGL.Icon(
        objectStyle.icon,
        new BMapGL.Size(objectStyle.width || 36, objectStyle.height || 36),
        { anchor: new BMapGL.Size(objectStyle.xOffset || 18, objectStyle.yOffset || 18) }
      ),
      enableRotation: true,
      speed: speed || 400
    })

    var lyr = this.getLyrIndexByName(movingObjectLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      list = list.concat({ name: name, lush: lushu })
      this.mapLayers[lyr].element = list
    } else {
      this.mapLayers.push(
        new BDMapOverLayer(movingObjectLayerName, [{ name: name, lush: lushu }], true, MapLayerType.Animation)
      )
    }
    lushu.start()
  }
  pauseObjectMoving(name) {
    var lyr = this.getLyrIndexByName(movingObjectLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      var cotrl = list.find((item) => item.name === name)
      if (cotrl) {
        cotrl.lush.pause()
      }
    }
  }
  continueObjectMoving(name) {
    var lyr = this.getLyrIndexByName(movingObjectLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      var cotrl = list.find((item) => item.name === name)
      if (cotrl) {
        cotrl.lush.start()
      }
    }
  }
  stopObjectMoving(name) {
    var lyr = this.getLyrIndexByName(movingObjectLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      var cotrl = list.find((item) => item.name === name)
      if (cotrl) {
        cotrl.lush.stop()
      }
    }
  }
  removeObjectMoving(name) {
    var lyr = this.getLyrIndexByName(movingObjectLayerName)
    if (lyr != -1) {
      var list = this.mapLayers[lyr].element
      var inx = list.findIndex((item) => item.name === name)
      this.map.removeOverlay(list[inx].lush._marker)
      list.splice(inx, 1)
    }
  }
  /*
   *注册事件
   */
  addEventListener(eventType, callback) {
    if (this.map == null) {
      return
    }
    this.map.addEventListener(eventType, function (e) {
      if (e.overlay) {
        return // 存在覆盖物
      }
      callback(e)
    })
  }
  /*
   *移除事件
   */
  removeEventListener(eventType) {
    if (this.map == null) {
      return
    }
    this.map.removeEventListener(eventType)
  }
  /**
   * 初始化自定义动态要素
   * @returns
   */
  initAnimationOverlay() {
    var AnimationOverlay = function (point, content, xoffset, yoffset) {
      this.point = point
      this.content = content
      this.xOffset = xoffset
      this.yOffset = yoffset
    }
    AnimationOverlay.prototype = new window.BMapGL.Overlay()
    AnimationOverlay.prototype.initialize = function (map) {
      this.map = map
      var node = document.createElement('div')
      node.innerHTML = this.content
      var divc = node.childNodes[0]
      map.getPanes().markerPane.appendChild(divc)
      this.div = divc
      return divc
    }
    AnimationOverlay.prototype.draw = function () {
      var position = this.map.pointToOverlayPixel(this.point)
      this.div.style.position = 'absolute'
      // this.div.style.left = `${position.x - this.width / 2}px`
      // this.div.style.top = `${position.y - this.height / 2}px`
      this.div.style.left = `${position.x - this.xOffset}px`
      this.div.style.top = `${position.y - this.yOffset}px`
    }
    return AnimationOverlay
  }

  /**
   * 编辑指定图层
   * @param {string} layername 图层名
   */
  enableLayerEditing(layername) {
    var indx = this.getLyrIndexByName(layername)
    if (indx == -1) {
      return
    }
    var type = this.mapLayers[indx].type
    var lyrs = this.mapLayers[indx].element
    for (var i = 0; i < lyrs.length; i++) {
      if (type == MapLayerType.Point) {
        lyrs[i].enableDragging()
      } else if (type == MapLayerType.Polygon || type == MapLayerType.Line) {
        lyrs[i].enableEditing()
      }
    }
  }
  /**
   * 停止编辑指定图层
   * @param {string} layername
   */
  disableLayerEditing(layername) {
    var indx = this.getLyrIndexByName(layername)
    if (indx == -1) {
      return
    }
    var type = this.mapLayers[indx].type

    var lyrs = this.mapLayers[indx].element
    for (var i = 0; i < lyrs.length; i++) {
      if (type == MapLayerType.Point) {
        lyrs[i].disableDragging()
      } else if (type == MapLayerType.Polygon || type == MapLayerType.Line) {
        lyrs[i].disableEditing()
      }
    }
  }
}
