import { WKT } from 'ol/format';
import { Overlay } from 'ol';
import { Style, Stroke, Fill, Icon } from 'ol/style';
import Feature from 'ol/Feature';
import { Point } from 'ol/geom';

interface PointType {
  latitudet: number;
  longitude: number;
}
function featureToWkt(feature: any) {
  const wkt = new WKT().writeFeature(feature, {
    dataProjection: 'EPSG:4326',
    featureProjection: 'EPSG:4326',
  });
  return wkt;
}

// 创建提示层
function createMouseTips(innerHTML = '左键添加点，双击结束绘制') {
  const tips = document.createElement('div');
  tips.innerHTML = innerHTML;
  tips.className = 'mouseTips';
  const mouseTips = new Overlay({
    element: tips,
    offset: [15, 0],
    positioning: 'center-left',
  });
  return mouseTips;
}
function createStyle(lineWidth?: number, strokeColor?: string, fillColor?: string) {
  return new Style({
    fill: new Fill({
      // 填充样式
      color: fillColor || 'rgba(255, 255, 255, 0.2)',
    }),
    stroke: new Stroke({
      // 线样式
      color: strokeColor || '#409EFF',
      width: lineWidth || 2,
    }),
    image: new Icon({
      src: '/src/views/lifeline/Map/img/locate.png',
      scale: 0.2,
    }),
  });
}
/**
 * layer:地图中的图层
 * points:点集合
 */
function addPoint(layer: any, points: Array<PointType>) {
  const features = points.map(function (point) {
    return new Feature({
      geometry: new Point([point.latitudet, point.longitude]),
    });
    layer.getSource().addFeatures(features);
  });
}
/**
 * layer:地图中的图层
 * plygons：wkt集合
 */
function addPlygon(layer: any, plygons: Array<String>) {
  const features = plygons.map(function (wkt) {
    const format = new WKT();
    const geometry = format.readGeometry(wkt);
    return new Feature({
      geometry: geometry,
    });
  });
  layer.getSource().addFeatures(features);
}

export { featureToWkt, createMouseTips, createStyle, addPoint, addPlygon };
