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

import Bio from '#/components/actors/bio.vue';

import Gender from '#/components/actors/gender.vue';
import Domains from '#/components/domains/domains.vue';
import Movies from '#/components/movies/movies.vue';
import Scenes from '#/components/scenes/scenes.vue';
import Heart from '#/components/stashes/heart.vue';
import getPath from '#/src/get-path.js';

const pageContext = inject('pageContext');
const { pageProps, routeParams } = pageContext;
const { actor } = pageProps;

const domain = routeParams.domain;

const badCredits = ['Pierre Woodman']; // consistently horrible photos
const photos = actor.photos.filter((photo) => photo.entropy > 5.5 && !badCredits.includes(photo.credit));
const aliasTooltipId = useId();
</script>

<template>
	<div class="actor">
		<div class="actor-header">
			<h2 class="header-name">
				<span v-if="actor.entity">{{ actor.name }} ({{ actor.entity.name }})</span>
				<span v-else>{{ actor.name }}</span>

				<a
					v-if="actor.alias"
					v-tooltip="{ content: `Alias for ${actor.alias.name} (#${actor.alias.id})`, ariaId: aliasTooltipId }"
					:href="`/actor/${actor.alias.id}/${actor.alias.slug}`"
				>
					<Icon
						icon="at-sign"
						class="header-alias"
					/>
				</a>

				<Gender
					:gender="actor.gender"
					class="header-gender"
				/>
			</h2>

			<Heart
				domain="actors"
				:item="actor"
				class="light"
			/>
		</div>

		<div class="content">
			<Bio :actor="actor" />

			<div
				class="photos nobar"
				:class="{ 'has-avatar': actor.avatar, 'has-photos': actor.avatar ? photos.length > 1 : photos.length > 0 }"
			>
				<div
					v-for="photo in photos"
					:key="`photo-${photo.id}`"
					class="photo-container"
					:class="{ avatar: photo.isAvatar }"
				>
					<img
						:src="getPath(photo, 'thumbnail')"
						:width="photo.width"
						:height="photo.height"
						:style="{ 'background-image': `url('${getPath(photo, 'lazy')}')` }"
						:title="photo.credit && `© ${photo.credit}`"
						loading="lazy"
						class="photo"
					>

					<span
						v-if="photo.credit"
						class="photo-credit"
					>{{ photo.credit }}</span>
				</div>
			</div>

			<Domains
				:domain="domain"
				:path="`/actor/${actor.id}/${actor.slug}`"
				:domains="['scenes', 'movies']"
				class="domains-bar light"
			/>

			<Scenes v-if="domain === 'scenes'" />
			<Movies v-if="domain === 'movies'" />
		</div>
	</div>
</template>

<style scoped>
.actor {
	display: flex;
	flex-direction: column;
	flex-grow: 1;
	position: relative;
}

.actor-header {
	width: 100%;
    display: flex;
	justify-content: space-between;
    align-items: center;
	position: sticky;
	top: 0;
	z-index: 2;
    color: var(--highlight-strong-30);
    background: var(--grey-dark-40);
}

.header-name {
    padding: .5rem 1rem;
    margin: 0;
    display: inline-flex;
    justify-content: space-between;
    flex-shrink: 0;
}

.header-alias.icon {
	height: 1.75rem;
	display: flex;
	align-items: center;
    padding: 0 1rem;
	fill: var(--primary);
}

.header-social {
    overflow: hidden;
    white-space: nowrap;
    margin: 0 1rem 0 0;
}

.content {
	display: flex;
	flex-direction: column;
	flex-grow: 1;
	/* overflow-y: auto; */
}

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

.photos {
	display: flex;
	gap: 0.5rem;
	padding: .5rem;
	border-bottom: solid 1px var(--shadow-weak-40);
	background: var(--background-base-10);
	overflow-x: auto;

	&:not(.has-photos) {
		display: none;
	}
}

.photo-container {
	position: relative;
	font-size: 0;

	&.avatar {
		display: none;
	}
}

.photo {
	height: 14rem;
	width: auto;
	object-fit: cover;
	object-position: 50% 0;
	border-radius: .25rem;
	background-size: cover;
	background-position: center;
	box-shadow: 0 0px 3px var(--shadow-weak-30);
}

.photo-credit {
	width: 100%;
	position: absolute;
	left: 0;
	bottom: 0;
	z-index: 1;
	box-sizing: border-box;
	padding: .15rem .2rem;
	color: var(--text-light);
	font-size: .75rem;
	text-shadow: 1px 1px 0 var(--shadow-strong-20);
}

.domains-bar {
	background: var(--background-base-10);
	border-bottom: solid 1px var(--shadow-weak-40);
}

@media(--compact) {
	.photos.has-avatar {
		display: flex;
	}

	.photo-container.avatar {
		display: inline-block;
	}
}

@media(--small) {
	.photo {
		height: 10rem;
	}
}
</style>
