<script lang="ts">
	import MdClose from 'svelte-icons/md/MdClose.svelte';
	import MdEdit from 'svelte-icons/md/MdEdit.svelte';
	import MdDelete from 'svelte-icons/md/MdDelete.svelte';
	import { getContext } from 'svelte';
	import InputWithLabel from '../forms/InputWithLabel.svelte';
	import { useMutation, useQueryClient } from '@sveltestack/svelte-query';
	import { cloneExo, delExo, getExo } from '../../requests/exo.request';
	import type { Exercice } from 'src/types/exo.type';
	import TagContainer from './TagContainer.svelte';
	import PrivacyIndicator from './PrivacyIndicator.svelte';
	import MdContentCopy from 'svelte-icons/md/MdContentCopy.svelte';
	import ModalCard from './ModalCard.svelte';
	import { goto } from '$app/navigation';
	export let exo: Exercice;
	export let edit: Function;
	export let delete_: Function;

	const { close, show } = getContext<{ close: Function; show: Function }>('modal');
	const { alert } = getContext<{ alert: Function }>('alert');
	const { isAuth } = getContext<{ isAuth: boolean }>('auth');

	let name = '';
</script>

{#if exo != null}
	<h1>
		<span class="name">{exo.name}</span>
		{#if exo.is_author && exo.original == null}
			<span
				><PrivacyIndicator color={exo.private == true ? 'red' : 'green'}
					>{exo.private == true ? 'Privé' : 'Public'}</PrivacyIndicator
				></span
			>
		{:else if exo.is_author && exo.original != null}
			<span
				on:click={() => {
					if (exo.original == null) return;

					getExo(exo.original?.id_code).then((r) => {
						goto(`/exercices/${exo.original?.id_code}`);
						show(
							ModalCard,
							{ exo: r },
							() => {
								goto('/exercices/user');
							},
							true
						);
					});
				}}
				on:keyup={() => {}}
			>
				<PrivacyIndicator color="blue"
					>Exercice original de <strong>{exo.original?.author}</strong></PrivacyIndicator
				>
			</span>
		{:else if !exo.is_author && exo.original == null}
			<span>
				<PrivacyIndicator color="blue">Par <strong>{exo.author.username}</strong></PrivacyIndicator>
			</span>
		{/if}
	</h1>
	<InputWithLabel type="text" value={name} label="Nom" />

	<div class="examples">
		<h2>Exemples</h2>
		{#if !!exo.consigne}<p>{exo.consigne}</p>{/if}

		{#each exo.examples.data as e}
			<p>{e.calcul}</p>
		{/each}
	</div>
	<div class="flex-row-center wp-100">
		<button class="primary-btn">Télécharger</button>
	</div>
	<div class="tags" />

	{#if !!isAuth}
		<TagContainer {exo} />
	{/if}
	<div class="icons">
		<div>
			<div class="icon" style:color="black" on:click={() => close()} on:keypress={() => {}}>
				<MdClose />
			</div>
		</div>

		<div>
			{#if !!isAuth}
				{#if exo.is_author}
					<div
						class="icon"
						style:color="red"
						on:click={() => {
							alert({
								title: 'Sur ?',
								description: 'Voulez vous supprimer ? ',
								validate: () => {
									close();
									delete_();
								}
							});
						}}
						on:keypress={() => {}}
					>
						<MdDelete />
					</div>
					<div class="icon" style:color="green" on:click={() => edit()} on:keypress={() => {}}>
						<MdEdit />
					</div>
				{:else}
					<div
						class="icon"
						style:color="#f0f0f0"
						on:click={() => {
							cloneExo(exo.id_code).then((r) => {
								goto('/exercices/' + r.id_code);
								show(
									ModalCard,
									{ exo: r },
									() => {
										goto('/exercices/user');
									},
									true
								);
							});
						}}
						on:keypress={() => {}}
						title="Copier l'exercice pour pouvoir le modifier"
					>
						<MdContentCopy />
					</div>
				{/if}
			{/if}
		</div>
	</div>
{/if}

<style lang="scss">
	.icon {
		width: 25px;
		height: 25px;
		transition: 0.3s;
		cursor: pointer;
		opacity: 0.7;
		transform: scale(0.9);
		&:hover {
			transform: scale(1);
			opacity: 1;
		}
	}

	.icons {
		position: absolute;
		top: 0;
		margin: 30px;
		gap: 10px;
		display: flex;
		justify-content: space-between;
		right: 0;
		left: 0;
		div {
			display: flex;
		}
	}
	h1 {
		font-size: 1.5em;
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		span.name {
			overflow: hidden;
			word-wrap: break-word;
			
		}
		span:not(.name) {
			position: relative;
			height: min-content;
			font-size: 18px;
			cursor: pointer;
		}
	}

	.examples {
		h2 {
			font-size: 1em;
			font-weight: 800;
			margin-bottom: 5px;
		}
		p {
			margin: 5px 0;
			margin-left: 30px;
			color: gray;
			font-weight: 400;
		}
	}
</style>
