import route from "@/router/index";
export function allPositions(options, show) {
  options.forEach((item, index) => {
    let graphic = window.marsMap.getLayerById(
      item.geometry.coordinates[0][0][0]
    );
    // console.log(item,'item')
    if (!graphic) {
      //扩散效果
      graphic = new mars3d.layer.GeoJsonLayer({
        id: item.geometry.coordinates[0][0][0],
        name: "zhendi",
        data: item,
        symbol: {
          type: "diffuseWall",
          merge: true,
          styleOptions: {
            color: "#0066cc", // 较深的蓝色(顶部)
            baseColor: "#002244", // 更深的蓝色(底部),增强对比
            opacity: 0.7, // 顶部略透明,突出渐变
            diffHeight: 120, // 高度适中,扩散明显
            speed: 5, // 中等速度
            outline: true,
            outlineColor: "#0066cc", // 轮廓使用较深蓝色
            outlineOpacity: 1,
            outlineWidth: 3.0 // 加粗轮廓,边界清晰
          }
        },
        show,
        popup: item.name,
        flyTo: true
      });
      window.marsMap.addLayer(graphic);
    } else {
      graphic.show = show;
    }
    let graphics = window.marsMap.getLayerById(
      item.geometry.coordinates[0][0][0] + 10
    );
    if (!graphics) {
      //区域
      graphics = new mars3d.layer.GeoJsonLayer({
        id: item.geometry.coordinates[0][0][0] + 10,
        name: "zhendi",
        data: item,
        symbol: {
          styleOptions: {
            fill: true,
            color: "#3388cc",
            opacity: 0.5,
            outline: true,
            outlineStyle: {
              width: 2,
              opacity: 0.1
              //   materialType: "CircleScan",
              //   materialOptions: {
              //     color: item.properties.color
              //   }
            }
          }
        },
        show,
        popup: item.name
      });
      window.marsMap.addLayer(graphics);
    } else {
      graphics.show = show;
      graphics.flyTo();
    }
    // if (item.regionModels.length !== 0) {
    //   item.regionModels.forEach((val, index) => {
    // }
  });
}
export function allDevices(options, type, show) {
  let graphicLayer = window.marsMap.getLayerById("devLog");
  if (!graphicLayer) {
    graphicLayer = new mars3d.layer.GraphicLayer({ id: "devLog" });
    window.marsMap.addLayer(graphicLayer);
  } else {
    graphicLayer.clear();
  }
  if (type === "all") {
    options.forEach((item, index) => {
      if (item.devices.length !== 0) {
        item.devices.forEach((deviceItem, index) => {
          let graphic = graphicLayer.getGraphicById(deviceItem.id);
          if (!graphic) {
            graphic = new mars3d.graphic.BillboardEntity({
              id: deviceItem.id,
              name: "设备",
              position: new mars3d.LngLatPoint(
                deviceItem.lon,
                deviceItem.lat,
                0
              ),
              style: {
                image: require(`@/assets/img/device/deviceIcon/${deviceItem.icon}.png`),
                // visibleDepth:false,
                scale: 1.5,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                clampToGround: true,
                scaleByDistance: true,
                scaleByDistance_near: 100,
                scaleByDistance_nearValue: 0.5
              },
              attr: deviceItem,
              show
            });
            bindLayerPopup(graphicLayer, graphic);
            graphicLayer.addGraphic(graphic);
          } else {
            graphic.show = show;
          }
        });
      }
    });
  } else {
    options.devices.forEach((item, index) => {
      // const image = getImageSrc(item.deviceType.type);
      let graphic = graphicLayer.getGraphicById(item.id);
      if (!graphic) {
        graphic = new mars3d.graphic.BillboardEntity({
          id: item.id,
          name: "设备",
          position: new mars3d.LngLatPoint(item.lon, item.lat, 0),
          style: {
            image: require(`@/assets/img/device/deviceIcon/${item.icon}.png`),
            // visibleDepth:false,
            scale: 1.5,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            clampToGround: true,
            scaleByDistance: true,
            scaleByDistance_near: 100,
            scaleByDistance_nearValue: 0.5
          },
          attr: item,
          show
        });
        bindLayerPopup(graphicLayer, graphic);
        graphicLayer.addGraphic(graphic);
      } else {
        graphic.show = show;
      }
    });
  }
}
import imgUav from "@/assets/img/uavimg.png";
//设备广告牌
function bindLayerPopup(graphicLayer, graphic) {
  let attr = graphic.attr;
  if (graphic.name === "无人机") {
    graphic.bindPopup(
      ` 
          ${attr.device_type}
          
          
            
            
            
            
            
              SN码
              ${attr.serial_number}
             
            
           
         
      `,
      {
        closeButton: false,
        hasZIndex: false,
        zIndex: 2
      }
    );
    graphic.on(mars3d.EventType.popupRender, function (event) {
      const container = event.container; // popup对应的DOM
      const params = graphic?.attr;
      if (!params) {
        return;
      }
      const lblHeight = container.querySelector("#lblHeight");
      if (lblHeight) {
        lblHeight.innerHTML = params.height + "米";
      }
      const lblDistance = container.querySelector("#lblDistance");
      if (lblDistance) {
        lblDistance.innerHTML = Math.round(params.distance) + "米";
      }
      const lblSpeed = container.querySelector("#lblSpeed");
      if (lblSpeed) {
        const speeds = [params.speed_E, params.speed_N, params.speed_U].filter(
          (speed) => speed !== undefined
        );
        const maxSpeed = speeds.length > 0 ? Math.max(...speeds) : undefined;
        lblSpeed.innerHTML = maxSpeed + "m/s";
      }
    });
  } else if (graphic.name === "飞手") {
  } else {
    let wPath = window.document.location.href;
    let pathName = route.path;
    let pos = wPath.indexOf(pathName);
    let localhostPath = wPath.substring(0, pos);
    let uploadUrl =
      process.env.VUE_APP_API_URL === "/"
        ? localhostPath
        : process.env.VUE_APP_API_URL;
    graphic.bindPopup(
      ` 
          ${attr.name}
          
          
            
            
            
            
              状态
              ${attr.isOnline ? "在线" : "离线"}
             
           
         
      `,
      {
        closeButton: false,
        hasZIndex: false,
        zIndex: 2
      }
    );
  }
}
export function mapUavFiex(options) {
  let map = window.marsMap; // 记录map
  let graphicLayer = map.getLayerById("uavFiex");
  if (!graphicLayer) {
    // eslint-disable-next-line no-undef
    graphicLayer = new mars3d.layer.GraphicLayer({ id: "uavFiex" });
    map.addLayer(graphicLayer);
  }
  if (options.length === 0) {
    graphicLayer.remove();
  } else {
    options.forEach((item) => {
      let graphic = graphicLayer.getGraphicById(item.BatchId);
      // let startPoint = graphicLayer.getGraphicById(item.BatchId + "start"); // 起始点
      //发现无人机
      if (!graphic) {
        // eslint-disable-next-line no-undef
        graphic = new mars3d.graphic.Route({
          id: item.BatchId,
          name: "无人机",
          maxCacheCount: -1,
          polyline: {
            width: 2,
            opacity: 1,
            randomColor: true,
            show: true
          },
          billboard: {
            image: require("@/assets/img/uav.svg"),
            scale: 0.5,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM
          },
          attr: item
        });
        graphicLayer.addGraphic(graphic);
      }
      graphic.attr = item;
      bindLayerPopup(graphicLayer, graphic);
      let app_graphic = graphicLayer.getGraphicById(item.BatchId + "_app");
      // let startPoint = graphicLayer.getGraphicById(item.BatchId + "start"); // 起始点
      //发现无人机
      if (!app_graphic) {
        // eslint-disable-next-line no-undef
        app_graphic = new mars3d.graphic.Route({
          id: item.BatchId + "_app",
          name: "飞手",
          maxCacheCount: -1,
          label: {
            text: "飞手位置",
            font_size: 14,
            font_family: "微软雅黑"
          },
          polyline: {
            width: 2,
            opacity: 1,
            randomColor: true,
            show: true
          },
          point: {
            color: "#00ffff",
            pixelSize: 8,
            outlineColor: "#ffffff",
            outlineWidth: 2
          },
          attr: item
        });
        graphicLayer.addGraphic(app_graphic);
      }
      // 起始点位
      // if (item.lon !== 0 && item.lat !== 0) {
      //   if (!startPoint) {
      //     // eslint-disable-next-line no-undef
      //     startPoint = new mars3d.graphic.BillboardEntity({
      //       id: item.BatchId + "start",
      //       // eslint-disable-next-line no-undef
      //       position: new mars3d.LngLatPoint(item.lon, item.lat, item.alt),
      //       style: {
      //         image: require("@/assets/img/uav.svg"),
      //         scale: 1,
      //         // eslint-disable-next-line no-undef
      //         horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      //         // eslint-disable-next-line no-undef
      //         verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      //         label: {
      //           text: "起始点",
      //           font: "14px Arial",
      //           color: "#ffffff",
      //           outline: true,
      //           outlineColor: "#000000",
      //           outlineWidth: 2,
      //           // eslint-disable-next-line no-undef
      //           horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      //           // eslint-disable-next-line no-undef
      //           verticalOrigin: Cesium.VerticalOrigin.BOTTOM
      //         }
      //       },
      //       show: false,
      //       attr: item
      //     });
      //     graphicLayer.addGraphic(startPoint);
      //   }
      // }
      let graphicDevice = window.marsMap.getLayerById("devLog");
      if (graphicDevice) {
        let deviceGraphic = graphicDevice.getGraphicById(item.DeviceId);
        if (deviceGraphic !== undefined) {
          deviceGraphic.setOptions({
            style: {
              scale: 2
            }
          });
        }
      }
    });
    //绑定气泡弹窗
    graphicLayer.eachGraphic((car) => {
      // 取出对应无人机的轨迹列表
      options.map((item) => {
        if (item.BatchId === car.id) {
          // eslint-disable-next-line no-undef
          const point = new mars3d.LngLatPoint(
            item.drone_lon,
            item.drone_lat,
            item.height
          );
          car.addDynamicPosition(point, 0);
        } else if (item.BatchId + "_app" === car.id) {
          // eslint-disable-next-line no-undef
          const point = new mars3d.LngLatPoint(item.app_lon, item.app_lat, 0);
          car.addDynamicPosition(point, 0);
        }
      });
    });
  }
}