<script lang="ts">
	import userIdentity from '$stores/identity';
	import { onMount } from 'svelte';
	import type { AniListAuthorisation } from '$lib/Data/AniList/identity';
	import { lastActivityDate } from '../Data/AniList/activity';
	import settings from '$stores/settings';

	export let user: AniListAuthorisation;

	let lastActivityWasToday = true;

	onMount(async () => {
		if (user !== undefined && !$settings.displayDisableLastActivityWarning) {
			lastActivityWasToday =
				(await lastActivityDate($userIdentity, user)).date.toDateString() >=
				new Date().toDateString();

			if (!lastActivityWasToday) {
				if ($settings.displayLimitListHeight) {
					document.querySelectorAll('.list').forEach((list) => {
						(list as HTMLElement).style.maxHeight = `calc((100vh - ${
							document.querySelector('#list-container')?.getBoundingClientRect().top
						}px) - 5rem)`;
					});
				}
			}
		}
	});

	const timeLeftToday = () => {
		const now = new Date();
		const currentHour = now.getHours();
		const currentMinute = now.getMinutes();
		const hoursLeft = 24 - currentHour;
		let minutesLeft = 0;
		let timeLeft = '';

		if (hoursLeft > 0) {
			minutesLeft = hoursLeft * 60 - currentMinute;
		} else {
			minutesLeft = 24 * 60 - (currentHour * 60 + currentMinute);
		}

		if (minutesLeft > 60) {
			timeLeft = `${Math.round(minutesLeft / 60)} hours`;
		} else {
			timeLeft = `${minutesLeft} minutes`;
		}

		return timeLeft;
	};
</script>

{#if !lastActivityWasToday}
	<blockquote>
		You don't have any new activity statuses from the past day! Create one within {timeLeftToday()}
		to keep your streak!
	</blockquote>
{/if}
