<script lang="ts">
	import { onMount } from 'svelte';
	import { userIdentity as getUserIdentity } from '$lib/AniList/identity';
	import AnimeList from '$lib/List/Anime/DueAnimeList.svelte';
	import MangaListTemplate from '$lib/List/Manga/MangaListTemplate.svelte';
	import UpcomingAnimeList from '$lib/List/Anime/UpcomingAnimeList.svelte';
	import userIdentity from '$stores/userIdentity';
	import settings from '$stores/settings';
	import ListTitle from '$lib/List/ListTitle.svelte';
	import HeadTitle from '$lib/HeadTitle.svelte';
	import LastActivity from '$lib/LastActivity.svelte';
	import { limitListHeight } from '$lib/Utility/html.js';

	export let data;

	let currentUserIdentity = { name: '', id: -1 };

	onMount(async () => {
		limitListHeight();

		if (data.user !== undefined) {
			if ($userIdentity === '') {
				userIdentity.set(JSON.stringify(await getUserIdentity(data.user)));
			}

			currentUserIdentity = JSON.parse($userIdentity);
			currentUserIdentity.name = currentUserIdentity.name;
		}
	});
</script>

<HeadTitle />

<LastActivity user={data.user} />

<div
	id="list-container"
	style={`column-width: 300px; column-count: ${
		Number(!$settings.disableAnime) +
		Number(!$settings.disableManga) +
		Number(!$settings.disableUpcomingAnime) -
		Number(!$settings.disableUpcomingAnime && !$settings.disableAnime && !$settings.disableManga)
	};`}
>
	{#if data.user === undefined}
		Please log in to view due media.
	{:else}
		{#if !$settings.disableUpcomingAnime}
			<details open={!$settings.displayUpcomingAnimeCollapsed} class="list">
				{#if currentUserIdentity.id != -1}
					<UpcomingAnimeList user={data.user} identity={currentUserIdentity} />
				{:else}
					<ListTitle custom="Upcoming Episodes" />

					<ul><li>Loading user identity ... 50%</li></ul>
				{/if}
			</details>

			{#if !$settings.disableAnime || !$settings.disableManga}
				<p />
			{/if}
		{/if}

		{#if !$settings.disableAnime}
			<details open={!$settings.displayAnimeCollapsed} class="list">
				{#if currentUserIdentity.id != -1}
					<AnimeList user={data.user} identity={currentUserIdentity} />
				{:else}
					<ListTitle anime />

					<ul><li>Loading user identity ... 50%</li></ul>
				{/if}
			</details>

			{#if !$settings.disableManga}
				<p />
			{/if}
		{/if}

		{#if !$settings.disableManga}
			<details open={!$settings.displayMangaCollapsed} class="list">
				{#if currentUserIdentity.id != -1}
					<MangaListTemplate
						user={data.user}
						identity={currentUserIdentity}
						displayUnresolved={$settings.displayUnresolved}
						due={true}
					/>
				{:else}
					<ListTitle />

					<ul><li>Loading 0% ...</li></ul>
				{/if}
			</details>
		{/if}

		{#if $settings.disableUpcomingAnime && $settings.disableAnime && $settings.disableManga}
			<video src="https://video.twimg.com/tweet_video/Do_eDPnX0AAKV9f.mp4" autoplay loop>
				<track kind="captions" />
			</video>
		{/if}
	{/if}
</div>

<style>
	.list {
		overflow-y: auto;
		break-inside: avoid-column;
		/* max-height: 80vh; */
		overflow-y: auto;
	}
</style>
