<template>
	<transition :name="$store.state.animation ? 'stream-disconnected' : ''">
		<div v-if="hasDisconnected" class="nsbbhtug _acrylic">
			<button class="close _button" type="button" aria-label="閉じる" @click="close">
				<i class="ph-x ph-bold ph-lg"></i>
			</button>
			<div class="message">接続が切れました。</div>
			<div class="command">
				<button class="_button reload" type="button" @click="reload">
					<i class="ph-arrow-clockwise ph-bold ph-lg"></i>
					<span>リロードする</span>
				</button>
			</div>
		</div>
	</transition>
</template>

<script lang="ts" setup>
import { onUnmounted, ref } from 'vue';
import { stream } from "@/stream";

let hasDisconnected = ref(false);

function onDisconnected() {
	hasDisconnected.value = true;
}

function close() {
	hasDisconnected.value = false;
}

function reload() {
	location.reload();
}

stream.on("_disconnected_", onDisconnected);

onUnmounted(() => {
	stream.off("_disconnected_", onDisconnected);
});
</script>

<style lang="scss" scoped>
.nsbbhtug {
	position: fixed;
	z-index: 16385;
	bottom: max(16px, env(safe-area-inset-bottom));
	right: max(16px, env(safe-area-inset-right));
	margin: 0;
	padding: 16px 44px 16px 16px;
	color: var(--fg);
	background: var(--panel);
	border: solid 1px var(--divider);
	border-radius: 8px;
	box-shadow: 0 8px 24px rgb(0 0 0 / 18%);
	width: min(320px, calc(100vw - 32px));

	> .close {
		position: absolute;
		top: 8px;
		right: 8px;
		display: grid;
		place-items: center;
		width: 28px;
		height: 28px;
		border-radius: 6px;
		color: var(--fgTransparentWeak);

		&:hover {
			color: var(--fg);
			background: var(--buttonBg);
		}
	}

	> .message {
		font-weight: 700;
		line-height: 1.5;
	}

	> .command {
		display: flex;
		justify-content: flex-start;
		margin-top: 12px;

		> .reload {
			display: inline-flex;
			align-items: center;
			gap: 6px;
			padding: 8px 12px;
			font-weight: 700;
			color: var(--accent);
			background: var(--buttonBg);
			border-radius: 6px;

			&:hover {
				background: var(--buttonHoverBg);
			}
		}
	}
}

.stream-disconnected-enter-active,
.stream-disconnected-leave-active {
	transition: opacity 0.2s ease, transform 0.2s ease;
}

.stream-disconnected-enter-from,
.stream-disconnected-leave-to {
	opacity: 0;
	transform: translateY(8px);
}
</style>
