<script lang="ts">
import { ref, type Ref } from 'vue'
import StreamPreviewVue from '@/components/StreamPreview.vue'
import ErrorMessage from '@/components/ErrorMessage.vue'
import LoadingScreen from '@/components/LoadingScreen.vue'

export default {
  async setup() {
    const protocol = import.meta.env.VITE_HTTPS === 'true' ? 'https://' : 'http://'

    let data: Ref<any | undefined> = ref()
    let frontend_url = protocol + import.meta.env.VITE_INSTANCE_DOMAIN

    return {
      data,
      protocol,
      frontend_url,
      filterTags: '',
      following: ref([])
    }
  },
  methods: {
    abbreviate(text: number) {
      return Intl.NumberFormat('en-US', {
        //@ts-ignore
        notation: 'compact',
        maximumFractionDigits: 1
      }).format(text)
    },

    filterSearches(toFilter: string) {
      const categories = this.$refs.categoryItem
      const wantedTags: string[] = toFilter.split(',').filter((v) => v.toLowerCase())

      for (let category of categories as any) {
        let tagElements = category.getElementsByTagName('span')
        let tags = []

        for (let tag of tagElements) {
          tags.push(tag.innerText.toLowerCase())
        }

        // Create sets from the arrays
        const [set1, set2] = [new Set(wantedTags), new Set(tags)]
        const common = [...set1].filter((x) => set2.has(x))

        if (common.length === wantedTags.length) {
          category.style.display = ''
        } else if (wantedTags[0] === '') {
          category.style.display = ''
          console.log('ok')
        } else {
          category.style.display = 'none'
        }
      }
    },
    getNextCategory() {
      window.onscroll = async () => {
        let bottomOfWindow =
          document.documentElement.scrollTop + window.innerHeight ===
          document.documentElement.offsetHeight
        if (bottomOfWindow && this.data) {
          const cursor = this.data[this.data.length - 1].cursor
          if (!cursor) return
          const res = await fetch(
            `${this.protocol}${import.meta.env.VITE_BACKEND_DOMAIN}/api/discover/?cursor=${cursor}`
          )
          if (!res.ok) {
            throw new Error('Failed to fetch data')
          }
          const data = await res.json()

          for (let category of data) {
            this.data.push(category)
          }
        }
      }
    }
  },
  async mounted() {
    this.getNextCategory()

    let following = localStorage.getItem('following')
    if (following) {
      this.following = JSON.parse(following)
    } else {
      this.following = []
    }

    try {
      const res = await fetch(`${this.protocol}${import.meta.env.VITE_BACKEND_DOMAIN}/api/discover`)
      const rawData = await res.json()
      if (rawData.status === "ok") {
        this.data = rawData.data
      } else {
        this.data = { status: 'error' }
      }

    } catch (error) {
      console.error(error)
      this.data = { status: 'error' }
    }
  },
  components: {
    StreamPreviewVue,
    ErrorMessage,
    LoadingScreen
  }
}
</script>

<template>
  <loading-screen v-if="!data"></loading-screen>
  <error-message v-else-if="data.status === 'error'"></error-message>

  <div v-else class="max-w-5xl mx-auto">
    <div v-if="following.length > 0" class="p-2 text-white">
      <h1 class="font-bold text-5xl">Following</h1>
      <p class="text-xl">Streamers you follow</p>
      <ul class="flex overflow-x-scroll flex-nowrap h-80 space-x-1">
        <li
          v-for="streamer in following"
          :key="streamer"
          class="inline-block hover:scale-105 transition-transform"
        >
          <stream-preview-vue :name="streamer"></stream-preview-vue>
        </li>
      </ul>
    </div>

    <div class="p-2">
      <h1 class="font-bold text-5xl text-white">Discover</h1>
      <p class="text-xl text-white">Sort through popular categories</p>

      <div class="pt-5 inline-flex text-white">
        <p class="mr-2 font-bold text-white">Filter by tag</p>
        <form class="relative">
          <label for="searchBar" class="hidden">Search</label>
          <v-icon name="io-search-outline" class="absolute my-auto inset-y-0 left-2"></v-icon>
          <input
            type="text"
            id="searchBar"
            name="searchBar"
            placeholder="Search"
            v-model="filterTags"
            @keyup="filterSearches(filterTags)"
            class="rounded-md p-1 pl-8 text-black bg-neutral-500 placeholder:text-white"
          />
        </form>
      </div>
    </div>

    <ul ref="categoryList">
      <li
        v-for="category in data"
        :key="category"
        ref="categoryItem"
        class="inline-flex m-2 hover:scale-105 transition-transform"
      >
        <div class="bg-ctp-crust w-40 lg:w-[13.5rem] md:w-[13.5rem] rounded-lg">
          <router-link :to="`/game/${category.name}`">
            <img :src="category.image" class="rounded-lg rounded-b-none" />
          </router-link>

          <div class="p-2">
            <div>
              <p class="font-bold text-white text-xl sm:text-base md:text-xl">{{ category.displayName }}</p>
              <p class="text-sm text-white">{{ abbreviate(category.viewers) }} viewers</p>
            </div>

            <ul class="h-8 overflow-hidden">
              <li v-for="tag in category.tags" :key="tag" class="inline-flex">
                <span
                  class="p-2.5 py-1.5 bg-ctp-surface0 rounded-md m-0.5 text-xs font-bold text-white"
                  >{{ tag }}</span
                >
              </li>
            </ul>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>
