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

import Heart from '#/components/stashes/heart.vue';
import getPath from '#/src/get-path.js';

import { formatDate } from '#/utils/format.js';
import Gender from './gender.vue';

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

const pageContext = inject('pageContext');
const { user, restriction } = pageContext;
const pageStash = pageContext.pageProps.stash;
const currentStash = pageStash || pageContext.assets?.primaryStash;

const favorited = ref(props.actor.stashes.some((actorStash) => actorStash.id === currentStash?.id));
const entityTooltipId = useId();
const aliasTooltipId = useId();
</script>

<template>
	<div
		class="tile"
		:class="{
			unstashed: !favorited && pageStash && user && pageStash.user.id === user?.id,
		}"
	>
		<div class="avatar-container">
			<Link
				:href="`/actor/${actor.id}/${actor.slug}`"
				class="avatar-link no-link"
			>
				<img
					v-if="actor.avatar && !restriction"
					:src="getPath(actor.avatar, 'thumbnail')"
					:style="{ 'background-image': `url(${getPath(actor.avatar, 'lazy')})` }"
					loading="lazy"
					class="avatar"
				>

				<img
					v-else
					:src="`/img/avatars/${actor.gender || 'female'}.svg`"
					loading="lazy"
					class="fallback"
				>
			</Link>

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

			<div class="details">
				<span class="birth">
					<Gender :gender="actor.gender" />

					<span
						v-if="actor.ageAtDeath"
						:title="`Passed ${formatDate(actor.dateOfDeath, 'MMMM d, yyyy')}`"
						class="age age-death"
					>{{ actor.ageAtDeath }}</span>

					<span
						v-else-if="actor.ageFromBirth"
						:title="`Born ${formatDate(actor.dateOfBirth, 'MMMM d, yyyy')}`"
						class="age"
					>{{ actor.ageFromBirth }}</span>

					<!-- don't show 'age then' for scenes released after the performer's death, because it might show an age they sadly never reached -->
					<span
						v-if="actor.ageThen && actor.ageThen < actor.ageFromBirth && (!actor.ageAtDeath || actor.ageThen < actor.ageAtDeath)"
						title="Age on date of release"
						class="age age-then"
					>{{ actor.ageThen }}</span>
				</span>

				<span
					v-if="actor.origin?.country"
					:title="`Born in ${actor.origin.country.alias || actor.origin.country.name}`"
					class="country"
				>
					{{ actor.origin.country.alpha2 }}
					<img
						:src="`/img/flags/${actor.origin.country.alpha2.toLowerCase()}.svg`"
						class="flag"
					>
				</span>
			</div>
		</div>

		<span class="label">
			<span
				class="name ellipsis"
				:style="{ 'font-size': `${Math.max(0.9 + Math.min((17 - actor.name.length), 0) * 0.06, 0.65)}rem` }"
			>{{ actor.name }}</span>

			<img
				v-if="actor.entity"
				v-tooltip="{ content: actor.entity.name, ariaId: entityTooltipId }"
				:src="`/logos/${actor.entity.slug}/favicon_dark.png`"
				class="favicon"
			>

			<Icon
				v-if="actor.alias && actor.alias.name !== actor.name"
				v-tooltip="{
					content: `Credited as '${actor.alias.name}'`,
					triggers: ['hover', 'click'],
					ariaId: aliasTooltipId,
				}"
				icon="at-sign"
				class="alias"
			/>
		</span>
	</div>
</template>

<style scoped>
.tile {
	display: flex;
	flex-direction: column;
	width: 100%;
	position: relative;
	aspect-ratio: 3/5;
	border-radius: .25rem;
	box-shadow: 0 0 3px var(--shadow-weak-30);
	overflow: hidden;
	background: var(--background);

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

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

		.menu {
			fill: var(--text-light);
		}
	}

	&.unstashed {
		opacity: .5;
	}
}

.menu {
	padding: .5rem .75rem;
	position: absolute;
	top: 0;
	left: 0;
	fill: var(--highlight-strong-20);
}

.label {
	height: 1.75rem;
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-shrink: 0;
	font-weight: bold;
	font-size: .9rem;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	user-select: all;
}

.name {
	padding: 0 .25rem 0 .5rem;
}

.favicon {
	height: 1rem;
	padding: .35rem .5rem .35rem .25rem;
}

.avatar-container {
	position: relative;
	flex-grow: 1;
	overflow: hidden;
	background: var(--grey-dark-40);
}

.avatar-link {
	height: 100%;
	display: block;
	overflow: hidden;
}

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

.fallback {
	height: 100%;
	width: 100%;
	display: block;
	object-fit: contain;
	opacity: .1;
}

.details {
	width: 100%;
	height: 1.5rem;
	display: flex;
	justify-content: space-between;
	align-items: center;
	position: absolute;
	bottom: 0;
	box-sizing: border-box;
	padding: 0 .5rem;
	color: var(--text-light);
	background: var(--shadow);
	font-size: .9rem;
	font-weight: bold;
}

.gender {
	display: inline-block;
	padding: .25rem 0;
	margin-right: .25rem;
	transform: translateY(1px);
}

.birth {
	display: flex;
	align-items: center;
}

.age {
	display: inline-flex;
	align-items: center;
	margin-right: .25rem;

	.icon {
		width: .8rem;
		height: .8rem;
		fill: var(--highlight-weak-10);
		margin-left: .25rem;
	}
}

.age-then {
	color: var(--grey-light-20);
	font-weight: normal;
}

.age-death {
	color: var(--grey-light-20);
}

.country {
	display: flex;
	align-items: center;
}

.flag {
	height: .75rem;
	margin-left: .25rem;
}

.alias {
	height: 100%;
	fill: var(--glass-weak-20);
	padding: 0 .25rem;
	cursor: help;
}
</style>
