<template>
	<div class="filter actors-container">
		<div class="filters-sort">
			<input
				v-model="search"
				type="search"
				placeholder="Filter actors"
				class="input input-inline filters-search"
			>

			<div
				class="filter-sort noselect"
				@click="selectGender"
			>
				<div
					v-if="!selectedGender"
					class="gender-unselected"
				><Icon icon="genders" /></div>

				<Gender
					v-else
					:gender="selectedGender"
					class="gender"
				/>
			</div>

			<div
				v-show="order === 'name'"
				class="filter-sort order noselect"
				@click="order = 'count'"
			>
				<Icon
					icon="sort-alpha-asc"
				/>
			</div>

			<div
				v-show="order === 'count'"
				class="filter-sort order noselect"
				@click="order = 'name'"
			>
				<Icon
					icon="sort-numeric-desc"
				/>
			</div>
		</div>

		<ul
			v-for="(group, groupKey) in groupedActors"
			:key="groupKey"
			class="filter-items nolist"
		>
			<li
				v-for="(actor, index) in group"
				:key="`filter-actor-${actor.id}`"
				class="filter-item"
				:class="{ selected: filters.actors.some((filterActor) => filterActor.id === actor.id), first: groupKey === 'available' && index === 0 && filters.actors.length > 0 }"
				@click="emit('update', 'actors', [actor])"
			>
				<div
					class="filter-include"
					@click.stop="toggleActor(actor)"
				>
					<Icon
						icon="checkmark"
						class="filter-add"
					/>

					<Icon
						icon="cross2"
						class="filter-remove"
					/>
				</div>

				<span class="filter-name actor-name">
					<span
						class="filter-text"
						:title="actor.name"
					>{{ actor.name }}</span>

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

						<span
							v-if="actor.count"
							class="filter-count"
						>{{ actor.count }}</span>
					</span>
				</span>
			</li>
		</ul>
	</div>
</template>

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

import Gender from '#/components/actors/gender.vue';

const props = defineProps({
	filters: {
		type: Object,
		default: null,
	},
	actors: {
		type: Array,
		default: () => [],
	},
});

const emit = defineEmits(['update']);

const search = ref('');
const searchRegexp = computed(() => new RegExp(search.value, 'i'));
const selectedGender = ref(null);
const order = ref('name');

const { pageProps } = inject('pageContext');
const { actor: pageActor } = pageProps;

const groupedActors = computed(() => ({
	selected: props.filters.actors.map((filterActor) => props.actors.find((actor) => actor.id === filterActor.id)).filter(Boolean),
	available: props.actors
		.filter((actor) => !props.filters.actors.some((filterActor) => filterActor.id === actor.id)
			&& actor.id !== pageActor?.id
			&& searchRegexp.value.test(actor.name)
			&& (!selectedGender.value || actor.gender === selectedGender.value))
		.sort((actorA, actorB) => {
			if (order.value === 'count') {
				return actorB.count - actorA.count;
			}

			return actorA.name.localeCompare(actorB.name);
		}),
}));

const genders = computed(() => [null, ...['female', 'male', 'transsexual', 'other'].filter((gender) => props.actors.some((actor) => actor.gender === gender))]);

function toggleActor(actor) {
	if (props.filters.actors.some((filterActor) => filterActor.id === actor.id)) {
		emit('update', 'actors', props.filters.actors.filter((filterActor) => filterActor.id !== actor.id));
		return;
	}

	emit('update', 'actors', props.filters.actors.concat(actor));
}

function selectGender() {
	const genderIndex = genders.value.indexOf(selectedGender.value);

	if (genderIndex >= genders.value.length - 1) {
		selectedGender.value = genders.value[0];
		return;
	}

	selectedGender.value = genders.value[genderIndex + 1];
}
</script>

<style scoped>
.filter {
	padding: 0;
}

.filter-item.first {
	border-top: solid 1px var(--shadow-weak-30);
}

.filter-name {
	align-items: stretch;
}

.actor-name {
	display: flex;
	align-items: stretch;
}

.actor-gender {
	width: 1rem;
	display: flex;
	align-items: center;

	.gender {
		display: flex;
	}
}
</style>
