<template>
    <div
        v-if="actor"
        class="content actor"
    >
        <FilterBar :fetch-releases="fetchReleases" />

        <div class="header">
            <img
                v-if="actor.avatars && actor.avatars.length > 0"
                :src="`/media/${actor.avatars[0].path}`"
                class="avatar"
            >

            <ul class="bio sidebar nolist">
                <h2 class="title">{{ actor.name }}</h2>


                <li v-if="actor.aliases.length">
                    <dfn class="bio-heading">Also known as</dfn>
                    <span>{{ actor.aliases.join(', ') }}</span>
                </li>

                <li v-if="actor.gender">
                    <dfn class="bio-heading">Gender</dfn>
                    <span>{{ actor.gender }}</span>
                </li>

                <li v-if="actor.birthdate">
                    <dfn class="bio-heading">Date of birth</dfn>
                    <span>{{ formatDate(actor.birthdate, 'MMMM D, YYYY') }} ({{ age }})</span>
                </li>

                <li v-if="actor.birthCountry || actor.birthPlace">
                    <dfn class="bio-heading">Born in</dfn>

                    <span v-if="actor.birthCountry">
                        <img
                            class="flag"
                            :src="`/img/flags/${actor.birthCountry.alpha2.toLowerCase()}.svg`"
                        >{{ actor.birthCountry.name }}
                    </span>

                    <span v-if="actor.birthPlace">{{ actor.birthPlace }}</span>
                </li>

                <li v-if="actor.residenceCountry || actor.residencePlace">
                    <dfn class="bio-heading">Lives in</dfn>

                    <span v-if="actor.residenceCountry">
                        <img
                            class="flag"
                            :src="`/img/flags/${actor.residenceCountry.alpha2.toLowerCase()}.svg`"
                        >{{ actor.residenceCountry.name }}
                    </span>

                    <span v-if="actor.residencePlace">{{ actor.residencePlace }}</span>
                </li>

                <li v-if="actor.ethnicity">
                    <dfn class="bio-heading">Ethnicity</dfn>
                    <span>{{ actor.ethnicity }}</span>
                </li>

                <li v-if="actor.height">
                    <dfn class="bio-heading">Height</dfn>
                    <span>{{ actor.height }} cm</span>
                </li>

                <li v-if="actor.bust || actor.waist || actor.hip">
                    <dfn class="bio-heading">Measurements</dfn>
                    <span>{{ actor.bust || '??' }}-{{ actor.waist || '??' }}-{{ actor.hip || '??' }} <span v-if="actor.naturalBoobs !== undefined">({{ actor.naturalBoobs ? 'Natural' : 'Enhanced' }})</span></span>
                </li>

                <li v-if="actor.social && actor.social.length > 0">
                    <dfn class="bio-heading">Social</dfn>
                    <a
                        v-for="social in actor.social"
                        :key="`social-${social.id}`"
                        :href="social.url"
                        target="_blank"
                        rel="noopener noreferrer"
                        class="social"
                    >{{ social.platform || social.url }}</a>
                </li>

                <li class="description">{{ actor.description }}</li>

                <div class="photos">
                    <img
                        v-for="avatar in actor.avatars.slice(1)"
                        :key="`avatar-${avatar.id}`"
                        :src="`/media/${avatar.path}`"
                        class="photo"
                    >
                </div>
            </ul>

        </div>

        <div class="content-inner">
            <Releases
                :releases="releases"
                :context="actor.name"
            />
        </div>
    </div>
</template>

<script>
import dayjs from 'dayjs';

import FilterBar from '../header/filter-bar.vue';
import Releases from '../releases/releases.vue';

function age() {
    return dayjs(new Date()).diff(this.actor.birthdate, 'years');
}

async function fetchReleases() {
    this.releases = await this.$store.dispatch('fetchActorReleases', this.$route.params.actorSlug);
}

async function mounted() {
    [[this.actor]] = await Promise.all([
        this.$store.dispatch('fetchActors', this.$route.params.actorSlug),
        this.fetchReleases(),
    ]);

    this.pageTitle = this.actor.name;
}

export default {
    components: {
        FilterBar,
        Releases,
    },
    data() {
        return {
            actor: null,
            releases: null,
            pageTitle: null,
        };
    },
    computed: {
        age,
    },
    mounted,
    methods: {
        fetchReleases,
    },
};
</script>

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

.header {
    display: flex;
    justify-content: space-between;
    padding: 1rem;
}

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

.heading {
    padding: 0;
    margin: 0 0 1rem 0;
}

.bio-heading {
    display: inline-block;
    font-weight: bold;
    margin: .5rem 0 0 0;

    &::after {
        content: ':';
    }
}

.description {
    padding: 1rem;
}

.avatar {
    height: 20rem;
    margin: 0 1rem 0 0;
    display: block;
}

.photo {
    height: 10rem;
}

.social {
    display: block;
}

.flag {
    height: 1rem;
    border: solid 1px $shadow-weak;
    margin: 0 .25rem 0 0;
}

.releases {
    flex-grow: 1;
}
</style>
