<template>
	<div class="mk-media-banner" @click.stop>
		<div
			v-if="media.isSensitive && hide"
			class="sensitive"
			@click="hide = false"
		>
			<span class="icon"><i class="ph-warning ph-bold ph-lg"></i></span>
			<b>{{ i18n.ts.sensitive }}</b>
			<span>{{ i18n.ts.clickToShow }}</span>
		</div>
		<div
			v-else-if="
				media.type.startsWith('audio') && media.type !== 'audio/midi'
			"
			class="audio"
		>
			<VuePlyr
				:options="{
					controls: audioControls,
					disableContextMenu: !media.allowDownload,
				}"
			>
				<audio
					ref="audioEl"
					class="audio"
					:src="media.url"
					:title="media.name"
					controls
					:controlsList="media.allowDownload ? undefined : 'nodownload'"
					preload="metadata"
					@contextmenu="onMediaContextmenu"
					@volumechange="volumechange"
				/>
			</VuePlyr>
		</div>
		<a
			v-else-if="media.allowDownload"
			class="download"
			:href="media.url"
			:title="media.name"
			:download="media.name"
		>
			<span class="icon"
				><i class="ph-download-simple ph-bold ph-lg"></i
			></span>
			<b>{{ media.name }}</b>
		</a>
		<div v-else class="download disabled" :title="media.name">
			<span class="icon"
				><i class="ph-lock-simple ph-bold ph-lg"></i
			></span>
			<b>{{ media.name }}</b>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue';
import VuePlyr from "./VuePlyr.vue";
import type * as misskey from "iceshrimp-sdk";
import { ColdDeviceStorage } from "@/store";
import { i18n } from "@/i18n";

const props = withDefaults(
	defineProps<{
		media: misskey.entities.DriveFile;
	}>(),
	{},
);

const audioEl = ref<HTMLAudioElement | null>();
let hide = ref(true);

const audioControls = computed(() => [
	"play-large",
	"play",
	"progress",
	"current-time",
	"mute",
	"volume",
	...(props.media.allowDownload ? ["download"] : []),
]);

function volumechange() {
	if (audioEl.value) ColdDeviceStorage.set("mediaVolume", audioEl.value.volume);
}

function onMediaContextmenu(ev: MouseEvent) {
	if (!props.media.allowDownload) ev.preventDefault();
	ev.stopPropagation();
}

onMounted(() => {
	if (audioEl.value) audioEl.value.volume = ColdDeviceStorage.get("mediaVolume");
});
</script>

<style lang="scss" scoped>
.mk-media-banner {
	width: 100%;
	border-radius: 4px;
	margin-top: 4px;
	overflow: hidden;
	--plyr-color-main: var(--accent);
	--plyr-audio-controls-background: var(--panelHighlight);
	--plyr-audio-controls-background-hover: var(--accentedBg);
	--plyr-audio-control-color: var(--navFg);

	> .download,
	> .sensitive {
		display: flex;
		align-items: center;
		font-size: 12px;
		padding: 8px 12px;
		white-space: nowrap;

		> * {
			display: block;
		}

		> b {
			overflow: hidden;
			text-overflow: ellipsis;
		}

		> *:not(:last-child) {
			margin-right: 0.2em;
		}

		> .icon {
			font-size: 1.6em;
		}
	}

	> .download {
		background: var(--noteAttachedFile);

		&.disabled {
			cursor: default;
			opacity: 0.75;
		}
	}

	> .sensitive {
		background: #111;
		color: #fff;
	}

	> .audio {
		.audio {
			display: block;
			width: 100%;
		}
	}
}
</style>
