import { usePilotConnectionStore } from "_store/pilot/connectionStore";
import { useDmeStore } from "_store/pilot/dmeStore";

import { useSession } from "next-auth/react";
export const useButtons = () => {
	const { page, setPage } = useDmeStore((state) => state);
	const user = useSession().data?.user;
	const station = usePilotConnectionStore((state) => state.selectedStation);

	const handleButton = (button: "main" | "menu" | "left" | "right") => () => {
		switch (button) {
			case "main":
				if (page === "mission" || page === "new-mission") {
					setPage({ page: "acknowledge" });
				}
				break;
			case "menu":
				if (page === "mission" || page === "new-mission") {
					setPage({ page: "acknowledge" });
					if (station && user) setPage({ page: "home", station, user });
					break;
				}
				if (station && user) {
					setPage({ page: "home", station, user });
				} else {
					setPage({ page: "error", error: "No station or user found" });
				}
				break;
			default:
				setPage({ page: "error", error: "Button now allowed" });

				break;
		}
	};

	return { handleButton };
};
