<script setup lang="ts">
import type {
  EventNavigationResponse,
  EventNavigation,
} from '#layers/types/api/eventNavigation'

const { locale } = useI18n()
const gameDomain = useGetGameDomain()

const isEventNavigationOpen = ref(true)
const eventNavigationList = ref<Record<string, EventNavigation>>({})

const getEventNavigation = async (): Promise<Record<
  string,
  EventNavigation
> | null> => {
  const runtimeConfig = useRuntimeConfig()
  const stoveApiBaseUrl = runtimeConfig.public.stoveApiUrl
  const apiUrl = `${stoveApiBaseUrl}/pub-comm/v1.0/template/eventBanners`

  const queryParams: Record<string, string> = {
    game_domain: gameDomain,
    lang_code: locale.value,
    _t: Date.now().toString(),
  }

  const response = (await commonFetch('GET', apiUrl, {
    query: queryParams,
  })) as EventNavigationResponse | null

  if (response?.code === 0 && 'value' in response) {
    return response.value
  }
  return null
}

const toggleEventNavigation = () => {
  isEventNavigationOpen.value = !isEventNavigationOpen.value
}

onMounted(async () => {
  eventNavigationList.value = await getEventNavigation()
})
</script>

<template>
  <div
    v-if="Object.keys(eventNavigationList).length > 1"
    class="event-navigation"
    :class="{
      'is-closed': !isEventNavigationOpen,
    }"
  >
    <div class="navigation-wrapper">
      <AtomsButtonCircle
        sr-only="event navigation control"
        class="btn-control"
        @click="toggleEventNavigation"
      >
        <AtomsIconsArrowRightLine size="24" color="#ffffff" />
      </AtomsButtonCircle>
      <ul class="navigation-list">
        <li v-for="item in eventNavigationList" :key="item.banner_seq">
          <AtomsLocaleLink
            :to="item.page_url"
            :target="item.link_type === 2 ? '_blank' : '_self'"
            class="item-link"
          >
            <div class="item-thumbnail">
              <img
                v-if="item.thumbnail"
                :src="item.thumbnail"
                :alt="item.banner_title || item.promotion_name"
                class="w-full h-full object-cover"
              />
            </div>
            <span class="item-title">
              {{ item.banner_title || item.promotion_name }}
            </span>
          </AtomsLocaleLink>
        </li>
      </ul>
    </div>
  </div>
</template>

<style scoped>
.empty-game + main .event-navigation {
  @apply mt-[var(--scroll-position,48px)];
}
.event-navigation {
  @apply fixed top-0 left-0 bottom-0 mt-[calc(var(--scroll-position,48px)+48px)] md:mt-[calc(var(--scroll-position,64px)+64px)] z-[100] transition-transform duration-300 ease-in-out;
}
.navigation-wrapper {
  @apply relative h-full p-3 sm:p-5 sm:pr-3
  md:p-8 md:pt-6 md:pr-4;
}
.navigation-list {
  @apply flex flex-col gap-4 h-full overflow-y-auto rounded-[20px] p-4 bg-[rgba(25,25,25,0.5)] shadow-[0_2px_4px_0_rgba(0,0,0,0.06)] backdrop-blur-[25px] transition-opacity duration-300 ease-in-out;
}
.item-thumbnail {
  @apply overflow-hidden relative w-[148px] h-[75px] rounded-[10px]
  before:content-[''] before:absolute before:top-0 before:left-0 before:w-full before:h-full before:rounded-[10px] before:border-[1px] before:border-white/[0.08]
  after:content-[''] after:absolute after:top-0 after:left-0 after:w-full after:h-full after:bg-[lightgray]/50;
}
.item-title {
  @apply block mt-2 text-center line-clamp-2 text-[#ebebeb] text-[14px] font-normal leading-[20px] tracking-[-0.42px] opacity-50;
}
.btn-control {
  @apply absolute top-3 right-[-40px]
  bg-black/20 shadow-[0_1.667px_3.333px_0_rgba(0,0,0,0.06)] backdrop-blur-[12.5px] 
  sm:top-5 md:top-6 md:right-[-48px];
}

.event-navigation.is-closed {
  @apply translate-x-[calc(-100%+20px)] sm:translate-x-[calc(-100%+40px)];
}
.event-navigation.is-closed .btn-control {
  @apply rotate-180;
}
.event-navigation.is-closed .navigation-list {
  @apply pointer-events-none opacity-0;
}

.router-link-active .item-thumbnail,
.item-link:hover .item-thumbnail {
  @apply before:border-[var(--primary)] after:opacity-0;
}
.router-link-active .item-title,
.item-link:hover .item-title {
  @apply text-[var(--primary)] opacity-100;
}
</style>
