import { Mission, Station, User } from "@repo/db";
import { DisplayLineProps } from "pilot/_components/dme/Dme";
import { create } from "zustand";
import { syncTabs } from "zustand-sync-tabs";

interface SetHomePageParams {
	page: "home";
	user: User;
	station: Station;
}

interface SetErrorPageParams {
	page: "error";
	error: string;
}

interface SetNewMissionPageParams {
	page: "new-mission";
}

interface SetMissionPageParams {
	page: "mission";
	mission: Mission & { Stations: Station[] };
}

interface SetAcknowledgePageParams {
	page: "acknowledge";
}

type SetPageParams =
	| SetHomePageParams
	| SetNewMissionPageParams
	| SetMissionPageParams
	| SetErrorPageParams
	| SetAcknowledgePageParams;

interface MrtStore {
	page: SetPageParams["page"];

	lines: DisplayLineProps[];

	setPage: (pageData: SetPageParams) => void;
	setLines: (lines: MrtStore["lines"]) => void;
}

let interval: NodeJS.Timeout | null = null;

export const useDmeStore = create<MrtStore>(
	syncTabs(
		(set) => ({
			page: "home",
			pageData: {
				message: "",
			},
			lines: [
				{
					textLeft: "",
				},
				{
					textMid: "VAR . DME# No Data",
					textSize: "2",
				},
				{
					textLeft: "",
				},
			],
			setLines: (lines) => set({ lines }),
			setPage: (pageData) => {
				if (interval) clearInterval(interval);
				switch (pageData.page) {
					case "home": {
						const setHomePage = () =>
							set({
								page: "home",
								lines: [
									{
										textMid: pageData.station.bosCallsign
											? `${pageData.station.bosCallsign}`
											: "no Data",
										style: { fontWeight: "bold" },
									},
									{ textMid: "⠀" },
									{
										textMid: new Date().toLocaleDateString("de-DE", {
											year: "numeric",
											month: "2-digit",
											day: "2-digit",
										}),
									},
									{
										textMid: new Date().toLocaleTimeString(),
										style: { fontWeight: "bold" },
									},
									{ textMid: "⠀" },
									{
										textMid: `${pageData.user.lastname} ${pageData.user.firstname}`,
									},
									{ textMid: "⠀" },
								],
							});
						setHomePage();

						interval = setInterval(() => {
							setHomePage();
						}, 1000);
						break;
					}

					case "new-mission": {
						set({
							page: "new-mission",
							lines: [
								{ textMid: "⠀" },
								{
									textMid: "new mission received",
									style: { fontWeight: "bold" },
								},
								{ textMid: "⠀" },
							],
						});
						break;
					}
					case "mission": {
						set({
							page: "mission",
							lines: [
								{
									textLeft: `${pageData.mission.missionKeywordAbbreviation}`,
									textRight: pageData.mission.Stations.map((s) => s.bosCallsignShort).join(","),
									style: { fontWeight: "bold" },
								},
								{
									textMid: `${pageData.mission.missionKeywordName}`,
									style: { fontWeight: "bold" },
								},
								{ textLeft: `${pageData.mission.addressStreet}` },
								{
									textLeft: `${pageData.mission.addressZip} ${pageData.mission.addressCity}`,
								},
								{
									textMid: "Patienteninfos:",
									style: { fontWeight: "bold" },
								},
								{
									textLeft: pageData.mission.missionPatientInfo || "keine Daten",
								},
								{
									textMid: "Weitere Infos:",
									style: { fontWeight: "bold" },
								},
								{
									textLeft: pageData.mission.missionAdditionalInfo || "keine Daten",
								},
							],
						});
						break;
					}
					case "error": {
						set({
							page: "error",
							lines: [
								{ textMid: "Fehler:" },
								{
									textMid: pageData.error,
									style: { fontWeight: "bold" },
								},
								{ textMid: "⠀" },
							],
						});
						break;
					}
					case "acknowledge": {
						set({
							page: "acknowledge",
							lines: [
								{ textMid: "⠀" },
								{
									textMid: "Einsatz angenommen",
									style: { fontWeight: "bold" },
								},
								{ textMid: "⠀" },
							],
						});
						break;
					}
					default:
						set({
							page: "error",
							lines: [
								{ textMid: "Fehler:" },
								{
									textMid: `Unbekannte Seite`,
									style: { fontWeight: "bold" },
								},
								{ textMid: "⠀" },
							],
						});
						break;
				}
			},
		}),
		{
			name: "dme-store", // unique name
		},
	),
);
