import { MissionSdsLog, Station } from "@repo/db";
import { fmsStatusDescription } from "_data/fmsStatusDescription";
import { DisplayLineProps } from "pilot/_components/mrt/Mrt";
import { create } from "zustand";
import { syncTabs } from "zustand-sync-tabs";

interface SetSdsPageParams {
	page: "sds";
	station: Station;
	sdsMessage: MissionSdsLog;
}

interface SetHomePageParams {
	page: "home";
	station: Station;
	fmsStatus: string;
}

interface SetSendingStatusPageParams {
	page: "sending-status";
	station: Station;
}

interface SetNewStatusPageParams {
	page: "new-status";
	station: Station;
}

type SetPageParams =
	| SetHomePageParams
	| SetSendingStatusPageParams
	| SetSdsPageParams
	| SetNewStatusPageParams;

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

	lines: DisplayLineProps[];

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

export const useMrtStore = create<MrtStore>(
	syncTabs(
		(set) => ({
			page: "home",
			pageData: {
				message: "",
			},
			lines: [
				{
					textLeft: "VAR.#",
					textSize: "2",
				},
				{
					textLeft: "No Data",
					textSize: "3",
				},
			],
			setLines: (lines) => set({ lines }),
			setPage: (pageData) => {
				switch (pageData.page) {
					case "home": {
						const { station, fmsStatus } = pageData as SetHomePageParams;
						set({
							page: "home",
							lines: [
								{
									textLeft: `${station?.bosCallsign}`,
									style: { fontWeight: "bold" },
									textSize: "2",
								},
								{ textLeft: "ILS VAR#", textSize: "3" },
								{
									textLeft: fmsStatus,
									style: { fontWeight: "extrabold" },
									textSize: "4",
								},
								{
									textLeft: fmsStatusDescription[fmsStatus],
									textSize: "1",
								},
							],
						});
						break;
					}

					case "sending-status": {
						const { station } = pageData as SetSendingStatusPageParams;
						set({
							page: "sending-status",
							lines: [
								{
									textLeft: `${station?.bosCallsign}`,
									style: { fontWeight: "bold" },
									textSize: "2",
								},
								{ textLeft: "ILS VAR#", textSize: "3" },
								{
									textMid: "sending...",
									style: { fontWeight: "bold" },
									textSize: "4",
								},
								{
									textLeft: "Status wird gesendet...",
									textSize: "1",
								},
							],
						});
						break;
					}
					case "new-status": {
						const { station } = pageData as SetNewStatusPageParams;
						set({
							page: "new-status",
							lines: [
								{
									textLeft: `${station?.bosCallsign}`,
									style: { fontWeight: "bold" },
									textSize: "2",
								},
								{ textLeft: "ILS VAR#", textSize: "3" },
								{
									textLeft: "new status received",
									style: { fontWeight: "bold" },
									textSize: "4",
								},
							],
						});
						break;
					}
					case "sds": {
						const { sdsMessage } = pageData as SetSdsPageParams;
						set({
							page: "sds",
							lines: [
								{
									textLeft: `neue SDS-Nachricht`,
									style: { fontWeight: "bold" },
									textSize: "2",
								},
								{
									textLeft: sdsMessage.data.message,
									style: {},
									textSize: "1",
								},
							],
						});
						break;
					}
					default:
						set({ page: "home" });
						break;
				}
			},
		}),
		{
			name: "mrt-store", // unique name
		},
	),
);
