<script lang="ts">
	import { createEventDispatcher } from 'svelte';

	export let action;

	const dispatch = createEventDispatcher();

	let dialog: HTMLDialogElement;

	const adjustTime = (minutes: number) => {
		const timestamp = new Date(action.timestamp);
		timestamp.setMinutes(timestamp.getMinutes() + minutes);
		action.timestamp = timestamp.toISOString();
		console.log(action);
		dispatch('update', action);
	};

	const deleteAction = () => {
		dispatch('delete', action);
		dialog.close();
	};

	const openDialog = () => {
		dialog.showModal();
	};

	const closeDialog = () => {
		dialog.close();
	};
</script>

<style lang="scss">
	.button--action {
		margin: 0;
		padding: 0;
		border: none;
		background: none;
		cursor: pointer;
		position: absolute;
		top: 0;
		right: 0;
		width: 100%;
		height: 100%;
	}

	.button--edit-time {
		padding: 0 1em;
		border-radius: 999px;
	}

	.button--close {
		padding: 0 1em;
		border-radius: 999px;
		background-color: #c6c6c6;
		border-color: #c6c6c6;
	}

	dialog {
		width: 50vw;
		height: 50vh;
		border-radius: 3rem;
		border: none;
        z-index: 1;
		&::backdrop {
			background: rgba(0, 0, 0, 0.5);
			backdrop-filter: blur(5px);
		}

		.dialog__content {
			display: flex;
			flex-direction: column;
			height: 100%;
			.dialog__middle {
				flex-grow: 1;
			}
		}
	}
</style>

<button class="button--action" on:click={openDialog}></button>

<span class="badge badge--{action.type}">{action.type}</span> at {new Date(
	action.timestamp
).toLocaleTimeString('en-NL', { hour: '2-digit', minute: '2-digit' })}

<dialog bind:this={dialog}>
	<div class="dialog__content">
		<div class="">
			<h3>
				{action.type} @{new Date(action.timestamp).toLocaleString('en-NL', {
					hour: '2-digit',
					minute: '2-digit'
				})}
			</h3>
		</div>
		<div class="dialog__middle">
			<button class="button--edit-time" on:click={() => adjustTime(-5)}>-5</button>
			<button class="button--edit-time" on:click={() => adjustTime(-1)}>-1</button>
			<button class="button--edit-time" on:click={() => adjustTime(1)}>+1</button>
			<button class="button--edit-time" on:click={() => adjustTime(5)}>+5</button>
			<button class="button--edit-time" on:click={deleteAction}>Delete</button>
		</div>
		<div class=""><button class="button--close" on:click={closeDialog}>Done Editing</button></div>
	</div>
</dialog>