<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';
	import Dialog from '$lib/components/Dialog.svelte';
	import Labelled from '$lib/components/Labelled.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 { closeModal } from 'svelte-modals';
	import UpdateModeSelector from './components/UpdateModeSelector.svelte';

	const client = getContextClient();

	export let isOpen: boolean;
	export let ids: number[];

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

	$: tags = $tagsQuery.data?.comicTags.edges;
	$: artists = $artistsQuery.data?.artists.edges;
	$: characters = $charactersQuery.data?.characters.edges;
	$: circles = $circlesQuery.data?.circles.edges;
	$: worlds = $worldsQuery.data?.worlds.edges;

	const controls = new UpdateComicsControls();

	const update = () => {
		updateComics(client, {
			ids: ids,
			input: controls.toInput()
		})
			.then(closeModal)
			.catch(toastFinally);
	};
</script>

<Dialog {isOpen}>
	<svelte:fragment slot="header">
		<h2>Edit Comics</h2>
	</svelte:fragment>
	<form on:submit|preventDefault={update}>
		<div class="grid-labels">
			<Labelled label="Category" let:id>
				<Select clearable {id} options={categories} bind:value={controls.category.value} />
			</Labelled>
			<Labelled label="Rating" let:id>
				<Select clearable {id} options={ratings} bind:value={controls.rating.value} />
			</Labelled>
			<Labelled label="Censorship" let:id>
				<Select clearable {id} options={censorships} bind:value={controls.censorship.value} />
			</Labelled>
			<Labelled label="Language" let:id>
				<Select clearable {id} options={languages} bind:value={controls.language.value} />
			</Labelled>
			<Labelled label="Direction" let:id>
				<Select clearable {id} options={directions} bind:value={controls.direction.value} />
			</Labelled>
			<Labelled label="Layout" let:id>
				<Select clearable {id} options={layouts} bind:value={controls.layout.value} />
			</Labelled>
		</div>

		<LabelledBlock label="Artists" let:id>
			<Select multi {id} options={artists} bind:value={controls.artists.ids} />
			<UpdateModeSelector bind:mode={controls.artists.options.mode} slot="controls" />
		</LabelledBlock>
		<LabelledBlock label="Circles" let:id>
			<Select multi {id} options={circles} bind:value={controls.circles.ids} />
			<UpdateModeSelector bind:mode={controls.circles.options.mode} slot="controls" />
		</LabelledBlock>
		<LabelledBlock label="Characters" let:id>
			<Select multi {id} options={characters} bind:value={controls.characters.ids} />
			<UpdateModeSelector bind:mode={controls.characters.options.mode} slot="controls" />
		</LabelledBlock>
		<LabelledBlock label="Worlds" let:id>
			<Select multi {id} options={worlds} bind:value={controls.worlds.ids} />
			<UpdateModeSelector bind:mode={controls.worlds.options.mode} slot="controls" />
		</LabelledBlock>
		<LabelledBlock label="Tags" let:id>
			<Select multi {id} options={tags} bind:value={controls.tags.ids} />
			<UpdateModeSelector bind:mode={controls.tags.options.mode} slot="controls" />
		</LabelledBlock>

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