<template>
	<div
		class="tile"
		:class="{
			unstashed: !favorited && pageStash && user && pageStash.user.id === user?.id,
			'is-new': scene.isNew,
		}"
	>
		<div class="poster-container">
			<Link
				:href="`/scene/${scene.id}/${scene.slug || ''}`"
				:title="scene.title"
				target="_blank"
				class="poster"
			>
				<img
					v-if="scene.poster"
					:src="getPath(scene.poster, 'thumbnail')"
					:style="{ 'background-image': `url(${getPath(scene.poster, 'lazy')})` }"
					loading="lazy"
					class="thumbnail"
				>

				<Icon
					v-else
					icon="clapboard"
				/>
			</Link>

			<Heart
				domain="scenes"
				:item="scene"
				:show-secondary="false"
				class="light tiled"
				@stashed="(stash) => { favorited = stash.id === currentStash.id ? true : favorited; }"
				@unstashed="(stash) => { favorited = stash.id === currentStash.id ? false : favorited; }"
			/>

			<span
				v-if="scene.isNew"
				:title="`Newly added ${format(scene.createdAt, 'yyyy-MM-dd')}`"
				class="new"
			><Icon icon="star-full" /></span>
		</div>

		<div class="meta">
			<div class="channel">
				<Link
					:href="scene.channel.isIndependent || !scene.network ? `/${scene.channel.type}/${scene.channel.slug}` : `/${scene.network.type}/${scene.network.slug}`"
					class="favicon-link"
				>
					<img
						:src="scene.channel.isIndependent || !scene.network ? `/logos/${scene.channel.slug}/favicon.png` : `/logos/${scene.network.slug}/favicon.png`"
						class="favicon"
					>
				</Link>

				<Link
					:href="`/${scene.channel.type}/${scene.channel.slug}`"
					class="nolink channel-link"
				>{{ scene.channel.name }}</Link>
			</div>

			<time
				:datetime="scene.effectiveDate.toISOString()"
				class="date"
			>{{ format(scene.effectiveDate, 'MMM d, y') }}</time>
		</div>

		<Link
			:href="`/scene/${scene.id}/${scene.slug || ''}`"
			:title="scene.title"
			target="_blank"
			class="row title nolink"
		>{{ scene.title }}</Link>

		<ul
			class="row actors nolist"
			:title="scene.actors.map((actor) => actor.name).join(', ')"
		>
			<li
				v-for="actor in scene.actors"
				:key="`actor-${scene.id}-${actor.id}`"
				class="actor"
			>
				<Link
					:href="`/actor/${actor.id}/${actor.slug}`"
					class="nolink"
				>{{ actor.name }}</Link>
			</li>
		</ul>

		<ul
			class="row tags nolist"
			:title="scene.tags.map((tag) => tag.name).join(', ')"
		>
			<li
				v-for="tag in scene.tags"
				:key="`tag-${scene.id}-${tag.id}`"
				class="tag"
			>
				<Link
					:href="`/tag/${tag.slug}`"
					class="nolink"
				>{{ tag.name }}</Link>
			</li>
		</ul>
	</div>
</template>

<script setup>
import { ref, inject } from 'vue';
import { format } from 'date-fns';

import getPath from '#/src/get-path.js';

import Heart from '#/components/stashes/heart.vue';

const props = defineProps({
	scene: {
		type: Object,
		default: null,
	},
});

const pageContext = inject('pageContext');
const user = pageContext.user;
const pageStash = pageContext.pageProps.stash;
const currentStash = pageStash || user?.primaryStash;

const favorited = ref(props.scene.stashes.some((sceneStash) => sceneStash.id === currentStash.id));
</script>

<style scoped>
.tile {
	width: 100%;
	overflow: hidden;
	background: var(--background-base);
	border-radius: .25rem;
	box-shadow: 0 0 3px var(--shadow-weak-30);

	&:hover {
		box-shadow: 0 0 3px var(--shadow-weak-20);

		:deep(.bookmarks) .icon:not(.favorited):not(:hover) {
			fill: var(--text-light);
		}
	}

	&.unstashed {
		opacity: .5;
	}
}

.poster-container {
	position: relative;
	overflow: hidden;
	font-size: 0;
}

.poster {
	display: flex;
	justify-content: center;
	align-items: center;
	aspect-ratio: 16/9;
	border-radius: .25rem .25rem 0 0;
	overflow: hidden;
	background: var(--background-dark-20);

	.icon {
		width: 3rem;
		height: 3rem;
		fill: var(--shadow-weak-40);
	}
}

.thumbnail {
	width: 100%;
	height: 100%;
	object-fit: cover;
	object-position: 50% 50%;
	background-size: cover;
	background-position: center;
}

.new {
	display: flex;
	flex-direction: row-reverse;
	align-items: center;
	position: absolute;
	bottom: 0;
	right: .5rem;
	padding: .2rem .5rem .1rem .5rem;
	border-radius: .25rem .25rem 0 0;
	background: var(--grey-dark-40);
	color: var(--gold);
	font-size: .7rem;
	font-weight: bold;
	box-shadow: 0 0 3px var(--shadow-weak-20);

	.icon {
		width: .75rem;
		height: .75rem;
		fill: var(--gold);
	}
}

.meta {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: .4rem .5rem;
	border-radius: 0 0 .25rem .25rem;
	margin-bottom: .5rem;
	position: relative;
	font-size: .8rem;
	color: var(--text-light);
	background: var(--shadow-strong-30);
	box-shadow: 0 0 3px var(--shadow);
}

.channel {
	display: inline-flex;
	align-items: center;
	font-weight: bold;
}

.favicon-link {
	display: inline-flex;
}

.favicon {
	width: 1rem;
	height: 1rem;
	margin-right: .5rem;
	object-fit: contain;
}

.row {
	margin: 0 .5rem .25rem .5rem;
	font-size: .9rem;
}

.title {
	display: block;
	margin-bottom: .4rem;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	font-weight: bold;
}

.actor:hover,
.tag:hover {
	color: var(--primary);
}

.actors {
	height: 1rem;
	overflow: hidden;
	white-space: pre-wrap;
}

.actor {
	&:not(:last-child)::after {
		content: ',\0020';
	}
}

.tags {
	height: 1.25rem;
	overflow: hidden;
}

.tag {
	margin: 0 .5rem .25rem 0;
	padding: .1rem 0;
	color: var(--shadow-strong-10);
	font-size: .75rem;
}
</style>
