import type { MutableRefObject } from 'react';
import type maplibregl from 'maplibre-gl';
import type { AisTarget } from '../../../entities/aisTarget/model/types';
import type { LegacyVesselInfo } from '../../../entities/legacyVessel/model/types';
import type { LegacyAlarmKind } from '../../../features/legacyDashboard/model/types';
import type { MapToggleState } from '../../../features/mapToggles/MapToggles';
import type { Map3DSettings, MapProjectionId } from '../types';
import { useGlobeShipLabels } from './useGlobeShipLabels';
import { useGlobeShipLayers } from './useGlobeShipLayers';
import { useGlobeShipHover } from './useGlobeShipHover';

export function useGlobeShips(
  mapRef: MutableRefObject<maplibregl.Map | null>,
  projectionBusyRef: MutableRefObject<boolean>,
  reorderGlobeFeatureLayers: () => void,
  opts: {
    projection: MapProjectionId;
    settings: Map3DSettings;
    shipData: AisTarget[];
    shipHighlightSet: Set<number>;
    shipHoverOverlaySet: Set<number>;
    shipOverlayLayerData: AisTarget[];
    shipLayerData: AisTarget[];
    shipByMmsi: Map<number, AisTarget>;
    mapSyncEpoch: number;
    onSelectMmsi: (mmsi: number | null) => void;
    onToggleHighlightMmsi?: (mmsi: number) => void;
    targets: AisTarget[];
    overlays: MapToggleState;
    legacyHits: Map<number, LegacyVesselInfo> | null | undefined;
    selectedMmsi: number | null;
    isBaseHighlightedMmsi: (mmsi: number) => boolean;
    hasAuxiliarySelectModifier: (ev?: { shiftKey?: boolean; ctrlKey?: boolean; metaKey?: boolean } | null) => boolean;
    onGlobeShipsReady?: (ready: boolean) => void;
    alarmMmsiMap?: Map<number, LegacyAlarmKind>;
  },
) {
  // Mercator 모드 선명 라벨
  useGlobeShipLabels(mapRef, projectionBusyRef, {
    projection: opts.projection,
    settings: opts.settings,
    shipData: opts.shipData,
    shipHighlightSet: opts.shipHighlightSet,
    overlays: opts.overlays,
    legacyHits: opts.legacyHits,
    selectedMmsi: opts.selectedMmsi,
    mapSyncEpoch: opts.mapSyncEpoch,
    alarmMmsiMap: opts.alarmMmsiMap,
  });

  // Globe 모드 선박 아이콘 레이어
  useGlobeShipLayers(mapRef, projectionBusyRef, reorderGlobeFeatureLayers, {
    projection: opts.projection,
    settings: opts.settings,
    shipData: opts.shipData,
    overlays: opts.overlays,
    legacyHits: opts.legacyHits,
    selectedMmsi: opts.selectedMmsi,
    isBaseHighlightedMmsi: opts.isBaseHighlightedMmsi,
    mapSyncEpoch: opts.mapSyncEpoch,
    onGlobeShipsReady: opts.onGlobeShipsReady,
    alarmMmsiMap: opts.alarmMmsiMap,
  });

  // Globe 호버 오버레이 + 클릭 선택
  useGlobeShipHover(mapRef, projectionBusyRef, reorderGlobeFeatureLayers, {
    projection: opts.projection,
    settings: opts.settings,
    shipLayerData: opts.shipLayerData,
    shipHoverOverlaySet: opts.shipHoverOverlaySet,
    legacyHits: opts.legacyHits,
    selectedMmsi: opts.selectedMmsi,
    mapSyncEpoch: opts.mapSyncEpoch,
    onSelectMmsi: opts.onSelectMmsi,
    onToggleHighlightMmsi: opts.onToggleHighlightMmsi,
    targets: opts.targets,
    hasAuxiliarySelectModifier: opts.hasAuxiliarySelectModifier,
  });
}
