import axios from 'axios'

/**
 * @description:加载WMS服务
 * @param map
 * @param url String
 * @param option Object
 *
 */
export function loadWMS (map, url, option) {
  if (url) {
    const id = url.split('layer=')[1]
    const source = map.getSource(option.source)
    const layer = map.getLayer(option.id)
    if (source === undefined && layer === undefined) {
      map.addSource(option.source, {

        type: 'raster',
        tiles: [url],
        tileSize: 512
      })
      map.addLayer({
        id: id,
        type: option.type,
        source: option.source,
        paint: option.paint
      })
    } else {
      alert('该图层已存在!请勿重复添加。')
    }
  } else {
    alert('请输入WMS服务地址!')
  }
}

/**
 * @description:根据id删除指定图层
 * @param  map
 * @param  url
 */
export function removeLayer (map, url, option) {
  const id = url.split('layer=')[1]

  map.removeLayer(id)
  map.removeSource(option.source)
}

/**
 * @description:移除WFS服务图层
 * @param {*} map
 * @param {*} url
 * @param {*} option
 */
export function removeWFSLayer (map, option) {
  map.removeLayer(option.id)
  map.removeSource(option.id)
}
/**
 * @description:加载WFS服务
 * @param url String
 * @param option Object
 */
export function loadWFSLayer (map, url, option) {
  const source = map.getSource(option.id)
  const layer = map.getLayer(option.id)
  if (source === undefined && layer === undefined) {
    if (url) {
      axios.get(url).then((res) => {
        const features = res.data.features
        // console.log(features, "元素层");
        // let layerType = null;
        // let geometryType = features[0].geometry.type;
        const addLayerOption = {
          id: option.id,
          type: option.type,
          source: {
            type: 'geojson',
            data: {
              type: 'FeatureCollection',
              features: features
            }
          },
          layout: {
            visibility: 'visible'
          },
          paint: option.paint
        }
        // if (geometryType == "MultiLineString") {
        //   addLayerOption.type = "line";
        // } else if (geometryType == "Line") {
        //   addLayerOption.type = "line";
        // } else if (geometryType == "Point") {
        //   addLayerOption.type = "circle";
        // } else if (geometryType == " MultiPoint") {
        //   addLayerOption.type = "circle";
        // } else if (geometryType == "Polygon") {
        //   addLayerOption.type = "fill";
        //   addLayerOption.paint = {
        //     "fill-color": "rgba(200,100,240,0.4)",
        //     "fill-outline-color": "rgba(200,100,240,1)",
        //   };
        // } else if (geometryType == "MultiPolygon") {
        //   addLayerOption.type = "fill";
        //   addLayerOption.paint = {
        //     "fill-color": "rgba(200,100,240,0.4)",
        //     "fill-outline-color": "rgba(200,100,240,1)",
        //   };
        // }

        map.addLayer(addLayerOption)
      })
    } else {
      alert('请输入WFS地址!')
    }
  } else {
    alert('该图层已存在!请勿重复添加')
  }
}

/**
 * @description:加载WMTS服务
 * @param map
 * @param url String
 * @param option Object
 */
export function loadWMTS (map, url, option) {
  const source = map.getSource(option.source)
  const layer = map.getLayer(option.id)
  if (source === undefined && layer === undefined) {
    if (url) {
      const id = url.split('layer=')[1]
      map.addSource(option.source, {
        type: 'raster',
        tiles: [url],

        tileSize: 256
      })
      map.addLayer({
        id: id,
        type: option.type,
        source: option.source,
        paint: option.paint
      })
    } else {
      alert('请输入WMTS服务地址!')
    }
  } else {
    alert('该图层已存在!请勿重复添加。')
  }
}

/**
 * @description:加载TMS服务
 * @param map
 * @param url String
 * @param option Object
 */
export function loadTMSLayer (map, url, option) {
  const source = map.getSource(option.source)
  const layer = map.getLayer(option.id)
  if (source === undefined && layer === undefined) {
    if (url) {
      const id = url.split('layer=')[1]
      map.addSource(option.source, {
        type: 'raster',
        tiles: [url],
        scheme: 'tms',
        tileSize: 256
      })
      map.addLayer({
        id: id,
        type: option.type,
        source: option.source,
        paint: option.paint
      })
    } else {
      alert('请输入TMS服务地址!')
    }
  } else {
    alert('该图层已存在!请勿重复添加')
  }
}
