let defaultCenter = "石家庄";
export let map;
class gaodeMap {
  //构造函数中设置中央点默认值
  constructor(center) {
    this.center = center?center:defaultCenter;
    map = new AMap.Map('container', {
      //mask: addMask(result.districtList[0].boundaries),
      center: [114.72995, 38.37417],
      // resizeEnable: true,
      disableSocket: true,
      viewMode: '3D',
      showLabel: true,
      // labelzIndex: 110,
      pitch: 8,
      zoom: 9,
      //mapStyle: 'amap://styles/darkblue',
      // mapStyle: 'amap://styles/3b679a15f448a4740ba2ff7524e1a4ae',
    });
    this.districtBoundaries();
  }

  /**
   * 获取map
   * @returns {AMap.Map}
   */
  get getMap(){
    return this.map;
  }

  /**
   * 重新设置中央点
   * @param value
   */
  set setCenter(value){
    this.center = value;
    this.districtBoundaries();
  }

  /**
   * 设置城市边界
   */
  districtBoundaries(){
   let that = this;
   let opts = {
     subdistrict: 0,
     extensions: 'all',
     level: 'city'
   };
   let district = new AMap.DistrictSearch(opts);
   district.search(that.center, function (status, result) {
     if(status == "complete"){
       let defaultCenter = [];
       defaultCenter.push(result.districtList[0].center.lng);
       defaultCenter.push(result.districtList[0].center.lat);
       map.setCenter(defaultCenter);
       let  bounds =result.districtList[0].boundaries;
       for (let i = 0; i < bounds.length; i += 1) {
         new AMap.Polyline({
           path: bounds[i],
           strokeColor: '#0088ff',
           strokeWeight: 5,
           strokeOpacity: 7,
           map: map,
         });
       }
     }
   })
 }

  /**
   * 添加Market
   * @param lnglats
   */
  addMarker(lnglats){
    let infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(-13, -70)});
    for (let i = 0, marker; i < lnglats.length; i++) {
      let marker = new AMap.Marker({
        position: lnglats[i],
        map: map,
        offset: new AMap.Pixel(-13, -30),
        // 设置是否可以拖拽
        draggable: true,
        cursor: 'move',
        // 设置拖拽效果
        raiseOnDrag: true
      });
      marker.content  = '<h4>我是第' + (i + 1) + '个XXX</h4>';
      marker.content += '<h6>经度：'+lnglats[i][0]+'</h6>';
      marker.content += '<h6>纬度：'+lnglats[i][1]+'</h6>';
      marker.content += '<button class="btn btn-success btn-xs">历史轨迹</button>';
      marker.content += '<button class="btn btn-warning btn-xs">实时跟踪 </button>';
      marker.content += '<button class="btn btn-danger btn-xs">设置</button>';

      marker.on('mouseover', infoOpen);
      //注释后打开地图时默认关闭信息窗体
      //marker.emit('mouseover', {target: marker});

      marker.on('mouseout', infoClose);
      marker.on('click', newMAp);
    }
    map.setFitView();

    //鼠标点击事件,设置地图中心点及放大显示级别
    function newMAp(e) {
      //map.setCenter(e.target.getPosition());
      map.setZoomAndCenter(13, e.target.getPosition());
      let infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(-13, -70)});
      infoWindow.setContent(e.target.content);
      infoWindow.open(map, e.target.getPosition());
    }

    function infoClose(e) {
      infoWindow.close(map, e.target.getPosition());
    }

    function infoOpen(e) {
      infoWindow.setContent(e.target.content);
      infoWindow.open(map, e.target.getPosition());
    }
  }

  /**
   * 添加折线
   * @param path
   */
  addPolyline(path){
    let polylines = [];
    for(let i = 0;i< path.length; i++){
      let polyline = new AMap.Polyline({
        path: path[i],
        strokeColor: "green",
        strokeWeight: 8,
        strokeOpacity: 0.9,
        zIndex: 50,
        bubble: true,
      })
      polylines.push(polyline);
      let polyEditor = new AMap.PolyEditor(map, polyline);
      //创建右键菜单
      let contextMenu = new AMap.ContextMenu();
      //右键放大
      contextMenu.addItem("编辑Polyline", function () {
        polyEditor.setTarget(polyline);
        polyEditor.open();
      }, 0);
      contextMenu.addItem("结束编辑", function () {
        polyEditor.close();
      }, 0);
      //添加事件
      polyline.on("rightclick",function (e) {
        contextMenu.open(map, e.lnglat);
      })
    }
    map.add(polylines);
    // 缩放地图到合适的视野级别
    map.setFitView();
  }



}
export default gaodeMap;
