<script setup>
import { inject } from 'vue';

import ActorTile from '#/components/actors/tile.vue';
import SceneTile from '#/components/scenes/tile.vue';

import Heart from '#/components/stashes/heart.vue';
import Player from '#/components/video/player.vue';
import getPath from '#/src/get-path.js';
import { formatDate, formatDuration } from '#/utils/format.js';

const pageContext = inject('pageContext');

const serie = pageContext.pageProps.serie;
const scenes = pageContext.pageProps.scenes;
</script>

<template>
	<div class="page">
		<div class="content">
			<div
				class="banner-container"
				:style="{ 'background-image': `url(${getPath(serie.poster)})` }"
			>
				<div class="banner">
					<div
						v-if="serie.trailer"
						class="trailer"
					>
						<Player
							:video="serie.trailer"
							:poster="poster"
							class="item"
							:class="{ playing }"
							@play="playing = true; paused = false;"
							@pause="playing = false; paused = true;"
						/>
					</div>

					<div
						v-else-if="serie.poster"
						class="poster-container"
					>
						<div class="poster-link">
							<img
								:src="getPath(serie.poster, 'thumbnail')"
								:style="{ 'background-image': `url(${getPath(serie.poster, 'lazy')}` }"
								:width="serie.poster.width"
								:height="serie.poster.height"
								class="poster"
							>
						</div>
					</div>

					<img
						v-for="cover in serie.covers"
						:key="`cover-${cover.id}`"
						:src="getPath(cover, 'thumbnail')"
						class="cover"
					>

					<div
						v-if="serie.photos.length > 0"
						class="album"
						:class="{ single: serie.photos.length === 1 }"
					>
						<div
							v-for="photo in serie.photos"
							:key="`photo-${photo.id}`"
							class="photo-container"
						>
							<a
								:href="getPath(photo)"
								target="_blank"
								class="photo-link"
							>
								<img
									:src="getPath(photo, 'thumbnail')"
									:style="{ 'background-image': getPath(photo, 'lazy') }"
									:width="photo.width"
									:height="photo.height"
									class="photo"
								>
							</a>
						</div>
					</div>
				</div>
			</div>

			<div class="meta">
				<div class="entity">
					<Link
						v-if="serie.channel"
						:href="`/${serie.channel.type}/${serie.channel.slug}`"
						class="channel-link entity-link"
					>
						<img
							v-if="serie.channel.hasLogo"
							:src="serie.channel.isIndependent || !serie.network ? `/logos/${serie.channel.slug}/network.png` : `/logos/${serie.network.slug}/${serie.channel.slug}.png`"
							class="channel-logo entity-logo"
						>
					</Link>

					<span
						v-if="!serie.channel.isIndependent && serie.network"
						class="network-container"
					>
						by
						<Link
							:href="`/${serie.network.type}/${serie.network.slug}`"
							class="network-link entity-link"
						>
							<img
								v-if="serie.network.hasLogo"
								:src="`/logos/${serie.network.slug}/network.png`"
								class="network-logo entity-logo"
							>
						</Link>
					</span>
				</div>

				<time
					:datetime="serie.effectiveDate.toISOString()"
					class="date ellipsis compact-hide"
					:class="{ nodate: !serie.date }"
				>{{ formatDate(serie.effectiveDate, {
					month: 'MMMM y',
					year: 'y',
				}[serie.datePrecision] || 'MMMM d, y') }}</time>

				<time
					:datetime="serie.effectiveDate.toISOString()"
					class="date ellipsis compact-show"
					:class="{ nodate: !serie.date }"
				>{{ formatDate(serie.effectiveDate, {
					month: 'MMM y',
					year: 'y',
				}[serie.datePrecision] || 'MMM d, y') }}</time>
			</div>

			<div class="header">
				<h2
					v-if="serie.title"
					:title="serie.title"
					class="title"
				>{{ serie.title }}</h2>

				<h2
					v-else-if="serie.actors.length > 0"
					class="title notitle"
				>serie featuring {{ serie.actors.map((actor) => actor.name).join(', ') }}</h2>

				<h2
					v-else
					class="title notitle"
				>No title</h2>

				<div class="actions">
					<Heart
						domain="series"
						:item="serie"
					/>

					<div class="view">
						<button
							v-if="serie.photos.length > 0"
							class="button view nolink"
						>View photos</button>

						<Link
							v-if="serie.url"
							:href="serie.url"
							target="_blank"
							class="button watch nolink"
						>Watch serie</Link>
					</div>
				</div>
			</div>

			<div class="info">
				<ul class="actors nolist">
					<li
						v-for="actor in serie.actors"
						:key="`actor-${actor.id}`"
						class="actor"
					>
						<ActorTile :actor="actor" />
					</li>
				</ul>

				<ul class="tags nolist">
					<li
						v-for="tag in serie.tags"
						:key="`tag-${tag.id}`"
					>
						<Link
							:href="`/tag/${tag.slug}`"
							class="tag nolink"
						>{{ tag.name }}</Link>
					</li>
				</ul>

				<div
					v-if="scenes.length > 0"
					class="section"
				>
					<h3 class="heading">Scenes</h3>

					<ul class="scenes nolist">
						<li
							v-for="scene in scenes"
							:key="`scene-${scene.id}`"
						>
							<SceneTile :scene="scene" />
						</li>
					</ul>
				</div>

				<div
					v-if="serie.description"
					class="section"
				>
					<h3 class="heading">Description</h3>

					<p class="description">{{ serie.description }}</p>
				</div>

				<div class="section details">
					<div
						v-if="serie.duration"
						class="detail"
					>
						<h3 class="heading">Duration</h3>
						{{ formatDuration(serie.duration) }}
					</div>

					<div
						v-if="serie.directors.length > 0"
						class="detail"
					>
						<h3 class="heading">Director</h3>
						{{ serie.directors.map((director) => director.name).join(', ') }}
					</div>

					<div
						v-if="serie.shootId"
						class="detail"
					>
						<h3 class="heading">Shoot</h3>
						{{ serie.shootId }}
					</div>
				</div>

				<div class="section details">
					<div class="detail">
						<h3 class="heading">Added</h3>
						<span class="added-date">{{ formatDate(serie.createdAt, 'yyyy-MM-dd') }}</span>
						<span
							:title="`Batch ${serie.createdBatchId}`"
							class="added-batch"
						>#{{ serie.createdBatchId }}</span>
					</div>

					<div
						v-if="serie.comment"
						class="detail"
					>
						<h3 class="heading">Comment</h3>
						{{ serie.comment }}
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<style scoped>
.page {
	display: flex;
	justify-content: center;
	background: var(--background-base-10);
}

.content {
	width: 100%;
	max-width: 1200px;
	margin: 0 .5rem;
	display: flex;
	flex-direction: column;
}

.banner-container {
	background-position: center;
	background-size: cover;
	border-radius: .5rem .5rem 0 0;
	margin-top: .5rem;
	box-shadow: 0 0 3px var(--shadow-weak-30);
}

.banner {
	max-height: 21rem;
	border-radius: .5rem 0 0 0;
	display: flex;
	font-size: 0;
	backdrop-filter: brightness(150%) blur(1rem);
	overflow: hidden;
}

.poster-container {
	flex-shrink: 0;
	margin-right: .5rem;
}

.poster {
	height: 100%;
	width: 100%;
	border-radius: .25rem 0 0 0;
}

.poster,
.photo {
	object-fit: cover;
	background-size: cover;
	background-position: center;
	box-shadow: 0 0 3px var(--shadow-weak-10);
}

.trailer {
	max-width: 100%;
	width: calc(21/9 * 16rem);
	margin-right: .5rem;
	flex-shrink: 0;
	aspect-ratio: 16/9;
}

:deep(.player) {
	width: 100%;
	height: 100%;
	flex-shrink: 0;

	&:not(.playing) {
		&[poster],
		.vjs-tech[poster],
		.vjs-poster img {
			object-fit: cover;
		}
	}
}

.album {
	height: auto;
	flex-grow: 1;
	display: grid;
    grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
	gap: .25rem;
	box-sizing: border-box;
	padding: .5rem .5rem .5rem 0;
	overflow-y: auto;
	scrollbar-width: 0;

	&::-webkit-scrollbar {
		display: none;
	}

	&.single .photo {
		max-height: calc(100% - 1.5rem);
	}
}

.photo-container {
	display: flex;
	justify-content: center;
	align-items: center;
	flex-grow: 1;
}

.photo-link {
	width: 100%;
	height: 100%;
}

.photo {
	width: 100%;
	height: 100%;
	border-radius: .25rem;
}

.meta {
	display: flex;
	justify-content: space-between;
	align-items: center;
	background: var(--grey-dark-40);
	border-radius: 0 0 .5rem .5rem;
	color: var(--text-light);
}

.entity {
	display: flex;
	align-items: center;
	font-weight: bold;
	color: var(--highlight);
}

.entity-link {
	padding: .5rem 1rem;
}

.entity-logo {
	width: 10rem;
	max-height: 100%;
	object-fit: contain;
}

.network-container {
	display: flex;
	align-items: center;
}

.date {
	padding: 1rem;
	font-weight: bold;
}

.nodate {
	color: var(--highlight);
	font-weight: normal;
}

.info,
.header {
	border-top: none;
	border-bottom: none;
}

.info {
	padding: 0;
}

.header {
	display: flex;
	align-items: flex-start;
	justify-content: space-between;
	padding: 1rem .5rem .5rem .5rem;
}

.title {
	margin: 0 .5rem 1rem 0;
	line-height: 1.25;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}

.notitle {
	color: var(--grey-dark-10);
}

.actions {
	display: flex;
	align-items: center;
	justify-content: space-between;
	flex-shrink: 0;

	.icon {
		width: 1.5rem;
		height: 1.5rem;
		fill: var(--glass);
	}

	.button {
		flex-shrink: 0;
		padding: .75rem;
	}

	.button:not(:last-child) {
		margin-right: .5rem;
	}
}

.bookmarks {
	margin-right: .75rem;
}

.view {
	display: flex;
}

.watch {
	background: var(--primary);
	color: var(--text-light);
}

.cover {
	height: 20rem;
}

.actors,
.tags {
	margin-bottom: 1rem;
}

.section {
	margin-bottom: 1rem;
}

.heading {
	color: var(--primary);
	margin: 0 0 .5rem 0;
	font-size: .9rem;
}

.details {
	display: flex;
	gap: 1rem;
}

.description {
	font-size: 1rem;
	line-height: 1.5;
	text-align: justify;
	margin: 0;
}

.added-batch {
	color: var(--glass-weak-10);
	margin-left: .25rem;
}

.actors {
	display: grid;
	flex-grow: 1;
	grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr));
	gap: .25rem;
}

.tag {
	padding: .5rem;
	border-radius: .25rem;
	margin: 0 .25rem .25rem 0;
	background: var(--background);
	box-shadow: 0 0 3px var(--shadow-weak-30);

	&:hover {
		color: var(--primary);
		box-shadow: 0 0 3px var(--shadow-weak-20);
		cursor: pointer;
	}
}

.scenes {
	display: grid;
    grid-template-columns: repeat(auto-fill, minmax(20rem, 1fr));
	gap: .5rem;
	margin-bottom: 1rem;
}

.compact-show {
	display: none;
}

@media(--small) {
	.content {
		margin: 0;
	}

	.info {
		margin: 0 .5rem;
	}

	.banner-container {
		border-radius: 0;
		margin-top: 0;
	}

	.network-container {
		display: none;
	}

	.header {
		flex-direction: column-reverse;
	}

	.actions {
		width: 100%;
		justify-content: space-between;
		margin-bottom: 1.5rem;
	}

	.title {
		width: 100%;
		margin-left: .5rem;
		white-space: wrap;
	}

	.meta {
		border-radius: 0;
	}

	.entity-logo {
		width: 7.5rem;
	}
}

@media(--small-10) {
	.banner {
		justify-content: center;
		border-radius: 0;
	}

	.poster-container {
		margin: 0;
		flex-shrink: 1;
	}

	.trailer {
		margin: 0;
		flex-shrink: 1;
	}

	.poster {
		border-radius: 0;
	}

	.album {
		display: none;
	}

	.header {
		padding: 1rem .5rem 1.5rem .5rem;
	}

	.info {
		padding: 0 .5rem;
	}

	.actors {
		grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
	}
}

@media(--small-60) {
	.actors {
		grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
	}

	.compact-show {
		display: flex;
	}

	.compact-hide {
		display: none;
	}
}
</style>
