<template>
    <div class="inline-flex p-1 min-w-[25px]">
        <div v-show="visible" class="bg-black rounded-full text-xs inline-flex" >

            <button v-if="type === 'score'" @click="openScoreModal()" class="inline-flex p-1">
                <Icon name="ion:book-sharp" style="color: white" />
            </button>

            <a v-else-if="type === 'document'" :href="isExternalLink ? link : undefined" :target="isExternalLink ? '_blank' : undefined" :rel="isExternalLink ? 'noopener noreferrer' : undefined" @click="openDocument()" class="inline-flex p-1 cursor-pointer">
                <Icon name="ion:book-sharp" style="color: white" />
            </a>

            <a v-else-if="type === 'buy'" :href="link" :target="newTab ? '_blank' : undefined" :rel="newTab ? 'noopener noreferrer' : undefined" class="inline-flex p-1 cursor-pointer">
                <Icon name="bxs:purchase-tag" style="color: white" />
            </a>

            <NuxtLink v-else-if="type === 'email'" class="inline-flex p-1" :to="link">
                <Icon name="ic:baseline-email" style="color: white" />
            </NuxtLink>

            <a v-else-if="type === 'discogs'" :href="link" :target="newTab ? '_blank' : undefined" :rel="newTab ? 'noopener noreferrer' : undefined" class="inline-flex p-1 cursor-pointer">
                <Icon name="simple-icons:discogs" style="color: white" />
            </a>

            <button @click="audioPlayerStore.setSoundCloudTrackID(work.soundcloud_trackid)" v-else-if="type === 'audio'" class="inline-flex p-1">
                <Icon name="wpf:speaker" style="color: white" />
            </button>

            <button @click="modalStore.setModalProps('video', 'aspect-video', true, '', '', work.vimeo_trackid)" v-else-if="type === 'video'" class="inline-flex p-1">
                <Icon name="fluent:video-48-filled" style="color: white" />
            </button>

            <button @click="modalStore.setModalProps('image', 'aspect-auto', true, 'images', work.gallery, '', '', work.soundcloud_trackid ? 'https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + work.soundcloud_trackid + '&auto_play=true&show_user=false' : '')" v-else="type === 'image'" class="inline-flex p-1">
                <Icon name="mdi:camera" style="color: white" />
            </button>

        </div>
    </div>
</template>

<script setup>
  import { useAudioPlayerStore } from "@/stores/AudioPlayerStore"
  import { useModalStore } from "@/stores/ModalStore"
  import { computed, watch } from "vue"

  const props = defineProps(['type', 'work', 'visible', 'link', 'newTab'])

  const audioPlayerStore = useAudioPlayerStore()
  const modalStore = useModalStore()

  const slugify = (title) => {
    if (!title) return ''
    return title.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '')
  }

  const workSlug = computed(() => slugify(props.work?.title))

  const isExternalLink = computed(() => {
    return props.link && !props.link.endsWith('.pdf') && !props.link.startsWith('/')
  })

  const isInternalPage = computed(() => {
    return props.link && props.link.startsWith('/') && !props.link.endsWith('.pdf')
  })

  const openDocument = () => {
    if (props.link?.endsWith('.pdf')) {
      modalStore.setModalProps('pdf', 'aspect-[1/1.414]', true, '', '', '', props.link)
    } else if (props.link?.startsWith('/')) {
      modalStore.setModalProps('document', 'aspect-[1/1.414]', true, '', '', '', '', '', props.link)
    }
  }

  const openScoreModal = () => {
    modalStore.setModalProps('pdf', 'aspect-[1/1.414]', true, '', '', '', props.link, props.work?.soundcloud_trackid ? 'https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + props.work.soundcloud_trackid + '&auto_play=true&show_user=false' : '')
    if (workSlug.value && typeof window !== 'undefined') {
      window.history.replaceState({}, '', '/?work=' + workSlug.value)
    }
  }

  watch(() => modalStore.isOpen, (isOpen) => {
    if (!isOpen && workSlug.value && typeof window !== 'undefined' && window.location.search.includes('work=')) {
      window.history.replaceState({}, '', '/')
    }
  })
</script>
