import { OSMWay } from "@repo/db";
import { create } from "zustand";

export interface MapStore {
	contextMenu: {
		lat: number;
		lng: number;
	} | null;

	map: {
		center: L.LatLngExpression;
		zoom: number;
	};
	setMap: (map: MapStore["map"]) => void;
	openMissionMarker: {
		id: number;
		tab: "home" | "details" | "patient" | "log";
	}[];
	setOpenMissionMarker: (mission: { open: MapStore["openMissionMarker"]; close: number[] }) => void;
	openAircraftMarker: {
		id: number;
		tab: "home" | "fms" | "aircraft" | "mission" | "chat";
	}[];
	setOpenAircraftMarker: (aircraft: {
		open: MapStore["openAircraftMarker"];
		close: number[];
	}) => void;
	searchElements: OSMWay[];
	setSearchElements: (elements: MapStore["searchElements"]) => void;
	toggleSearchElementSelection: (elementId: number, forceState?: boolean) => void;
	setContextMenu: (popup: MapStore["contextMenu"]) => void;
	searchPopup: {
		lat: number;
		lng: number;
		elementId: number;
	} | null;
	setSearchPopup: (popup: MapStore["searchPopup"]) => void;
	aircraftTabs: {
		[aircraftId: string]: "home" | "fms" | "aircraft" | "mission" | "chat";
	};
	setAircraftTab: (aircraftId: number, tab: MapStore["aircraftTabs"][number]) => void;
	userSettings: {
		settingsAutoCloseMapPopup: boolean;
	};
	setUserSettings: (settings: Partial<MapStore["userSettings"]>) => void;
}

export const useMapStore = create<MapStore>((set, get) => ({
	openMissionMarker: [],
	setOpenMissionMarker: ({ open, close }) => {
		const { settingsAutoCloseMapPopup } = get().userSettings;

		const oldMarkers =
			settingsAutoCloseMapPopup && open.length > 0
				? [] // If auto-close is enabled and opening a new popup, close all others
				: get().openMissionMarker.filter(
						(m) => !close.includes(m.id) && !open.find((o) => o.id === m.id),
					);
		set(() => ({
			openMissionMarker: [...oldMarkers, ...open],
		}));
	},
	openAircraftMarker: [],
	setOpenAircraftMarker: ({ open, close }) => {
		const { settingsAutoCloseMapPopup } = get().userSettings;

		const oldMarkers =
			settingsAutoCloseMapPopup && open.length > 0
				? [] // If auto-close is enabled and opening a new popup, close all others
				: get().openAircraftMarker.filter(
						(m) => !close.includes(m.id) && !open.find((o) => o.id === m.id),
					);
		set(() => ({
			openAircraftMarker: [...oldMarkers, ...open],
		}));
	},
	map: {
		center: [51.5, 10.5],
		zoom: 6,
	},
	setMap: (map) => {
		set(() => ({
			map,
		}));
	},
	searchPopup: null,
	searchElements: [],
	setSearchPopup: (popup) =>
		set(() => ({
			searchPopup: popup,
		})),
	contextMenu: null,
	setContextMenu: (contextMenu) =>
		set(() => ({
			contextMenu,
		})),
	setSearchElements: (elements) =>
		set(() => ({
			searchElements: elements,
		})),
	toggleSearchElementSelection: (elementId, forceState) => {
		const searchElements = get().searchElements;
		const element = searchElements.find((e) => e.wayID === elementId);
		if (!element) return;
		element.isSelected = forceState ? forceState : !element.isSelected;
		set(() => ({
			searchElements: [...searchElements],
		}));
	},
	aircraftTabs: {},
	setAircraftTab: (aircraftId, tab) =>
		set((state) => ({
			aircraftTabs: {
				...state.aircraftTabs,
				[aircraftId]: tab,
			},
		})),
	missionTabs: {},
	userSettings: {
		settingsAutoCloseMapPopup: false,
	},
	setUserSettings: (settings) =>
		set((state) => ({
			userSettings: {
				...state.userSettings,
				...settings,
			},
		})),
}));
