import axios from 'axios'
import { wgs84togcj02 } from '@/utils/coordinate-util'
export default function getDroneLine(url, map, isfitView = true) {
  if (!url) return Promise.resolve(null)
  const temp = url.split('//')
  url = temp[temp.length - 1]
  url = process.env.VUE_APP_API_BASE_URL + url
  const generateMarker = () => {
    const dotContainer = document.createElement('div')
    dotContainer.style = 'height:16px;width:16px;background-color:#5798FF;border-radius:50%;position:relative'
    const dotInner = document.createElement('div')
    dotInner.style = 'height:8px;width:8px;background-color:#fff;border-radius:50%;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)'
    dotContainer.appendChild(dotInner)
    return dotContainer
  }
  return axios.get(url).then((res) => {
    const text = res.data
    let arr = text.split('\r\n')
    arr.shift()
    arr.pop()
    arr = arr.map((text) => {
      return text.split(/\s+/)
    })
    const polylinePath = []
    // let markerArr = [];
    const overlayGroup = new AMap.OverlayGroup()

    for (const point of arr) {
      if (point[9] !== 0) {
        const coordinate = wgs84togcj02([point[9], point[8]])
        polylinePath.push(coordinate)
        const marker = new AMap.Marker({
          position: new AMap.LngLat(coordinate[0], coordinate[1]),
          content: generateMarker(),
          anchor: 'center',
          zIndex: 20
        })
        overlayGroup.addOverlay(marker)
      }
    }

    const polyline = new AMap.Polyline({
      path: polylinePath,
      strokeColor: '#5798FF',
      strokeWeight: 4,
      strokeStyle: 'dashed'
    })
    overlayGroup.addOverlay(polyline)
    map.add(overlayGroup)
    if (isfitView) {
      map.setFitView(overlayGroup.getOverlays(), true, [70, 100, 300, 50])
    }
    return overlayGroup
  })
}
