<template>
  <content-with-hero>
    <template #heading>
      <pane-hero :content="heading" />
    </template>
    <template #image>
      <control-image
        :url="album.images?.[0]?.url ?? ''"
        :caption="album.name"
        class="is-medium"
        @click="openDetails"
      />
    </template>
    <template #content>
      <list-tracks-spotify :items="tracks" :context-uri="album.uri" />
    </template>
  </content-with-hero>
  <modal-dialog-album-spotify
    :item="album"
    :show="showDetailsModal"
    @close="showDetailsModal = false"
  />
</template>

<script>
import ContentWithHero from '@/templates/ContentWithHero.vue'
import ControlImage from '@/components/ControlImage.vue'
import ListTracksSpotify from '@/components/ListTracksSpotify.vue'
import ModalDialogAlbumSpotify from '@/components/ModalDialogAlbumSpotify.vue'
import PaneHero from '@/components/PaneHero.vue'
import queue from '@/api/queue'
import services from '@/api/services'
import { useServicesStore } from '@/stores/services'

export default {
  name: 'PageAlbumSpotify',
  components: {
    ContentWithHero,
    ControlImage,
    ListTracksSpotify,
    ModalDialogAlbumSpotify,
    PaneHero
  },
  beforeRouteEnter(to, from, next) {
    services.spotify().then(({ api, configuration }) => {
      api.albums
        .get(to.params.id, configuration.webapi_country)
        .then((album) => {
          next((vm) => {
            vm.album = album
          })
        })
    })
  },
  setup() {
    return { servicesStore: useServicesStore() }
  },
  data() {
    return {
      album: { artists: [{}], tracks: {} },
      showDetailsModal: false
    }
  },
  computed: {
    heading() {
      return {
        actions: [
          { handler: this.play, icon: 'shuffle', key: 'actions.shuffle' },
          { handler: this.openDetails, icon: 'dots-horizontal' }
        ],
        count: this.$t('data.tracks', { count: this.album.tracks.total }),
        handler: this.openArtist,
        subtitle: this.album.artists[0].name,
        title: this.album.name
      }
    },
    tracks() {
      const { album } = this
      if (album.tracks.total) {
        return album.tracks.items.map((track) => ({ ...track, album }))
      }
      return []
    }
  },
  methods: {
    openArtist() {
      this.$router.push({
        name: 'music-spotify-artist',
        params: { id: this.album.artists[0].id }
      })
    },
    openDetails() {
      this.showDetailsModal = true
    },
    play() {
      this.showDetailsModal = false
      queue.playUri(this.album.uri, true)
    }
  }
}
</script>
