<script lang="ts">
	import { onMount, onDestroy } from "svelte";

	let timeUntilNext = $state("");
	let newVerseReady = $state(false);
	let showEncouragement = $state(false);
	let intervalId: number | null = null;
	let targetTime = 0;

	function initTarget() {
		const target = new Date();
		target.setHours(0, 0, 0, 0);
		if (Date.now() >= target.getTime()) {
			target.setDate(target.getDate() + 1);
		}
		targetTime = target.getTime();
	}

	function updateTimer() {
		const diff = targetTime - Date.now();

		if (diff <= 0) {
			newVerseReady = true;
			timeUntilNext = "";
			if (intervalId) {
				clearInterval(intervalId);
				intervalId = null;
			}
			return;
		}

		const hours = Math.floor(diff / (1000 * 60 * 60));
		const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
		const seconds = Math.floor((diff % (1000 * 60)) / 1000);

		timeUntilNext = `${hours.toString().padStart(2, "0")}h ${minutes
			.toString()
			.padStart(2, "0")}m ${seconds.toString().padStart(2, "0")}s`;
	}

	onMount(() => {
		initTarget();
		updateTimer();
		intervalId = window.setInterval(updateTimer, 1000);

		const winCount = Object.keys(localStorage).filter(
			(k) =>
				k.startsWith("bibdle-win-tracked-") &&
				localStorage.getItem(k) === "true",
		).length;
		showEncouragement = winCount < 3;
	});

	onDestroy(() => {
		if (intervalId) {
			clearInterval(intervalId);
		}
	});
</script>

<div class="w-full flex flex-col flex-1">
	<div
		class="flex flex-col items-center justify-center bg-white/50 dark:bg-black/30 backdrop-blur-sm px-8 py-4 rounded-2xl border border-white/50 dark:border-white/10 shadow-sm w-full flex-1"
	>
		{#if newVerseReady}
			<p
				class="text-xs uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400 font-bold mb-2"
			>
				Next Verse In
			</p>
			<p class="text-4xl font-triodion font-black text-gray-800">Now</p>
			<p
				class="text-xs uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400 font-bold mt-2"
			>
				(refresh page to see the new verse)
			</p>
		{:else}
			<p
				class="text-xs uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400 font-bold mb-2"
			>
				Next Verse In
			</p>
			<p
				class="text-4xl font-triodion font-black text-gray-800 dark:text-gray-100 tabular-nums whitespace-nowrap"
			>
				{timeUntilNext}
			</p>
			{#if showEncouragement}
				<p
					class="text-xs text-center uppercase tracking-[0.2em] text-gray-500 dark:text-gray-400 font-bold px-4 mt-3"
				>
					Come back tomorrow for a new verse!
				</p>
			{/if}
		{/if}
	</div>
</div>
