<template>
  <q-layout view="hHh lpr lFf">
    <q-header elevated class="bg-primary text-white">
      <q-toolbar>
        <!-- Button um Navigationsleiset ein und auszublenden. Nötig bei Desktop? -->
        <q-btn
          v-if="!leftDrawerOpen"
          dense
          flat
          round
          icon="menu"
          @click="leftDrawerOpen = !leftDrawerOpen"
        />

        <q-toolbar-title>
          <router-link :to="{ name: 'dashboard' }" style="text-decoration: none; color: inherit">
            <q-avatar rounded>
              <img src="flaschengeist-logo-white.svg" />
            </q-avatar>
            <span class="gt-xs"> Flaschengeist </span>
          </router-link>
        </q-toolbar-title>

        <!-- Hier kommen die Shortlinks hin -->
        <div>
          <short-cut-link
            v-for="(shortcut, index) in plugins.shortcuts"
            :key="'shortcut' + index"
            :link="shortcut.link"
            :icon="shortcut.icon"
            :permissions="shortcut.permissions"
          />
        </div>
        <q-btn flat round dense icon="mdi-exit-to-app" @click="logout()" />
      </q-toolbar>
    </q-header>

    <q-drawer
      v-model="leftDrawerOpen"
      show-if-above
      side="left"
      bordered
      :mini="leftDrawerMini"
      @click.capture="leftDrawerClicker"
    >
      <!-- Plugins -->
      <q-list>
        <essential-link
          v-for="(link, index) in plugins.mainLinks"
          :key="'plugin' + index"
          :title="link.title"
          :link="link.link"
          :icon="link.icon"
          :permissions="link.permissions"
        />
      </q-list>
      <q-separator />

      <!-- Plugin functions -->
      <!-- <router-view name="plugin-nav" /> -->
      <q-list>
        <essential-link
          v-for="(link, index) in pluginChildLinks"
          :key="'childPlugin' + index"
          :title="link.title"
          :link="link.link"
          :icon="link.icon"
          :permissions="link.permissions"
        />
      </q-list>

      <div class="q-mini-drawer-hide absolute" style="top: 15px; right: -11px">
        <q-btn
          size="sm"
          dense
          round
          unelevated
          color="secondary"
          icon="chevron_left"
          @click="leftDrawerMini = true"
        />
      </div>

      <q-separator />

      <essential-link
        v-for="(link, index) in links"
        :key="'main' + index"
        :title="link.title"
        :link="link.link"
        :icon="link.icon"
        :permissions="link.permissions"
      />
    </q-drawer>
    <q-page-container>
      <router-view />
    </q-page-container>
  </q-layout>
</template>

<script lang="ts">
import EssentialLink from 'components/navigation/EssentialLink.vue';
import ShortCutLink from 'components/navigation/ShortCutLink.vue';
import { Screen, Loading } from 'quasar';
import { defineComponent, ref, inject, computed } from 'vue';
import { FG_Plugin } from 'src/plugins';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';

const links = [
  {
    name: 'about',
    title: 'Über Flaschengeist',
    link: 'about',
    icon: 'mdi-information',
  },
];

const shortcuts = [
  {
    link: 'about',
    icon: 'mdi-information',
  },
  {
    link: 'user',
    icon: 'mdi-account',
  },
  {
    link: 'user-plugin1',
    icon: 'mdi-account-plus',
  },
];

export default defineComponent({
  name: 'MainLayout',
  components: { EssentialLink, ShortCutLink },
  setup() {
    const route = useRoute();
    const store = useStore();
    const plugins = inject<FG_Plugin.LoadedPlugins>('flaschengeistPlugins');
    const leftDrawer = ref(false);

    const leftDrawerOpen = ref(
      computed({
        get: () => (leftDrawer.value || Screen.gt.sm ? true : false),
        set: (val: boolean) => (leftDrawer.value = val),
      })
    );
    const leftDrawerMini = ref(false);

    function leftDrawerClicker() {
      if (leftDrawerMini.value) {
        leftDrawerMini.value = false;
      }
    }

    const pluginChildLinks = computed(() => {
      const link: FG_Plugin.PluginMainLink | undefined = plugins?.mainLinks.find(
        (plugin: FG_Plugin.PluginMainLink) => {
          if (route.matched.length > 1) {
            return plugin.name == route.matched[1].name;
          }
        }
      );

      if (link == undefined) {
        return [];
      } else {
        return link.children;
      }
    });

    function logout() {
      Loading.show({ message: 'Session wird abgemeldet' });
      store.dispatch('sessions/logout').finally(() => {
        Loading.hide();
      });
    }

    return {
      leftDrawerOpen,
      leftDrawerMini,
      leftDrawerClicker,
      links,
      pluginChildLinks,
      shortcuts,
      logout,
      plugins,
    };
  },
});
</script>
