<script lang="ts">
	import { page } from '$app/state';

	import stopErr from '$lib/assets/img/stop-error.svg';
	import noResult from '$lib/assets/img/no-data.svg';
	import Button from '$lib/components/ui/form-elements/Button.svelte';
	import { IconAntennaBarsOff } from '@tabler/icons-svelte-runes';
</script>

<!-- Will need to check error type, using the upstream code combined with response code -->

<!-- {#if page.error}
	{JSON.stringify(page.error)}
	STATUS: {page.status}
{/if} -->

<div class="error-wrapper">
	{#if page.error?.code}
		{#if page.error.code === 'NOT_FOUND'}
			<img class="err-img" src={noResult} alt="" role="presentation" width="200" height="200" />
		{:else if page.error.code === 'RATE_LIMIT'}
			<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
		{:else if page.error.code === 'VALIDATION'}
			<img class="err-img" src={noResult} alt="" role="presentation" width="150" height="210" />
		{:else if page.error.code === 'AUTH'}
			<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
		{:else if page.error.code === 'SERVER'}
			<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
		{:else if page.error.code === 'NETWORK_DISCONNECTED'}
			<div class="err-img gentle-flash">
				<IconAntennaBarsOff size={120} />
			</div>
		{:else}
			<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
		{/if}
	{:else}
		<!-- STOP Error image -->
		<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
	{/if}
	<h2 class="label">{page.status}</h2>

	<p class="error-message">
		{page.error?.message ?? 'An unexpected derailment occurred.'}
	</p>

	{#if page.error?.code}
		<div class="debug-info">
			<code>Ref: {page.error.code}</code>
		</div>
	{/if}

	{#if page.error?.code === 'NETWORK_DISCONNECTED'}
		<Button onclick={() => window.location.reload()} color={'accent'}>Retry</Button>
	{:else if page.error?.code === 'NOT_FOUND'}
		<Button onclick={() => history.back()} color={'accent'}>Go Back</Button>
	{:else}
		<Button href={'/'} color={'accent'}>Return to Home</Button>
	{/if}
</div>

<style>
	.error-wrapper {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: calc(100vh - 140px);
		padding: 0;
		text-align: center;
		font-family: 'URW Gothic', sans-serif;
	}

	.err-img {
		height: 40%;
		width: auto;
		max-width: 180px;
		max-height: 252px;
	}

	@media (max-height: 600px), (max-width: 270px) {
		.err-img {
			display: none;
		}
	}

	@keyframes gentle-flash {
		100% {
			opacity: 1;
		}
		50% {
			opacity: 0.25;
		}
	}

	.gentle-flash {
		animation: gentle-flash ease-in-out 2s infinite;
	}

	.label {
		color: #ff4444;
		letter-spacing: 0.2rem;
		margin-bottom: 0px;
	}

	.error-message {
		font-size: 1.1rem;
		color: var(--color-title);
		max-width: 300px;
		margin-top: 5px;
		margin-bottom: 20px;
	}

	.debug-info {
		background: rgba(255, 255, 255, 0.05);
		padding: 5px 15px;
		margin-top: 0;
		margin-bottom: 20px;
		border-radius: 4px;
		font-size: 0.8rem;
		opacity: 0.6;
	}
</style>
