<script lang="ts">
	import { exhibit, artists, type Artist } from '$lib/artists';
	import { precacheState } from '$lib/precache.svelte';
	import { fontsReady } from '$lib/fonts.svelte';

	const pre = $derived(precacheState());

	function artistYears(artist: Artist): string {
		const years = new Set<string>();
		for (const show of artist.shows) {
			const m = show.caption.match(/\b(19|20)\d{2}\b/);
			if (m) years.add(m[0]);
		}
		return [...years].sort().join(', ');
	}
</script>

<header class="hero">
	<div class="marquee-panel">
		{#key fontsReady()}
			<svg class="bulbs" width="100%" height="100%" aria-hidden="true"><rect pathLength="120" /></svg>
		{/key}
		<h1 class="title-marquee">{exhibit.title}</h1>
		{#if exhibit.description}
			{#each exhibit.description.split('\n').filter(Boolean) as line}
				<p class="marquee-body">{line}</p>
			{/each}
		{/if}
	</div>
</header>

{#if pre.state === 'running' || (pre.state === 'complete' && pre.failed > 0)}
	<div class="precache" role="status" aria-live="polite">
		{#if pre.state === 'running'}
			<span class="dot" aria-hidden="true"></span>
			<span class="text">
				Caching exhibit for offline listening… {pre.done} / {pre.total}
				{#if pre.failed > 0}({pre.failed} retrying){/if}
			</span>
			<div class="bar" aria-hidden="true">
				<div class="bar-fill" style="width: {pre.total ? (pre.done / pre.total) * 100 : 0}%"></div>
			</div>
		{:else}
			<span class="text">
				{pre.total - pre.failed} of {pre.total} files cached. {pre.failed}
				failed to download — they will stream over the network when played.
			</span>
		{/if}
	</div>
{/if}

<main class="grid" aria-label="Artists">
	{#each artists as artist (artist.id)}
		{@const cover = artist.images[0]}
		<a
			class="tile"
			href="/artist/{artist.id}"
			data-sveltekit-preload-data="hover"
			aria-label={artist.name}
		>
			<div class="tile-image">
				{#if cover}
					<img src={cover} alt="" loading="lazy" />
				{:else}
					<div class="tile-image-placeholder" aria-hidden="true"></div>
				{/if}
			</div>
			<div class="tile-meta">
				<span class="tile-name">{artist.name}</span>
				<span class="tile-shows">{artistYears(artist)}</span>
			</div>
		</a>
	{/each}
</main>


<style>
	.hero {
		padding: 1.5rem 1rem 1.5rem;
		text-align: center;
	}
	.precache {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 0.6rem;
		margin: 0 auto 0.75rem;
		padding: 0.6rem 1rem;
		max-width: 32rem;
		font-size: 0.9rem;
		color: var(--ink-on-callout);
		background: var(--bg-callout);
		border-radius: var(--radius);
		box-shadow: var(--shadow);
	}
	.precache .text {
		flex: 1 1 auto;
	}
	.precache .bar {
		flex: 1 0 100%;
		height: 4px;
		background: rgba(255, 255, 255, 0.12);
		border-radius: 2px;
		overflow: hidden;
	}
	.precache .bar-fill {
		height: 100%;
		background: var(--highlight);
		transition: width 0.2s linear;
	}
	.precache .dot {
		width: 8px;
		height: 8px;
		border-radius: 50%;
		background: var(--highlight);
		animation: pulse 1.4s ease-in-out infinite;
		flex-shrink: 0;
	}
	@keyframes pulse {
		0%, 100% { opacity: 0.3; transform: scale(0.8); }
		50% { opacity: 1; transform: scale(1.1); }
	}
	@media (prefers-reduced-motion: reduce) {
		.precache .dot { animation: none; opacity: 0.7; }
	}
	.hero :global(.title-marquee) {
		font-size: clamp(2.25rem, 9vw, 4rem);
	}
	.grid {
		display: grid;
		gap: 1rem;
		padding: 1rem;
		grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
	}
	@media (min-width: 768px) {
		.grid {
			gap: 1.25rem;
			padding: 1.25rem 2rem 3rem;
			grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
		}
	}
	.tile {
		display: flex;
		flex-direction: column;
		gap: 0.75rem;
		color: var(--ink);
		border-radius: var(--radius-lg);
		background: var(--bg-card);
		box-shadow: var(--shadow);
		overflow: hidden;
		transition: transform 0.15s ease;
	}
	.tile:active {
		transform: scale(0.98);
	}
	.tile-image {
		position: relative;
		aspect-ratio: 4 / 3;
		background: var(--rule);
		overflow: hidden;
	}
	.tile-image img {
		width: 100%;
		height: 100%;
		object-fit: cover;
		object-position: center top;
	}
	.tile-image-placeholder {
		width: 100%;
		height: 100%;
		background:
			repeating-linear-gradient(
				135deg,
				transparent 0,
				transparent 18px,
				rgba(75, 108, 100, 0.08) 18px,
				rgba(75, 108, 100, 0.08) 19px
			),
			var(--rule);
	}
	.tile-meta {
		display: flex;
		flex-direction: column;
		gap: 0.15rem;
		padding: 0.25rem 1rem 1rem;
	}
	.tile-name {
		font-family: var(--font-marquee);
		font-weight: 400;
		font-size: 1.35rem;
		line-height: 1.05;
		letter-spacing: 0.04em;
		text-transform: uppercase;
		color: #111111;
	}
	.tile-shows {
		font-family: var(--font-marquee);
		font-size: 0.9rem;
		line-height: 1.1;
		letter-spacing: 0.08em;
		text-transform: uppercase;
		color: #111111;
		font-variant-numeric: tabular-nums;
	}
</style>
