<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { momentFormat } from '~/utils/filters'

import Play from '~/components/ui/button/Play.vue'
import PlayButton from '~/components/audio/PlayButton.vue'
import OptionsButton from '~/components/ui/button/Options.vue'
import Card from '~/components/ui/Card.vue'
import Link from '~/components/ui/Link.vue'
import TagsList from '~/components/tags/List.vue'

import type { Album } from '~/types'

const play = defineEmit<[album: Album]>()

interface Props {
  album: Album;
}

const { t } = useI18n()

const props = defineProps<Props>()

const { album } = props

let navigate = (to: 'artist' | 'album') => {}

const artist_credit = album.artist_credit || []
const firstArtist = artist_credit.length > 0 ? artist_credit[0].artist : null

if (import.meta.env.PROD) {
  const router = useRouter()
  navigate = (to: 'artist' | 'album') => to === 'album'
    ? router.push({ name: 'library.albums.detail', params: { id: album.id } })
    : router.push({ name: 'library.artists.detail', params: { id: firstArtist?.id } })
}
</script>

<template>
  <Card
    :title="album.title"
    :image="album.cover?.urls.original"
    :to="{name: 'library.albums.detail', params: {id: album.id}}"
  >
    <template #image
      v-if="!album.cover || !album.cover.urls.original"
    >
      <div
        :class="[{'default-cover': !album.cover || !album.cover.urls.original}]"
        style="height: 200px; background-repeat: no-repeat; margin: 39px;"
      >
      </div>
    </template>

    <template
      v-for="ac in album.artist_credit"
      :key="ac.artist.id"
    >
      <router-link
        :to="{ name: 'library.artists.detail', params: { id: ac.artist.id }}"
      >
        {{ ac.credit ?? t('components.Queue.meta.unknownArtist') }}
      </router-link>
      <span>{{ ac.joinphrase }}</span>
    </template>

    <TagsList
      label-classes="tiny"
      :truncate-size="20"
      :limit="2"
      :show-more="false"
      :tags="album.tags"
    />

    <template #action>
      <span v-if="album.release_date">
        {{ momentFormat(new Date(album.release_date), 'Y') }}
      </span>
      <span>
        <i class="bi bi-music-note-list" />
        {{ t('components.audio.album.Card.meta.tracks', album.tracks_count) }}
      </span>
      <Spacer style="flex-grow: 1" />
      <PlayButton
        id="playmenu"
        :dropdown-only="true"
        :is-playable="album.is_playable"
        :album="album"
      />
    </template>
  </Card>
</template>

<style scoped>
.funkwhale {
  &.card.album-card {
    --fw-border-radius: 12px;
    --Card-width: 208px;
    --Card-image-width: var(--Card-width);
    --Card-padding: 16px;

    > .card-image {
      border-radius: 0 !important;
      width: var(--Card-image-width);
      margin: calc(-1 * var(--Card-padding)) calc(-1 * var(--Card-padding)) 0;
    }

    > .card-title {
      font-size: 1rem;
      text-align: left !important;
      padding-top: 16px !important;
    }

    > .card-content {
      padding-top: 0 !important;
      text-align: left !important;
    }
  }
}
</style>
