import type { alertEventData } from "web/alerts/types";
import type { serverInstruction } from "web/serverTypes";
import AlertManager from "./AlertManager";
// @ts-ignore
import "@fontsource/jersey-15";
import TTSManager from "./TTSManager";

const wsAddress = `ws${location.protocol === "https:" ? "s" : ""}://${location.host}`;

const socket = new WebSocket(wsAddress);

socket.onopen = () => {
	const instruction: serverInstruction = {
		type: "subscribe",
		target: "alerts",
	};
	socket.send(JSON.stringify(instruction));
};

socket.onmessage = (event) => {
	const data: alertEventData = JSON.parse(event.data);
	switch (data.function) {
		case "playAlert":
			AlertManager.queueAlert(data.alert);
			break;
		case "playTTS":
			TTSManager.queueTTS(data.tts);
			break;
		case "cancelTTS":
			TTSManager.endTTS();
			break;
		case "serverNotification":
			console.log(data.message);
			break;
	}
};
