<template>
	<Head>
		<Title>d[any] - Modification</Title>
	</Head>
	<div class="flex flex-row w-full max-w-full h-full max-h-full xl:-mx-12 xl:-my-8 lg:-mx-8 lg:-my-6 -mx-6 -my-3" style="--sidebar-width: 300px">
		<div class="bg-light-0 dark:bg-dark-0 w-[var(--sidebar-width)] border-r border-light-30 dark:border-dark-30 flex flex-col gap-2">
			<NuxtLink class="flex flex-row items-center justify-center group gap-2 my-2" aria-label="Accueil" :to="{ name: 'index', force: true }">
				<Avatar src="/logo.dark.svg" class="dark:block hidden" />
				<Avatar src="/logo.light.svg" class="block dark:hidden" />
				<span class="text-xl font-semibold group-hover:text-light-70 dark:group-hover:text-dark-70">d[any]</span>
			</NuxtLink>
			<div class="flex-1 px-2 max-w-full max-h-full overflow-y-auto overflow-x-hidden" ref="tree"></div>
			<div class="flex flex-col my-4 items-center justify-center gap-1 text-xs text-light-60 dark:text-dark-60">
				<NuxtLink class="hover:underline" :to="{ name: 'legal' }">Mentions légales</NuxtLink>
				<NuxtLink class="hover:underline" :to="{ name: 'usage' }">Conditions d'utilisations</NuxtLink>
				Copyright Peaceultime - 2025
			</div>
		</div>
		<div class="flex flex-col flex-1 h-full w-[calc(100vw-var(--sidebar-width))]">
			<div class="flex flex-row border-b border-light-30 dark:border-dark-30 justify-between px-8">
				<div class="flex flex-row gap-16 items-center">
					<NavigationMenuRoot class="relative">
						<NavigationMenuList class="flex items-center gap-8 max-md:hidden">
							<NavigationMenuItem>
								<NavigationMenuTrigger>
									<NuxtLink :href="{ name: 'character' }" class="flex flex-row gap-2 items-center border-b-2 border-transparent hover:border-accent-blue py-4 select-none" active-class="text-accent-blue"><span class="px-3 flex-1 truncate">Personnages</span><Icon icon="radix-icons:caret-down" /></NuxtLink>
								</NavigationMenuTrigger>
								<NavigationMenuContent class="absolute top-0 w-full sm:w-auto bg-light-0 dark:bg-dark-0 border border-light-30 dark:border-dark-30 py-2 z-20 flex flex-col">
									<NuxtLink :href="{ name: 'character-list' }" class="hover:bg-light-30 hover:dark:bg-dark-30 px-4 py-2 select-none" active-class="text-accent-blue"><span class="flex-1 truncate">Personnages publics</span></NuxtLink>
									<NuxtLink :href="{ name: 'character-id-edit', params: { id: 'new' } }" class="hover:bg-light-30 hover:dark:bg-dark-30 px-4 py-2 select-none" active-class="text-accent-blue"><span class="flex-1 truncate">Nouveau personnage</span></NuxtLink>
								</NavigationMenuContent>
							</NavigationMenuItem>
						</NavigationMenuList>
						<div class="absolute top-full left-0 flex w-full justify-center">
							<NavigationMenuViewport class="h-[var(--radix-navigation-menu-viewport-height)] w-full origin-[top_center] flex justify-center overflow-hidden sm:w-[var(--radix-navigation-menu-viewport-width)]" />
						</div>
					</NavigationMenuRoot>
					<NuxtLink :href="{ name: 'character' }" class="flex flex-row gap-2 items-center border-b-2 border-transparent hover:border-accent-blue py-4 select-none" active-class="text-accent-blue"><span class="px-3 flex-1 truncate">Campagnes</span></NuxtLink>
				</div>
				<div class="flex flex-row gap-16 items-center">
					<NuxtLink class="text-light-100 dark:text-dark-100 hover:text-light-70 hover:dark:text-dark-70" :to="{ name: 'user-login' }">{{ user!.username }}</NuxtLink>
				</div>
			</div>
			<div class="flex flex-1 flex-row max-h-full overflow-hidden" ref="container"></div>
		</div>
	</div>
</template>

<script setup lang="ts">
import { Content, Editor } from '~~/shared/content';
import { button, loading } from '~~/shared/components';
import { dom, icon } from '~~/shared/dom';
import { modal, tooltip } from '~~/shared/floating';
import { Toaster } from '~~/shared/components';
import { Icon } from '@iconify/vue';
 
definePageMeta({
	rights: ['admin', 'editor'],
	layout: 'null',
});

const { user } = useUserSession();
const tree = useTemplateRef('tree'), container = useTemplateRef('container');
let editor: Editor;

function pull()
{
	Content.pull().then(e => {
		Toaster.add({ type: 'success', content: 'Données mises à jour avec succès.', timer: true, duration: 7500 });
	}).catch(e => {
		Toaster.add({ type: 'success', content: 'Une erreur est survenue durant la récupération des données.', timer: true, duration: 7500 });
		console.error(e);
	});
}
function push()
{
	const { close } = modal([dom('div', { class: 'flex flex-col gap-4 justify-center items-center' }, [ dom('div', { class: 'text-xl', text: 'Mise à jour des données' }), loading('large') ])], { priority: false, closeWhenOutside: true, });
	Content.push().then(e => {
		close();
		Toaster.add({ type: 'success', content: 'Données mises à jour avec succès.', timer: true, duration: 7500 });
	}).catch(e => {
		close();
		Toaster.add({ type: 'success', content: 'Une erreur est survenue durant l\'enregistrement des données.', timer: true, duration: 7500 });
		console.error(e);
	});
}

onMounted(async () => {
	if(tree.value && container.value)
	{ 
		const load = loading('normal');
		tree.value.appendChild(load);

		const content = dom('div', { class: 'flex flex-row justify-start items-center gap-4 p-2' }, [
			tooltip(button(icon('ph:cloud-arrow-down', { height: 16, width: 16 }), pull, 'p-1'), 'Actualiser', 'top'),
			tooltip(button(icon('ph:cloud-arrow-up', { height: 16, width: 16 }), push, 'p-1'), 'Enregistrer', 'top'),
			tooltip(button(icon('radix-icons:reset', { height: 16, width: 16 }), () => editor?.undo(), 'p-1'), 'Annuler', 'top'),
			tooltip(button(icon('radix-icons:reset', { height: 16, width: 16, hFlip: true }), () => editor?.redo(), 'p-1'), 'Rétablir', 'top'),
		])

		tree.value.insertBefore(content, load);
		
		editor = new Editor();

		Content.ready.then(() => tree.value?.replaceChild(editor.tree.container, load));
		container.value.appendChild(editor.container);
	}
});

useShortcuts({
	"Meta-Z": () => editor?.undo(),
	"Meta-Y": () => editor?.redo(),
});

onBeforeUnmount(() => {
	editor?.unmount();
});
</script>