<script lang="ts">
	import tooltipPosition from '$stores/tooltipPosition';
	import { fade } from 'svelte/transition';

	export let id: string;
	export let tooltipPin: string | undefined = undefined;
	export let content: string;
	export let disable: boolean = false;

	let tooltipDiv: HTMLDivElement | null = null;
	const offset = 10;
	const tooltipTransitionTime = 200;
	const tooltipHideDelay = 10;
	const debounceDelay = 100;
	const tooltipOpacityTransitionTime = 200;
	let hideTimeout: number | null = null;
	let debounceTimer: number | null = null;
	let opacity = 0;

	const createTooltip = () => {
		if (!tooltipDiv) {
			tooltipDiv = document.createElement('div');

			tooltipDiv.style.position = 'absolute';
			tooltipDiv.style.zIndex = '1000';
			opacity = 0;
			tooltipDiv.style.pointerEvents = 'none';
			tooltipDiv.style.whiteSpace = 'nowrap';
			tooltipDiv.style.zIndex = '1000';

			tooltipDiv.classList.add('card');
			tooltipDiv.classList.add('card-small');
			document.body.appendChild(tooltipDiv);
		}
	};

	const updateTooltipPosition = (x: number, y: number) => {
		if (tooltipDiv) {
			if (tooltipPin) {
				const pinnedElement = document.getElementById(tooltipPin);

				if (pinnedElement) {
					const rect = pinnedElement.getBoundingClientRect();
					const tooltipWidth = tooltipDiv.offsetWidth;
					const tooltipHeight = tooltipDiv.offsetHeight;
					let top = rect.top - tooltipHeight - offset;
					let left = rect.left + rect.width / 2 - tooltipWidth / 2;

					if (left < 0) left = offset;
					if (left + tooltipWidth > window.innerWidth)
						left = window.innerWidth - tooltipWidth - offset;
					if (top < 0) top = rect.top + rect.height + offset;

					tooltipPosition.set({
						x: left,
						y: top + window.scrollY
					});

					return;
				}
			}

			const tooltipWidth = tooltipDiv.offsetWidth;
			const tooltipHeight = tooltipDiv.offsetHeight;
			let top = y - tooltipHeight - offset;
			let left = x - tooltipWidth / 2;

			if (left < 0) left = offset;
			if (left + tooltipWidth > window.innerWidth) left = window.innerWidth - tooltipWidth - offset;
			if (top < 0) top = y + offset;

			tooltipPosition.set({
				x: left,
				y: top
			});
		}
	};

	const showTooltip = (content: string, x: number, y: number) => {
		if (hideTimeout !== null) {
			clearTimeout(hideTimeout);

			hideTimeout = null;
		}

		createTooltip();

		if (tooltipDiv) {
			tooltipDiv.innerHTML = content.replace(/\n/g, '<br>');

			updateTooltipPosition(x, y);
			setTimeout(() => {
				if (tooltipDiv) {
					opacity = 1;
				}
			}, 10);
		}
	};

	const hideTooltip = () => {
		setTimeout(() => {
			if (tooltipDiv) {
				opacity = 0;

				hideTimeout = window.setTimeout(() => {
					if (tooltipDiv) {
						document.body.removeChild(tooltipDiv);
						tooltipDiv = null;
					}
				}, tooltipTransitionTime);
			}
		}, tooltipHideDelay);
	};

	const handleMouseEnter = (event: MouseEvent) => {
		if (disable) return;

		if (hideTimeout !== null) {
			clearTimeout(hideTimeout);

			hideTimeout = null;
		}

		if (!tooltipDiv) showTooltip(content, event.pageX, event.pageY);
	};

	const handleMouseMove = (event: MouseEvent) => {
		if (debounceTimer !== null) clearTimeout(debounceTimer);

		debounceTimer = window.setTimeout(() => {
			if (tooltipDiv && opacity === 1) updateTooltipPosition(event.pageX, event.pageY);
		}, debounceDelay);
	};

	const handleMouseLeave = () => {
		hideTooltip();
	};
</script>

<div
	{id}
	data-tooltippin={tooltipPin}
	on:mouseenter={handleMouseEnter}
	on:mousemove={handleMouseMove}
	on:mouseleave={handleMouseLeave}
	role="tooltip"
>
	<slot />
</div>

{#if tooltipDiv}
	<div
		class="tooltip card card-small"
		style={`left: ${$tooltipPosition.x}px; top: ${$tooltipPosition.y}px; opacity: ${opacity}; --tooltip-opacity-transition-time: ${tooltipOpacityTransitionTime}ms;`}
	>
		{#key content}
			<span
				in:fade={{ duration: tooltipTransitionTime }}
				out:fade={{ duration: tooltipTransitionTime }}
			>
				{@html content}
			</span>
		{/key}
	</div>
{/if}

<style>
	.tooltip {
		position: absolute;
		z-index: 1000;
		transition: opacity var(--tooltip-opacity-transition-time) ease-in-out;
		pointer-events: none;
		white-space: nowrap;
	}
</style>
