<script lang="ts">
	import { createEventDispatcher, onMount, setContext, tick } from 'svelte';
	import IconButton from './utils/IconButton.svelte';
	import { selected } from './EntitySelect.svelte';
	import type { BrowseContext } from '../util/browse';
	import { writable } from 'svelte/store';
	import { i18n } from '../i18n';
	import { page } from '$app/stores';
	const dispatch = createEventDispatcher();

	export let address: string | undefined = undefined;
	export let index: number;
	export let only: boolean;
	export let forceDetail = false;
	let shifted = false;
	let key = Math.random();

	let detail = only || forceDetail;
	let detailChanged = false;
	$: if (!detailChanged) detail = only || forceDetail;
	$: if (detailChanged) tick().then(() => dispatch('detail', detail));

	let indexStore = writable(index);
	$: $indexStore = index;

	let addressesStore = writable<string[]>([]);
	$: $addressesStore = $page.params.addresses?.split(',') || [];

	setContext('browse', {
		index: indexStore,
		addresses: addressesStore
	} as BrowseContext);

	onMount(() => {
		// Required to make detail mode detection work in Browse
		dispatch('detail', detail);
	});
	$: if ($selected.length) {
		detail = false;
	}

	function visit() {
		window.open(`/browse/${address}`, '_blank');
	}

	let width = 460;
	if (window.innerWidth < 600) {
		width = window.innerWidth - 6;
	}
	function drag(ev: MouseEvent) {
		const startWidth = width;
		const startX = ev.screenX;

		function onMouseMove(ev: MouseEvent) {
			width = startWidth + (ev.screenX - startX);
			width = width < 300 ? 300 : width;
		}
		function onMouseUp(_: MouseEvent) {
			window.removeEventListener('mousemove', onMouseMove);
			window.removeEventListener('mouseup', onMouseUp);
		}
		window.addEventListener('mousemove', onMouseMove);
		window.addEventListener('mouseup', onMouseUp);
	}

	function reload() {
		key = Math.random();
	}
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
<div class="browse-column" class:detail on:mousemove={(ev) => (shifted = ev.shiftKey)}>
	<div class="view" style="--width: {width}px">
		<header>
			{#if address}
				<IconButton name="link" on:click={() => visit()} disabled={only}>
					{$i18n.t('Detach')}
				</IconButton>
			{/if}
			{#if !forceDetail}
				<IconButton
					name={detail ? 'zoom-out' : 'zoom-in'}
					on:click={() => {
						detail = !detail;
						detailChanged = true;
					}}
					active={detail}
				>
					{$i18n.t('Detail')}
				</IconButton>
			{:else}
				<div class="noop"></div>
			{/if}
			{#if address}
				<IconButton name="intersect" on:click={() => dispatch('combine', address)}>
					{$i18n.t('Combine')}
				</IconButton>
			{/if}
			{#if !shifted}
				<IconButton name="x-circle" on:click={() => dispatch('close')} disabled={only}>
					{$i18n.t('Close')}
				</IconButton>
			{:else}
				<IconButton name="refresh" on:click={() => reload()}>
					{$i18n.t('Reload')}
				</IconButton>
			{/if}
		</header>
		{#key key}
			<slot {detail} />
		{/key}
	</div>
	<div class="resizeHandle" on:mousedown|preventDefault={drag} />
</div>

<style lang="scss">
	@use 'sass:color';
	@use '../styles/colors.scss';

	.browse-column {
		position: relative;
		display: flex;
	}

	.browse-column.detail {
		width: 100%;

		.view {
			@media screen and (min-width: 600px) {
				min-width: 85vw;
				max-width: min(85vw, 1920px);
				margin-left: auto;
				margin-right: auto;
			}
		}
	}

	.view {
		min-width: var(--width);
		max-width: var(--width);

		display: flex;
		flex-direction: column;

		background: var(--background-lighter);
		color: var(--foreground-lighter);
		border: 1px solid var(--foreground-lightest);
		border-radius: 0.5em;
		padding: 1rem;

		z-index: 1;

		// transition: min-width 0.2s, max-width 0.2s;
		// TODO - container has nowhere to scroll, breaking `detail` scroll

		header {
			font-size: 20px;
			position: relative;
			min-height: 1em;

			display: flex;
			justify-content: space-between;

			flex: none;
		}
	}

	.browse-column.image-background {
		--foreground: var(--foreground-lighter);
		--background-lighter: rgba(255, 255, 255, 0.08);
		--color-active: #{color.scale(colors.$orange, $alpha: -75%)};
	}

	.background {
		position: absolute;
		top: 0;
		left: 0;
		width: calc(100% - 0.5rem);
		height: 100%;
		border-radius: 0.5em;
		background-size: cover;
		background-position: center;
		filter: blur(0.5rem) brightness(0.3) contrast(1.1);
		z-index: 0;
	}

	.resizeHandle {
		cursor: ew-resize;
		height: 100%;
		width: 0.5rem;

		@media screen and (max-width: 600px) {
			display: none;
		}
	}
</style>
