<template>
	<div
		v-if="tag"
		class="tag content"
	>
		<div class="header">
			<h2 class="title">
				<Icon icon="price-tag4" />
				{{ tag.name }}
			</h2>
		</div>

		<div class="content-inner">
			<div
				v-if="description"
				class="description header-description"
				v-html="description"
			/>

			<Scroll
				v-if="hasMedia"
				:expanded="expanded"
				class="scroll-light"
				@expand="(state) => expanded = state"
			>
				<Photos
					:tag="tag"
					:class="{ expanded }"
				/>
			</Scroll>

			<FilterBar :fetch-releases="fetchReleases" />
			<Releases :releases="tag.releases" />
		</div>
	</div>
</template>

<script>
/* eslint-disable no-v-html */
import { Converter } from 'showdown';

import escapeHtml from '../../../src/utils/escape-html';

import FilterBar from '../header/filter-bar.vue';
import Photos from './photos.vue';
import Releases from '../releases/releases.vue';
import Scroll from '../scroll/scroll.vue';

const converter = new Converter();

async function fetchReleases() {
	this.tag = await this.$store.dispatch('fetchTagBySlug', {
		tagSlug: this.$route.params.tagSlug,
		range: this.$route.params.range,
	});

	this.hasMedia = this.tag.poster || this.tag.photos.length > 0;
	this.description = this.tag.description && converter.makeHtml(escapeHtml(this.tag.description));
}

async function route() {
	await this.fetchReleases();
}

async function mounted() {
	await this.fetchReleases();
	this.pageTitle = this.tag.name;
}

export default {
	components: {
		FilterBar,
		Releases,
		Photos,
		Scroll,
	},
	data() {
		return {
			tag: null,
			description: null,
			releases: null,
			pageTitle: null,
			hasMedia: false,
			expanded: false,
		};
	},
	watch: {
		$route: route,
	},
	mounted,
	methods: {
		fetchReleases,
	},
};
</script>

<style lang="scss">
@import 'theme';

.description a {
    color: var(--link);
    text-decoration: inherit;

    &:hover {
        color: var(--primary);
    }
}

.description,
.description p {
    padding: 0;
    margin: 0;
}
</style>

<style lang="scss" scoped>
@import 'theme';

.header {
    background: var(--profile);
    color: var(--text-light);
    justify-content: space-between;
}

.title {
    padding: .75rem 1rem;
    margin: 0;
    flex-shrink: 0;
    text-transform: capitalize;

    .icon {
        fill: var(--text-light);
        width: 1.25rem;
        height: 1.25rem;
    }
}

.description {
	padding: 0 1rem .5rem 1rem;
    line-height: 1.5;
	color: var(--text-light);
	background: var(--profile);
}
</style>
