function solvePointWkt(array) {
  let features = [];
  for (let item of array) {
    features.push(
      turf.point(
        item.wkt.split(",").map((item) => Number(item)),
        item
      )
    );
  }
  return turf.featureCollection(features);
}
function solvePointLnglat(array) {
  let features = [];
  for (let item of array) {
    features.push(turf.point([item.longitude, item.latitude], item));
  }
  return turf.featureCollection(features);
}
function solvePolygonsWkt(array) {
  let features = [];
  for (let item of array) {
    let wktarr = item.wkt.split(";");
    if (wktarr.length < 4) {
      // return null;
    } else {
      for (let i = 0; i < wktarr.length; i++) {
        wktarr[i] = wktarr[i].split(",");
        for (let j = 0; j < wktarr[i].length; j++) {
          wktarr[i][j] = Number(wktarr[i][j]);
        }
      }
      features.push(turf.polygon([wktarr], item));
    }
  }
  return turf.featureCollection(features);
}
function solveSingleLineArray(array) {
  let features = [];
  features.push(turf.lineString(array));
  return turf.featureCollection(features);
}

// 灯态icon
export function addOrUpdateLightIcon(map, layerName, arr) {
  let geo = solvePointLnglat(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "symbol",
      source: layerName,
      layout: {
        "icon-allow-overlap": true,
        "icon-ignore-placement": true,
        "icon-size": 0.7,
        "icon-image": ["concat", "light", ["get", "ts"]],
        "icon-pitch-alignment": "map",
        "icon-rotate": ["get", "roadDir"],
        // "icon-rotate": 45,
        "icon-rotation-alignment": "map",
      },
    });
  }
}
export function addOrUpdateLightText(map, layerName, arr) {
  let geo = solvePointLnglat(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "symbol",
      source: layerName,
      layout: {
        "text-field": ["get", "remainTime"],
        "text-size": 12,
        "text-pitch-alignment": "map",
        "text-rotation-alignment": "map",
        "text-rotate": ["get", "roadDir"],
      },
      paint: {
        "text-color": "white",
        "text-halo-blur": 0.5,
        "text-halo-width": 0.5,
        "text-halo-color": [
          "match",
          ["get", "state"], // 属性字段名称
          "1",
          "rgba(255, 130, 130, 0.5)",
          "2",
          "rgba(255, 179, 0, 0.5)",
          "3",
          "rgba(48, 255, 141, 0.5)",
          "rgba(255,255,255,1)", // 默认值，如果没有匹配到上述枚举值
        ],
      },
    });
  }
}

// 相位空放区域polygon
export function addOrUpdatePhasePolygon(map, layerName, arr) {
  let geo = solvePolygonsWkt(arr);
  if (geo) {
    if (!map.getSource(layerName)) {
      map.addSource(layerName, {
        type: "geojson",
        data: geo,
      });
    } else {
      map.getSource(layerName).setData(geo);
    }
    if (!map.getLayer(layerName)) {
      map.addLayer({
        id: layerName,
        source: layerName,
        type: "fill",
        paint: {
          "fill-color": "#00ff3c",
          "fill-opacity": 0.6,
        },
      });
    }
  }
}

// 直左冲突点位热力图
export function addOrUpdateConflictPoint(map, layerName, arr) {
  let geo = solvePointLnglat(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "circle",
      source: layerName,
      paint: {
        "circle-radius": 30,
        "circle-blur": 1,
        "circle-color": "#efba0a",
        "circle-pitch-alignment": "map",
      },
    });
  }
}

// 可移动基站图标
export function addOrUpdateMobile(map, layerName, arr) {
  let geo = solvePointLnglat(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "symbol",
      source: layerName,
      layout: {
        "icon-image": "mobile",
        "icon-size": 0.7,
        "icon-offset": [0, 0],
      },
    });
  }
}

// 相机图标图层
export function addOrUpdateHomeCameraLayer(map, layerName, arr, callback) {
  let geo = solvePointWkt(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "symbol",
      source: layerName,
      layout: {
        "icon-image": "homeCamera",
        "icon-size": 0.4,
        "icon-offset": [0, 0],
        "icon-allow-overlap": true,
      },
    });
    map.on("click", layerName, callback);
  }
}

// 更新zoom值较小时的车辆轨迹point
export function addOrUpdateVehicle(map, layerName, arr) {
  let geo = solvePointLnglat(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "circle",
      source: layerName,
      paint: {
        "circle-blur": 1,
        "circle-color": "#06f4fe",
        "circle-pitch-alignment": "map",
      },
    });
  }
}

// 行人区域人数映射fill图层
export function addOrUpdateWaitingPolygon(map, layerName, arr) {
  let geo = solvePolygonsWkt(arr);
  console.log("geo...", geo);
  if (geo) {
    if (!map.getSource(layerName)) {
      map.addSource(layerName, {
        type: "geojson",
        data: geo,
      });
    } else {
      map.getSource(layerName).setData(geo);
    }
    if (!map.getLayer(layerName)) {
      map.addLayer({
        id: layerName,
        source: layerName,
        type: "fill",
        paint: {
          "fill-color": [
            "match",
            ["get", "val"],
            1,
            "#009c3e",
            2,
            "#009c3e",
            3,
            "#009c3e",
            "#009c3e",
          ],
          "fill-opacity": 0.6,
        },
      });
    }
  }
}

// 中间层级车辆图片图层
export function addOrUpdateVehiclePic(map, layerName, arr) {
  let geo = solvePointLnglat(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer(
      {
        id: layerName,
        type: "symbol",
        source: layerName,
        layout: {
          "icon-allow-overlap": true,
          "icon-ignore-placement": true,
          "icon-size": 0.2,
          "icon-image": ["concat", "car", ["get", "originalType"]],
          "icon-pitch-alignment": "map",
          "icon-rotate": ["get", "courseAngle"],
          "icon-rotation-alignment": "map",
        },
      },
      "vehicle3D"
    );
  }
}

// 慢行交通行人路线
export function addOrUpdateSlowLines(map, layerName, arr) {
  let geo = solveSingleLineArray(arr);
  if (!map.getSource(layerName)) {
    map.addSource(layerName, {
      type: "geojson",
      data: geo,
    });
  } else {
    map.getSource(layerName).setData(geo);
  }
  if (!map.getLayer(layerName)) {
    map.addLayer({
      id: layerName,
      type: "line",
      source: layerName,
      paint: {
        "line-color": "red",
        "line-width": 2,
      },
    });
  }
}
