<script lang="ts">
	import { updateComics } from '$gql/Mutations';
	import { artistList, characterList, circleList, comicTagList, worldList } from '$gql/Queries';
	import { categories, censorships, directions, languages, layouts, ratings } from '$lib/Enums';
	import { toastFinally } from '$lib/Toasts';
	import { UpdateComicsControls } from '$lib/Update.svelte';
	import Dialog from '$lib/components/Dialog.svelte';
	import LabelledBlock from '$lib/components/LabelledBlock.svelte';
	import Select from '$lib/components/Select.svelte';
	import SubmitButton from '$lib/components/SubmitButton.svelte';
	import { getContextClient } from '@urql/svelte';
	import type { ModalProps } from 'svelte-modals';
	import UpdateModeSelector from './components/UpdateModeSelector.svelte';

	const client = getContextClient();

	interface Props extends ModalProps {
		ids: number[];
	}

	let { ids, ...modal }: Props = $props();

	let tagsQuery = $derived(comicTagList(client));
	let artistsQuery = $derived(artistList(client));
	let charactersQuery = $derived(characterList(client));
	let circlesQuery = $derived(circleList(client));
	let worldsQuery = $derived(worldList(client));

	let tags = $derived($tagsQuery.data?.comicTags.edges);
	let artists = $derived($artistsQuery.data?.artists.edges);
	let characters = $derived($charactersQuery.data?.characters.edges);
	let circles = $derived($circlesQuery.data?.circles.edges);
	let worlds = $derived($worldsQuery.data?.worlds.edges);

	const controls = new UpdateComicsControls();

	function update(event: SubmitEvent) {
		event.preventDefault();

		updateComics(client, { ids, input: controls.input() }).then(modal.close).catch(toastFinally);
	}
</script>

<Dialog title="Edit Comics" {...modal}>
	<form onsubmit={update}>
		<div class="grid-labels">
			<label class="self-center" for="category">Category</label>
			<Select clearable id="category" options={categories} bind:value={controls.category.value} />

			<label class="self-center" for="rating">Rating</label>
			<Select clearable id="rating" options={ratings} bind:value={controls.rating.value} />

			<label class="self-center" for="censor">Censorship</label>
			<Select clearable id="censor" options={censorships} bind:value={controls.censorship.value} />

			<label class="self-center" for="language">Language</label>
			<Select clearable id="language" options={languages} bind:value={controls.language.value} />

			<label class="self-center" for="direction">Direction</label>
			<Select clearable id="direction" options={directions} bind:value={controls.direction.value} />

			<label class="self-center" for="layout">Layout</label>
			<Select clearable id="layout" options={layouts} bind:value={controls.layout.value} />
		</div>

		<LabelledBlock label="Artists">
			{#snippet children({ id })}
				<Select multi {id} options={artists} bind:value={controls.artists.ids} />
			{/snippet}
			{#snippet side()}
				<UpdateModeSelector bind:mode={controls.artists.options.mode} />
			{/snippet}
		</LabelledBlock>
		<LabelledBlock label="Circles">
			{#snippet children({ id })}
				<Select multi {id} options={circles} bind:value={controls.circles.ids} />
			{/snippet}
			{#snippet side()}
				<UpdateModeSelector bind:mode={controls.circles.options.mode} />
			{/snippet}
		</LabelledBlock>
		<LabelledBlock label="Characters">
			{#snippet children({ id })}
				<Select multi {id} options={characters} bind:value={controls.characters.ids} />
			{/snippet}
			{#snippet side()}
				<UpdateModeSelector bind:mode={controls.characters.options.mode} />
			{/snippet}
		</LabelledBlock>
		<LabelledBlock label="Worlds">
			{#snippet children({ id })}
				<Select multi {id} options={worlds} bind:value={controls.worlds.ids} />
			{/snippet}
			{#snippet side()}
				<UpdateModeSelector bind:mode={controls.worlds.options.mode} />
			{/snippet}
		</LabelledBlock>
		<LabelledBlock label="Tags">
			{#snippet children({ id })}
				<Select multi {id} options={tags} bind:value={controls.tags.ids} />
			{/snippet}
			{#snippet side()}
				<UpdateModeSelector bind:mode={controls.tags.options.mode} />
			{/snippet}
		</LabelledBlock>

		<div class="flex justify-end gap-4">
			<SubmitButton pending={controls.pending()} />
		</div>
	</form>
</Dialog>
