<script setup lang="ts">
import MediaShowcase from '@/components/MediaShowcase.vue'
import ContinueWatching from '@/components/NextUp.vue'
import LatestMedia from '@/components/LatestMedia.vue'
import type { View, ViewList } from '@/jfapi'
import { onBeforeMount, ref } from 'vue'
import jfapi from '@/jfapi'

const views = ref<View[]>()
onBeforeMount(async () => {
  const viewlist = ref<ViewList | null>()
  const userid = localStorage.getItem('jf_userid')
  const host = localStorage.getItem('jf_host')
  console.log('Host =>', host)
  viewlist.value = await jfapi.LoadViews(userid ? userid : '')
  if (window.location.pathname.toLowerCase() !== '/login') {
    if (host === null) {
      window.location.href = '/login'
    } else if (viewlist.value === null) {
      window.location.href = '/login'
    }
  }
  localStorage.setItem('jf_views', JSON.stringify(viewlist.value ? viewlist.value.Items : ''))
  views.value = viewlist.value ? viewlist.value.Items : undefined
  console.log(views.value)
})
</script>

<template>
  <main>
    <MediaShowcase />
    <div class="home-nav">
      <div class="home-nav-item active">Home</div>
      <div class="home-nav-item" v-for="view in views" :key="view.Id">{{ view.Name }}</div>
    </div>
    <div class="media">
      <ContinueWatching />
      <!-- <MediaScroller title="Latest Movies" :items="LatestMovies" />
    <MediaScroller title="Latest Anime" :items="LatestAnime" /> -->
      <LatestMedia v-for="view of views" :view="view" :key="view.Id" />
    </div>
  </main>
</template>

<style scoped>
.home-nav {
  display: flex;
  justify-content: center;
  margin-top: 1.5em;
  gap: 3em;
  align-items: center;
}
.home-nav-item:hover {
  cursor: pointer;
  color: var(--color-text-faded);
}
.home-nav-item.active {
  padding: 0.4em 1.5em;
  background-color: var(--color-text);
  border-radius: 15px;
  color: var(--color-text-dark);
  font-weight: 600;
}
.home-nav-item.active:hover {
  background-color: var(--color-text-faded);
}
.media {
  margin: 0 10%;
}
</style>
