<template>
  <div>
    <!-- xl -->
    <div class="w-full border-b border-border">
      <UiContainer class="relative">
        <div class="hidden h-[120px] w-full items-center gap-[145px] xl:flex">
          <ul class="flex items-center justify-between gap-5 whitespace-nowrap w-full">
            <li v-for="(item, index) in menuStore.menu" :key="item.id" :class="[
              'hover:text-accent-green-light dark:hover:text-accent-green-dark cursor-pointer text-lg transition-all text-inter',
              route.params.id == item.id.toString()
                ? 'text-accent-green-light dark:text-accent-green-dark font-bold underline'
                : false,
            ]" @click="menuStore.navigateToItem(item)">
              0{{ index + 1 }} <br>
              {{ item.front_menu_lang[0].name }}
            </li>
          </ul>
          <ClientOnly v-if="!colorMode?.forced">
            <img class="cursor-pointer" :src="isDark ? '/logo-dark.svg' : '/logo.svg'" alt="logo"
                 @click="menuStore.navigateToItem()">
          </ClientOnly>
          <div class="w-full flex items-center justify-between">
            <div class="flex items-center gap-[30px]">
              <div>
                <i v-if="!userStore.isLogged" class="uil uil-user text-[31px] cursor-pointer"
                   @click="menuStore.navigateToItem(menuStore.menuItems?.find((item:any) => item.id === 11))"></i>
                <div v-else class="py-[6px] px-3 border border-block rounded-sm">
                  {{ userStore.user }}
                </div>
              </div>
              <CartPopup />
            </div>
            <div class="flex">
              <LangSwitcher />
              <CountryCurrencySelector />
            </div>
            <ThemeSwitcher />
            <button :class="[
              'cursor-pointer transition-all text-inter whitespace-nowrap',
              route.params.id == '5'
                ? 'text-accent-green-light dark:text-accent-green-dark font-bold pb-1 border-b-2'
                : 'hover:bg-button-hover bg-button text-white font-medium rounded-xl px-6 py-3',
            ]" @click="menuStore.navigateToShop">
              {{ $t("eshop") }}
            </button>
          </div>
        </div>
      </UiContainer>
    </div>

    <!-- md -->
    <div class="hidden w-full md:flex md:flex-col xl:hidden items-center justify-center">
      <div class="w-full border-border border-b">
        <UiContainer class="h-[116px] flex items-center justify-between">
          <ClientOnly v-if="!colorMode?.forced">
            <img class="cursor-pointer" :src="isDark ? '/logo-dark.svg' : '/logo.svg'" alt="logo"
                 @click="menuStore.navigateToItem()">
          </ClientOnly>
          <div class="flex items-center gap-6">
            <div class="flex items-center gap-[30px]">
              <div>
                <i v-if="!userStore.isLogged" class="uil uil-user text-[31px] cursor-pointer" @click="
                  menuStore.navigateToItem(menuStore.menuItems?.find((item:any) => item.id === 11)) " />
                <div v-else class="py-[6px] px-3 border border-block rounded-sm">
                  {{ userStore.user }}
                </div>
              </div>
              <CartPopup />
            </div>
            <div class="flex">
              <LangSwitcher />
              <CountryCurrencySelector />
            </div>
            <ThemeSwitcher />
            <i variant="subtle" block class="uil uil-apps text-[33px] cursor-pointer" @click="open = !open" />
          </div>
        </UiContainer>
      </div>
      <UCollapsible v-model:open="open" :ui="{ content: 'w-full' }" class="w-full">
        <template #content>
          <div class="w-full border-border border-b pt-6 pb-8">
            <UiContainer class="flex flex-col gap-[30px]">
              <div v-for="(item, index) in menuStore.menu" :key="index" :class="[
                'flex items-center justify-between transition-all hover:text-accent-green-light dark:hover:text-accent-green-dark cursor-pointer',
                route.params.slug === item.front_menu_lang[0].link_rewrite
                  && 'text-accent-green-light dark:text-accent-green-dark font-bold underline',
              ]" @click="
                () => {
                  menuStore.navigateToItem(item);
                  open = false;
                }
              ">
                <div class="leading-[70%] text-inter">
                  <span class="mr-4">0{{ index + 1 }}</span>
                  {{ item.front_menu_lang[0].name }}
                </div>
                <!-- <i class="uil uil-arrow-up-right text-[35px]"></i> -->
                <svg width="20" height="20" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M25.1274 1.87258C25.1274 1.3203 24.6797 0.872582 24.1274 0.872584L15.1274 0.872583C14.5751 0.872583 14.1274 1.3203 14.1274 1.87258C14.1274 2.42487 14.5751 2.87258 15.1274 2.87258L23.1274 2.87258L23.1274 10.8726C23.1274 11.4249 23.5751 11.8726 24.1274 11.8726C24.6797 11.8726 25.1274 11.4249 25.1274 10.8726L25.1274 1.87258ZM1.5 24.5L2.20711 25.2071L24.8345 2.57969L24.1274 1.87258L23.4203 1.16548L0.792893 23.7929L1.5 24.5Z"
                    fill="currentColor" />
                </svg>
              </div>
            </UiContainer>
          </div>
        </template>
      </UCollapsible>
    </div>

    <!-- sm -->
    <div class="hidden w-full items-center justify-between sm:flex sm:flex-col md:hidden">
      <div class="w-full border-border border-b">
        <UiContainer class="h-[84px] flex items-center justify-between">
          <ClientOnly v-if="!colorMode?.forced">
            <img class="cursor-pointer" :src="isDark ? '/logo-dark.svg' : '/logo.svg'" alt="logo"
                 @click="menuStore.navigateToItem()">
          </ClientOnly>
          <div class="flex items-center gap-6">
            <div class="flex items-center gap-[30px]">
              <div>
                <i v-if="!userStore.isLogged" class="uil uil-user text-[31px] cursor-pointer" @click="
                  menuStore.navigateToItem(menuStore.menuItems?.find((item:any) => item.id === 11)) " />
                <div v-else class="py-[6px] px-3 border border-block rounded-sm">
                  {{ userStore.user }}
                </div>
              </div>
              <CartPopup />
            </div>
            <i variant="subtle" block class="uil uil-apps text-[30px] cursor-pointer" @click="open = !open" />
          </div>
        </UiContainer>
      </div>
      <UCollapsible v-model:open="open" :ui="{ content: 'w-full' }" class="w-full">
        <template #content>
          <div class="w-full border-border border-b pt-6 pb-8">
            <UiContainer class="flex flex-col gap-[30px]">
              <div v-for="(item, index) in menuStore.menu" :key="index" :class="[
                'flex items-center justify-between transition-all hover:text-accent-green-light dark:hover:text-accent-green-dark cursor-pointer',
                route.params.slug === item.front_menu_lang[0].link_rewrite
                  && 'text-accent-green-light dark:text-accent-green-dark font-bold underline',
              ]" @click="
                () => {
                  menuStore.navigateToItem(item);
                  open = false;
                }
              ">
                <div class="leading-[70%] text-inter">
                  <span class="mr-4">0{{ index + 1 }}</span>
                  {{ item.front_menu_lang[0].name }}
                </div>
                <!-- <i class="uil uil-arrow-up-right text-[35px]"></i> -->
                <svg width="20" height="20" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M25.1274 1.87258C25.1274 1.3203 24.6797 0.872582 24.1274 0.872584L15.1274 0.872583C14.5751 0.872583 14.1274 1.3203 14.1274 1.87258C14.1274 2.42487 14.5751 2.87258 15.1274 2.87258L23.1274 2.87258L23.1274 10.8726C23.1274 11.4249 23.5751 11.8726 24.1274 11.8726C24.6797 11.8726 25.1274 11.4249 25.1274 10.8726L25.1274 1.87258ZM1.5 24.5L2.20711 25.2071L24.8345 2.57969L24.1274 1.87258L23.4203 1.16548L0.792893 23.7929L1.5 24.5Z"
                    fill="currentColor" />
                </svg>
              </div>
              <div class="flex items-center justify-between">
                <p class="leading-[70%] text-inter">
                  {{ $t("change_language") }}
                </p>
                <LangSwitcher />
              </div>
              <div class="flex items-center justify-between">
                <p class="leading-[70%] text-inter">
                  {{ $t("change_currency_and_country") }}
                </p>
                <CountryCurrencySelector />
              </div>
              <div class="flex items-center justify-between">
                <p class="leading-[70%] text-inter">
                  {{ $t("change_theme") }}
                </p>
                <ThemeSwitcher />
              </div>
            </UiContainer>
          </div>
        </template>
      </UCollapsible>
    </div>

    <!-- smallest one -->
    <div class="w-full items-center justify-between flex flex-col sm:hidden">
      <div class="w-full border-border border-b">
        <UiContainer class="h-[84px] flex items-center justify-between">
          <ClientOnly v-if="!colorMode?.forced">
            <img class="cursor-pointer" :src="isDark ? '/logo-dark.svg' : '/logo.svg'" alt="logo"
                 @click="menuStore.navigateToItem()">
          </ClientOnly>
          <div class="flex items-center gap-6">
            <div>
              <i class="uil uil-user text-[30px] cursor-pointer" @click=" !userStore.isLogged && menuStore.navigateToItem(menuStore.menuItems?.find((item:any) => item.id === 11)) " />
            </div>
            <CartPopup />
            <i variant="subtle" block class="uil uil-apps text-[30px] cursor-pointer" @click="open = !open" />
          </div>
        </UiContainer>
      </div>
      <UCollapsible v-model:open="open" :ui="{ content: 'w-full' }" class="w-full">
        <template #content>
          <div class="w-full border-border border-b pt-6 pb-8">
            <UiContainer class="flex flex-col gap-[30px]">
              <div v-for="(item, index) in menuStore.menu" :key="index" :class="[
                'flex items-center justify-between transition-all hover:text-accent-green-light dark:hover:text-accent-green-dark cursor-pointer',
                route.params.slug === item.front_menu_lang[0].link_rewrite
                  && 'text-accent-green-light dark:text-accent-green-dark font-bold underline',
              ]" @click="
                () => {
                  menuStore.navigateToItem(item);
                  open = false;
                }
              ">
                <div class="leading-[70%] text-inter">
                  <span class="mr-4">0{{ index + 1 }}</span>
                  {{ item.front_menu_lang[0].name }}
                </div>
                <!-- <i class="uil uil-arrow-up-right text-[35px]"></i> -->
                <svg width="20" height="20" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M25.1274 1.87258C25.1274 1.3203 24.6797 0.872582 24.1274 0.872584L15.1274 0.872583C14.5751 0.872583 14.1274 1.3203 14.1274 1.87258C14.1274 2.42487 14.5751 2.87258 15.1274 2.87258L23.1274 2.87258L23.1274 10.8726C23.1274 11.4249 23.5751 11.8726 24.1274 11.8726C24.6797 11.8726 25.1274 11.4249 25.1274 10.8726L25.1274 1.87258ZM1.5 24.5L2.20711 25.2071L24.8345 2.57969L24.1274 1.87258L23.4203 1.16548L0.792893 23.7929L1.5 24.5Z"
                    fill="currentColor" />
                </svg>
              </div>
              <div class="flex items-center justify-between">
                <p class="leading-[70%] text-lg text-inter">
                  {{ $t("change_language") }}
                </p>
                <LangSwitcher />
              </div>
              <div class="flex items-center justify-between">
                <p class="leading-[70%] text-lg text-inter">
                  {{ $t("change_currency_and_country") }}
                </p>
                <CountryCurrencySelector />
              </div>
              <div class="flex items-center justify-between">
                <p class="leading-[70%] text-lg text-inter">
                  {{ $t("change_theme") }}
                </p>
                <ThemeSwitcher />
              </div>
            </UiContainer>
          </div>
        </template>
      </UCollapsible>
    </div>
  </div>
</template>

<script lang="ts" setup>
import CartPopup from './CartPopup.vue'
import CountryCurrencySelector from './CountryCurrencySelector.vue'
import LangSwitcher from './LangSwitcher.vue'

const userStore = useUserStore()
const open = ref(false)
const colorMode = useColorMode()
const menuStore = useMenuStore()
const checkoutStore = useCheckoutStore()

await checkoutStore.getUserCart()

const route = useRoute()
const isDark = computed({
  get() {
    return colorMode.value === 'dark'
  },
  set(_isDark) {
    colorMode.preference = _isDark ? 'dark' : 'light'
  },
})
</script>
