<script setup lang="ts">
import log from "loglevel";

import IconLogout from "@/components/icons/IconLogout.vue";
import IconSettings from "@/components/icons/IconSettings.vue";
import MobileMenu from "@/components/MobileMenu.vue";
import ItDropdown from "@/components/ui/ItDropdown.vue";
import { useAppStore } from "@/stores/app";
import { useCourseSessionsStore } from "@/stores/courseSessions";
import { useUserStore } from "@/stores/user";
import type { DropdownListItem } from "@/types";
import type { Component } from "vue";
import { onMounted, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";

type DropdownActions = "logout" | "settings";

interface DropdownData {
  action: DropdownActions;
}

log.debug("MainNavigationBar created");

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
const appStore = useAppStore();
const courseSessionsStore = useCourseSessionsStore();

const { t } = useI18n();
const state = reactive({ showMenu: false });

function toggleNav() {
  state.showMenu = !state.showMenu;
}

function inCourse() {
  return route.path.startsWith("/course/");
}

function inCockpit() {
  const regex = new RegExp("/course/[^/]+/cockpit");
  return regex.test(route.path);
}

function inLearningPath() {
  const regex = new RegExp("/course/[^/]+/learn");
  return regex.test(route.path);
}

function inCompetenceProfile() {
  const regex = new RegExp("/course/[^/]+/competence");
  return regex.test(route.path);
}

function inMediaLibrary() {
  return route.path.startsWith("/media/");
}

function handleDropdownSelect(data: DropdownData) {
  switch (data.action) {
    case "settings":
      router.push("/profile");
      break;
    case "logout":
      userStore.handleLogout();
      break;
    default:
      console.log("No action");
  }
}

function logout() {
  userStore.handleLogout();
}

onMounted(() => {
  log.debug("MainNavigationBar mounted");
  if (userStore.loggedIn) {
    // fixme: only when i'm logged in? should this be handled in the store?
    // courseSessionsStore.loadCourseSessionsData();
  }
});

const profileDropdownData: DropdownListItem[] = [
  {
    title: t("mainNavigation.settings"),
    icon: IconSettings as Component,
    data: {
      action: "settings",
    },
  },
  {
    title: t("mainNavigation.logout"),
    icon: IconLogout as Component,
    data: {
      action: "logout",
    },
  },
];
</script>

<template>
  <div>
    <Teleport to="body">
      <MobileMenu
        :show="state.showMenu"
        :course-session="courseSessionsStore.courseSessionForRoute"
        @closemodal="state.showMenu = false"
      />
    </Teleport>
    <Transition name="nav">
      <div v-if="appStore.showMainNavigationBar" class="navigation bg-blue-900">
        <nav class="px-8 py-2 mx-auto lg:flex lg:justify-start lg:items-center lg:py-4">
          <div class="flex items-center justify-between">
            <div class="flex items-center">
              <router-link to="/" class="flex">
                <it-icon-vbv class="h-8 w-16 mr-3 -mt-6 -ml-3" />
              </router-link>
              <router-link to="/" class="flex">
                <div class="text-white text-2xl pr-10 pl-3 ml-1 border-l border-white">
                  myVBV
                </div>
              </router-link>
            </div>

            <div class="flex items-center lg:hidden">
              <router-link
                v-if="userStore.loggedIn"
                to="/messages"
                class="nav-item flex flex-row items-center"
                data-cy="messages-link"
              >
                <it-icon-bell class="w-6 h-6 mr-6" />
              </router-link>
              <router-link
                v-if="userStore.loggedIn"
                to="/messages"
                class="nav-item flex flex-row items-center"
                data-cy="messages-link"
              >
                <it-icon-persons class="w-6 h-6 mr-6" />
              </router-link>
              <!-- Mobile menu button -->
              <div class="flex" @click="toggleNav">
                <button
                  type="button"
                  class="w-8 h-8 text-white hover:text-sky-500 focus:outline-none focus:text-sky-500"
                >
                  <it-icon-menu class="h-8 w-8" />
                </button>
              </div>
            </div>
          </div>

          <!-- Mobile Menu open: "block", Menu closed: "hidden" -->
          <div
            v-if="appStore.userLoaded && appStore.routingFinished && userStore.loggedIn"
            :class="state.showMenu ? 'flex' : 'hidden'"
            class="flex-auto mt-8 lg:flex lg:space-y-0 lg:flex-row lg:items-center lg:space-x-10 lg:mt-0"
          >
            <!--            <router-link-->
            <!--              v-if="inCourse() && courseSessionsStore.courseSessionForRoute"-->
            <!--              :to="`${courseSessionsStore.courseSessionForRoute.course_url}/cockpit`"-->
            <!--              class="nav-item"-->
            <!--              :class="{ 'nav-item&#45;&#45;active': inCockpit() }"-->
            <!--            >-->
            <!--              Cockpit-->
            <!--            </router-link>-->

            <router-link
              v-if="
                inCourse() &&
                courseSessionsStore.courseSessionForRoute &&
                courseSessionsStore.hasCockpit
              "
              :to="`${courseSessionsStore.courseSessionForRoute.course_url}/cockpit`"
              class="nav-item"
              :class="{ 'nav-item--active': inCockpit() }"
            >
              {{ $t("cockpit.title") }}
            </router-link>

            <router-link
              v-if="inCourse() && courseSessionsStore.courseSessionForRoute"
              :to="courseSessionsStore.courseSessionForRoute.learning_path_url"
              class="nav-item"
              :class="{ 'nav-item--active': inLearningPath() }"
            >
              {{ $t("general.learningPath") }}
            </router-link>

            <router-link
              v-if="inCourse() && courseSessionsStore.courseSessionForRoute"
              :to="courseSessionsStore.courseSessionForRoute.competence_url"
              class="nav-item"
              :class="{ 'nav-item--active': inCompetenceProfile() }"
            >
              {{ $t("competences.title") }}
            </router-link>

            <div class="hidden lg:block flex-auto"></div>
            <a
              class="nav-item"
              target="_blank"
              href="https://bildung.vbv.ch/ilp/pages/catalogsearch.jsf"
            >
              Shop
            </a>
            <router-link
              to="/media/versicherungsvermittler-in-media"
              class="nav-item"
              :class="{ 'nav-item--active': inMediaLibrary() }"
              data-cy="medialibrary-link"
            >
              {{ $t("mediaLibrary.title") }}
            </router-link>
            <router-link
              to="/messages"
              class="nav-item flex flex-row items-center"
              data-cy="messages-link"
            >
              <it-icon-bell class="w-6 h-6 mr-6" />
            </router-link>
            <router-link
              to="/messages"
              class="nav-item flex flex-row items-center"
              data-cy="messages-link"
            >
              <it-icon-persons class="w-6 h-6 mr-6" />
            </router-link>
            <div v-if="userStore.loggedIn" class="nav-item flex items-center">
              <ItDropdown
                :button-classes="[]"
                :list-items="profileDropdownData"
                :align="'right'"
                @select="handleDropdownSelect"
              >
                <div v-if="userStore.avatar_url">
                  <img
                    class="inline-block h-8 w-8 rounded-full"
                    :src="userStore.avatar_url"
                    alt=""
                  />
                </div>
                <div v-else>
                  {{ userStore.getFullName }}
                </div>
              </ItDropdown>
            </div>
            <div v-else><a class="" href="/login">Login</a></div>
          </div>
        </nav>
      </div>
    </Transition>
  </div>
</template>

<style lang="postcss" scoped>
.nav-item {
  @apply text-white hover:text-sky-500 text-2xl font-bold lg:text-base lg:font-normal;
}

.nav-item--active {
  @apply underline underline-offset-[21px] decoration-sky-500 decoration-4;
}

.nav-enter-active,
.nav-leave-active {
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.nav-enter-from,
.nav-leave-to {
  opacity: 0;
  transform: translateY(-80px);
}
</style>
