<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Pomodoro Timer</title>
	<style>
		button {
			padding: 10px 20px;
			margin: 5px;
			font-size: 16px;
		}

		#timer {
			font-size: 24px;
			margin-top: 20px;
		}
	</style>
</head>

<body>
	<h1>Pomodoro Timer</h1>
	<div id="timer">Connecting to server...</div>

	<!-- Buttons to start, pause/resume, and stop the timer -->
	<button id="startButton">Start</button>
	<button id="pauseResumeButton" style="display: none;">Pause</button>
	<button id="resetButton" style="display: none;">Reset</button>

	<script>
		var ws = new WebSocket("ws://localhost:8080/ws");
		var isPaused = false; // Track if the timer is paused

		ws.onopen = function () {
			document.getElementById("timer").innerText = "Connected to server.";
		};

		// Handle incoming messages and update the timer display
		ws.onmessage = function (event) {
			var data = JSON.parse(event.data);
			var mode = data.mode;
			var session = data.session;
			var totalSession = data.total_sessions;
			var timeLeft = data.time_left;

			document.getElementById("timer").innerText =
				mode + " Session " + session + "/" + totalSession + ": " + formatTime(timeLeft);
		};

		ws.onclose = function () {
			document.getElementById("timer").innerText = "Connection closed.";
		};

		// Format time in MM:SS
		function formatTime(seconds) {
			var minutes = Math.floor(seconds / 60);
			var remainingSeconds = seconds % 60;
			return minutes.toString().padStart(2, '0') + ":" + remainingSeconds.toString().padStart(2, '0');
		}

		// Start Button Click Event
		document.getElementById("startButton").addEventListener("click", function () {
			// ws.send(JSON.stringify({command: "start"}));
			ws.send(JSON.stringify({command: "start", config: {work: 10, shortBreak: 5, longBreak: 10, sessions: 2}}));

			// Hide start button and show pause/resume and stop buttons
			document.getElementById("startButton").style.display = "none";
			document.getElementById("pauseResumeButton").style.display = "inline-block";
			document.getElementById("resetButton").style.display = "inline-block";

			// Set the pause/resume button to show "Pause" initially
			isPaused = false;
			document.getElementById("pauseResumeButton").innerText = "Pause";
		});

		// Pause/Resume Button Click Event
		document.getElementById("pauseResumeButton").addEventListener("click", function () {
			if (isPaused) {
				// If paused, send resume command and update button text
				ws.send(JSON.stringify({command: "resume"}));
				document.getElementById("pauseResumeButton").innerText = "Pause";
				isPaused = false;
			} else {
				// If running, send pause command and update button text
				ws.send(JSON.stringify({command: "pause"}));
				document.getElementById("pauseResumeButton").innerText = "Resume";
				isPaused = true;
			}
		});

		// Reset Button Click Event
		document.getElementById("resetButton").addEventListener("click", function () {
			ws.send(JSON.stringify({command: "stop"}));

			// Reset buttons after stopping
			document.getElementById("startButton").style.display = "inline-block";
			document.getElementById("pauseResumeButton").style.display = "none";
			document.getElementById("resetButton").style.display = "none";
		});
	</script>
</body>

</html>