<template>
	<header class="header">
		<nav class="nav">
			<Link href="/">
				<h1 class="title">
					<div
						class="logo"
						v-html="logo"
					/>
				</h1>
			</Link>

			<ul class="nav-list nolist">
				<!--
				<li class="nav-item">
					<Link
						class="link"
						:class="{ active: activePage === 'updates' }"
						href="/updates"
					>Updates</Link>
				</li>
				-->

				<li class="nav-item">
					<Link
						class="link"
						:class="{ active: activePage === 'actors' }"
						href="/actors"
					>Actors</Link>
				</li>

				<li class="nav-item">
					<Link
						class="link"
						:class="{ active: activePage === 'channels' }"
						href="/channels"
					>Channels</Link>
				</li>

				<li class="nav-item">
					<Link
						class="link"
						:class="{ active: activePage === 'tags' }"
						href="/tags"
					>Tags</Link>
				</li>

				<li class="nav-item">
					<Link
						class="link"
						:class="{ active: activePage === 'movies' }"
						href="/movies"
					>Movies</Link>
				</li>
			</ul>
		</nav>

		<div class="header-section">
			<form
				class="search"
				:class="{ focused: searchFocused }"
				@submit.prevent="search"
			>
				<input
					v-model="query"
					type="search"
					placeholder="Search"
					class="input"
					@focus="searchFocused = true"
					@blur="searchFocused = false"
				>

				<button class="search-button">
					<Icon icon="search" />
				</button>
			</form>

			<VDropdown
				v-if="user"
				:triggers="['click']"
			>
				<div class="userpanel">
					<img
						:src="user.avatar"
						class="avatar"
					>
				</div>

				<template #popper>
					<div class="menu">
						<a
							:href="`/user/${user.username}`"
							class="menu-header"
						>{{ user.username }}</a>

						<ul class="menu-list nolist">
							<li
								class="menu-item logout"
								@click="logout"
							><Icon icon="exit2" />Log out</li>
						</ul>
					</div>
				</template>
			</VDropdown>

			<div
				v-else
				class="userpanel"
			>
				<a
					:href="`/login?r=${encodeURIComponent(currentPath)}`"
					class="login button button-submit"
				>Log in</a>
			</div>
		</div>
	</header>
</template>

<script setup>
import { ref, computed, inject } from 'vue';
import navigate from '#/src/navigate.js';
import { del } from '#/src/api.js';

import logo from '../../assets/img/logo.svg?raw'; // eslint-disable-line import/no-unresolved

const pageContext = inject('pageContext');

const user = pageContext.user;
const query = ref(pageContext.urlParsed.search.q || '');
const searchFocused = ref(false);

const activePage = computed(() => pageContext.urlParsed.pathname.split('/')[1]);
const currentPath = `${pageContext.urlParsed.pathnameOriginal}${pageContext.urlParsed.searchOriginal || ''}`;

function search() {
	navigate('/search', { q: query.value }, { redirect: true });
}

async function logout() {
	del('/session');
	navigate('/login', null, { redirect: true });
}
</script>

<style scoped>
.header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	z-index: 1000; /* make sure shadow shows up above content */
	box-shadow: 0 0 3px var(--shadow-weak-10);
}

.title {
	margin: 0;
	display: inline-block;
}

.logo {
	display: flex;
	width: 8rem;
	height: 3rem;
	box-sizing: border-box;
	padding: .75rem;
	margin-right: 1rem;
	fill: var(--primary);
}

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

.nav-item .link {
	font-size: .9rem;
	color: var(--shadow-strong-10);
	box-sizing: border-box;
	padding: 1rem;
	height: 100%;

	&:hover {
		text-decoration: none;
	}
}

.link {
	font-weight: bold;

	&.active {
		color: var(--primary);
	}
}

.header-section {
	height: 100%;
	display: flex;
	align-items: center;
}

.search {
	height: 2rem;
	display: flex;
	align-items: center;
	border: solid 1px var(--shadow-weak-20);
	border-radius: 1rem;
	background: var(--background);

	.input {
		padding: .5rem 0 .5rem 1rem;
		border: none;
		margin: 0;
		background: none;
	}

	.search-button {
		padding: 0;
		border: none;
		background: none;
	}

	.icon {
		height: 100%;
		padding: 0 .75rem 0 .5rem;
		fill: var(--shadow-weak-10);
	}

	&.focused {
		border: solid 1px var(--primary-light-10);

		.icon {
			fill: var(--primary);
			cursor: pointer;
		}
	}
}

.userpanel {
	height: 100%;
	display: flex;
	align-items: center;
	padding: 0 1rem 0 1.5rem;
	font-size: 0;
	cursor: pointer;

	&:hover .avatar {
		box-shadow: 0 0 3px var(--shadow-weak-10);
	}
}

.avatar {
	width: 2rem;
	height: 2rem;
	border-radius: .25rem;
	object-fit: cover;
}

.login {
	text-decoration: none;
}

.menu-header {
	display: flex;
	justify-content: center;
	padding: .75rem 1rem;
	border-bottom: solid 1px var(--shadow-weak-30);
	color: var(--shadow-strong-30);
	text-decoration: none;
	text-align: center;
	font-weight: bold;
}

.menu-item {
	display: flex;
	align-items: center;
	padding: .5rem;

	.icon {
		fill: var(--shadow);
		margin-right: .5rem;
	}

	&:hover {
		background: var(--shadow-weak-30);
		cursor: pointer;
	}
}

.logout {
	color: var(--error);

	.icon {
		fill: var(--error);
	}
}
</style>
