// hooks/useLteModemMarkersLayer.js
import { useEffect, useState } from "react";
import L from "leaflet";
import { createAndSetDevices } from "../../utils/createAndSetDevices";
import { addContextMenuToMarker } from "../../utils/addContextMenuToMarker";
import { checkOverlappingMarkers } from "../../utils/mapUtils";

const useLteModemMarkersLayer = (map, oms, GisSystemStatic, priorityConfig) => {
  const [lteModemMarkers, setLteModemMarkers] = useState([]);

  useEffect(() => {
    if (GisSystemStatic && GisSystemStatic.length && map) {
      createAndSetDevices(5, setLteModemMarkers, GisSystemStatic, priorityConfig); // GSM-Modem
    }
  }, [GisSystemStatic, map, priorityConfig]);

  useEffect(() => {
    if (map && lteModemMarkers.length) {
      lteModemMarkers.forEach((marker) => {
        marker.addTo(map);
        oms.addMarker(marker);

        // Popup beim Überfahren mit der Maus öffnen und schließen
        marker.on("mouseover", function () {
          this.openPopup();
        });
        marker.on("mouseout", function () {
          this.closePopup();
        });

        addContextMenuToMarker(marker);
      });
      // Disable map context menu
      map.options.contextmenu = false;
      map.options.contextmenuItems = [];

      oms.map.options.contextmenu = false;
      oms.map.options.contextmenuItems = [];

      // Call the function to check for overlapping markers
      checkOverlappingMarkers(oms, map);
    }
  }, [map, lteModemMarkers]);

  return lteModemMarkers;
};
export default useLteModemMarkersLayer;
