import { getAddressForService } from "@/api/secondHandCar"
// 获取定位, 返回 经纬度
export function getLocal(mapContext) {
  return new Promise((resolve, reject) => {
    mapContext.plugin('AMap.Geolocation', function () {
      let geolocation = new AMap.Geolocation({
        enableHighAccuracy: true, // 是否使用高精度定位，默认：true
        timeout: 10000, // 设置定位超时时间，默认：无穷大
        offset: [10, 20],  // 定位按钮的停靠位置的偏移量
        zoomToAccuracy: true,  //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
        position: 'RB' //  定位按钮的排放位置,  RB表示右下
      })

      geolocation.getCurrentPosition(function(status, result){
        if(status == 'complete'){
          resolve(result.position)
        }else{
          reject(result)
        }
      });
    })
  })
}

// 逆地址解析，根据经纬度获取详细地址
export function getAddress(mapContext, lnglat) {
  return new Promise((resolve, reject) => {
    mapContext.plugin('AMap.Geocoder', function () {
      let geocoder = new AMap.Geocoder({
        // city: "010", //城市设为北京，默认：“全国”
      });

      geocoder.getAddress(lnglat, function(status, result) {
        if (status === 'complete' && result.info === 'OK') {
          // result为对应的地理位置详细信息
          resolve(result)
        } else {
          console.log(result)
          alert(JSON.stringify(result))
          reject(result)
        }
      })
    })
  })
}

// 输入提示
export function searchFun(mapContext, cityCode, keyword) {
  return new Promise((resolve) => {
     getAddressForService({
      city: '',
      address: keyword
    }).then((res) => {
      if(res.code == 200) {
        resolve(res?.data?.pois || [])
      } else {
        resolve([])
      }
    }).catch(() => {
      resolve([])
    })
    /*mapContext.plugin('AMap.AutoComplete', function(){
      var autoOptions = {
        city: cityCode || '全国',
      };
      // 实例化AutoComplete
      var autoComplete= new AMap.AutoComplete(autoOptions);
      // 根据关键字进行搜索
      autoComplete.search(keyword, function(status, result) {
        // 搜索成功时，result即是对应的匹配数据
        if(result.info == 'OK') {
          resolve(result.tips)
        } else {
          resolve([])
        }
      })
    })*/
  })
}

// 路径规划
export function getRoad( mapContext, startLng, startLat, endLng, endLat ) {
  return new Promise((resolve, reject) => {
    mapContext.plugin('AMap.Driving', function() {
      let driving = new AMap.Driving({
        // 驾车路线规划策略，AMap.DrivingPolicy.LEAST_TIME是最快捷模式
        // policy: AMap.DrivingPolicy.LEAST_TIME,
        // map: mapContext
      })

      let startLngLat = [startLng, startLat]
      let endLngLat = [endLng, endLat]

      driving.search(startLngLat, endLngLat, function (status, result) {
        // 未出错时，result即是对应的路线规划方案
        if (status === 'complete') {
          resolve(result)
        } else {
          reject(result)
        }
      })
    })
  })
}


/*
function drawRoute(route, map) {
  let path = parseRouteToPath(route)

  let routeLine = new AMap.Polyline({
    path: path,
    isOutline: true,
    outlineColor: '#ffeeee',
    borderWeight: 2,
    strokeWeight: 5,
    strokeOpacity: 0.9,
    strokeColor: '#0091ff',
    lineJoin: 'round'
  })

  map.add(routeLine);
}
*/
