<!--
 * @Description: 右上角工具栏
 * @Author: ZCY
 * @Date: 2022年12月7日09:49:19
-->
<template>
  <div id="toolbars" class="toolbars">
    <!--    <div -->
    <!--      class="toolbars-svgIcon" -->
    <!--      :class="{ 'toolbars-svgIcon-dark': config.navStyle === 'dark' }" -->
    <!--      :title="isFullscreen ? '退出全屏' : '全屏'" -->
    <!--      @click="toggleFullscreen(!isFullscreen)" -->
    <!--    > -->
    <!--      <Icon -->
    <!--        v-if="userTools.fullscreen" -->
    <!--        :icon="isFullscreen ? 'ant-design:fullscreen-exit-outlined' : 'ant-design:fullscreen-outlined'" -->
    <!--        width="15" -->
    <!--      /> -->
    <!--    </div> -->
    <!--    <div class="navBar-vertical-line" :style="{ backgroundColor: seqLineColor }" /> -->
    <slot name="rightTool" />
    <slot name="user">
      <MenuOverlay
        v-model:open="userMenuOpen"
        :cover="false"
        align="right"
        trigger="hover"
        :overlay-style="{ backgroundColor: panelBg, boxShadow: '#ddd -1px 3px 4px' }"
      >
        <div
          class="toolbars-dropdown-content"
          :class="{ 'toolbars-svgIcon-dark': config.navStyle === 'dark' }"
          @click.prevent
        >
          <a-avatar :size="28" :src="avatarUrl">
            <template v-if="!avatarUrl">
              {{ username?.slice(0, 1) || 'U' }}
            </template>
          </a-avatar>
          <u-icon icon="carbon:caret-down" />
        </div>
        <template #overlay>
          <div
            style="width: 270px; padding: 8px 0"
            :style="{ color: panelColor }"
            @mouseover="handleMenuMouseover"
            @mouseout="hoverMenuKey = null"
          >
            <div class="toolbars-menuOverlay-infoWrapper">
              <a-avatar :size="35" :src="avatarUrl" style="grid-row: 2 span">
                <template v-if="!avatarUrl">
                  {{ username?.slice(0, 1) || 'U' }}
                </template>
              </a-avatar>
              <h4>{{ username }}</h4>
              <p>账户ID：{{ userId }}</p>
            </div>
            <a-divider :style="{ margin: '8px 0', backgroundColor: seqLineColor }" />
            <div
              v-if="config.userMenu?.split(',').includes('account')"
              class="userMenu-item"
              data-id="account"
              :style="{ backgroundColor: hoverMenuKey === 'account' ? themeColor.primary : 'transparent' }"
              @click="navigateTo('/account/center')"
            >
              <u-icon icon="carbon:user" style="pointer-events: none" />
              <span class="toolbars-dropdown-menu-span" v-text="'个人中心'" />
            </div>
            <div
              v-if="config.userMenu?.split(',').includes('settings')"
              class="userMenu-item"
              data-id="settings"
              :style="{ backgroundColor: hoverMenuKey === 'settings' ? themeColor.primary : 'transparent' }"
              @click="navigateTo('/account/settings')"
            >
              <u-icon icon="carbon:settings" />
              <span class="toolbars-dropdown-menu-span" v-text="'平台设置'" />
            </div>
            <div
              v-if="config.userMenu?.split(',').includes('app')"
              class="userMenu-item"
              data-id="app"
              :style="{ backgroundColor: hoverMenuKey === 'app' ? themeColor.primary : 'transparent' }"
              @click="onAppDownload"
            >
              <u-icon icon="carbon:app" />
              <span class="toolbars-dropdown-menu-span" v-text="'App 下载'" />
            </div>
            <a-divider v-if="config.userMenu" :style="{ margin: '8px 0', backgroundColor: seqLineColor }" />
            <div class="userMenu-item-custom">
              <div style="display: flex; align-items: center">
                <u-icon icon="mdi:theme-light-dark" />
                <span class="toolbars-dropdown-menu-span" v-text="'菜单风格'" />
              </div>
              <a-radio-group
                v-model:value="navStyle"
                :options="navStyleOpt"
                size="small"
                option-type="button"
                button-style="solid"
                @change="handlenavStyleChange"
              />
            </div>
            <a-divider :style="{ margin: '8px 0', backgroundColor: seqLineColor }" />
            <div
              class="userMenu-item"
              data-id="exit"
              :style="{ backgroundColor: hoverMenuKey === 'exit' ? themeColor.primary : 'transparent' }"
              @click="onExit"
            >
              <u-icon icon="carbon:exit" />
              <span class="toolbars-dropdown-menu-span" v-text="'退出登录'" />
            </div>
          </div>
        </template>
      </MenuOverlay>
    </slot>
    <slot name="leftTool" />
  </div>
</template>

<script lang="ts" setup>
import type { Ref } from 'vue';
import { computed, inject, ref, watch } from 'vue';
import { arrayToOption, colorDarken, colorLighten, isDarkColor } from '@unyu/utils';
import { Icon as UIcon } from '@iconify/vue';
import type {LayoutConfig, NavStyle, ThemeColor} from '../../types';
import {
  avatarKey,
  configKey,
  emitsKey,
  routerKey, navStyleKey,
  themeColorKey,
  userIdKey,
  usernameKey
} from '../../utils/injectedKeys';
import MenuOverlay from './MenuOverlay.vue';

const { router } = inject<{ router: Ref<any> }>(routerKey)!;
const { config } = inject<{ config: Ref<LayoutConfig> }>(configKey)!;
const { username } = inject<{ username?: Ref<string> }>(usernameKey)!;
const { avatar } = inject<{ avatar: Ref<string> }>(avatarKey)!;
const { userId } = inject<{ userId: Ref<string> }>(userIdKey)!;
const { themeColor } = inject<{ themeColor: Ref<ThemeColor> }>(themeColorKey)!;
const emits = inject<any>(emitsKey)!;

const navStyle = ref('dark');
const { navStyleChange } = inject<{ navStyleChange: (mode: NavStyle) => void }>(navStyleKey)!;
const handlenavStyleChange = (e: any) => {
  navStyleChange(e.target.value);
};
const seqLineColor = computed(() => {
  if (config.value.layout !== 'default' && config.value.navStyle === 'dark') {
    return colorDarken('#c1c6c8' || '#fff', 0.6).slice(0, 7);
  }
  return colorLighten('#191919' || '#555', 0.8).slice(0, 7);
});

const userMenuOpen = ref(false);
const navigateTo = (path: string) => {
  userMenuOpen.value = false;
  router.value.push({ path });
};
const onAppDownload = () => {
  userMenuOpen.value = false;
  emits.downloadApp();
};
const onExit = () => {
  userMenuOpen.value = false;
  emits.exit();
};

const isFullscreen = ref(false);
const toggleFullscreen = (fullscreen: boolean) => {
  isFullscreen.value = fullscreen;
  emits.fullscreen(fullscreen);
};

const avatarUrl = ref(avatar);

watch(
  () => avatar.value,
  () => {
    avatarUrl.value = avatar.value;
  }
);

const hoverMenuKey = ref<string | null>();
const handleMenuMouseover = (e: Event) => {
  const target = e.target as HTMLDivElement;
  hoverMenuKey.value = target.getAttribute('data-id');
};
const panelColor = computed(() => {
  if (config.value.layout !== 'default' && config.value.navStyle === 'dark') {
    return '#c1c6c8';
  } else {
    return '#191919';
  }
});
const panelBg = computed(() => {
  const topBarBg =
    config.value.layout === 'default' || config.value.navStyle === 'light' ? '#fff' : themeColor.value?.nav;
  if (isDarkColor(topBarBg || '#fff')) {
    return colorDarken(topBarBg || '#fff', 0.33).slice(0, 7);
  }
  return colorDarken(topBarBg || '#555', 0.03).slice(0, 7);
});

const navStyleOpt = ref(
  arrayToOption([
    ['dark', '深色'],
    ['light', '浅色']
  ])
);
</script>

<style scoped>
.toolbars {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  height: 46px;
  color: inherit;
}
.toolbars-divider {
  position: relative;
  width: 1px;
  height: 1rem;
  margin: 0 8px 0 12px;
  opacity: 50%;
  background-color: v-bind("panelColor");
}

.toolbars-dropdown-tabs {
  padding: 4px 12px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05);
}

.toolbars-dropdown-content {
  height: 100%;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 0.5rem;
}
.toolbars-dropdown-content:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

.toolbars-svgIcon {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 40px;
  height: 100%;
  cursor: pointer;
}
.toolbars-svgIcon-dark:hover {
  background-color: rgba(255, 255, 255, 0.05) !important;
}
.toolbars-svgIcon:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

.toolbars-dropdown-menu-span {
  white-space: nowrap;
  margin-left: 8px;
  pointer-events: none;
}

.toolbars-search-modal {
  position: fixed;
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 99999;
  background-color: rgba(0, 0, 0, 0.5);
  transition: all 0.3s ease 0s;
}
.toolbars-search-modal-content {
  position: absolute;
  top: 15%;
}
.toolbars-search-modal-arrow {
  position: relative;
  width: 100%;
  height: 0;
  overflow: visible;
}
.toolbars-search-modal-arrow:before {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  bottom: -12px;
  left: 50%;
  border-bottom: 5px solid white;
  border-right: 5px solid transparent;
  border-left: 5px solid transparent;
  transform: translateX(-50%);
}
.toolbars-search-modal-result {
  position: relative;
  width: 100%;
  height: 300px;
  margin-top: 12px;
  border-radius: 8px;
  background-color: white;
  overflow-y: auto;
}
.toolbars-search-modal-result::-webkit-scrollbar-track-piece {
  background: transparent;
}
.toolbars-search-modal-result::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.toolbars-search-modal-result::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 20px;
}

.scale-up-ver-top {
  -webkit-animation: scale-up-ver-top 0.2s linear both;
  animation: scale-up-ver-top 0.2s linear both;
}

/**
 * ----------------------------------------
 * animation scale-up-ver-top
 * ----------------------------------------
 */
@-webkit-keyframes scale-up-ver-top {
  0% {
    -webkit-transform: scaleY(0);
    transform: scaleY(0);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
  100% {
    -webkit-transform: scaleY(1);
    transform: scaleY(1);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
}
@keyframes scale-up-ver-top {
  0% {
    -webkit-transform: scaleY(0);
    transform: scaleY(0);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
  100% {
    -webkit-transform: scaleY(1);
    transform: scaleY(1);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
}
.userMenu-item {
  width: 100%;
  display: flex;
  align-items: center;
  padding: 4px 12px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s;
}
.userMenu-item svg {
  pointer-events: none !important;
}
.userMenu-item:hover {
  color: whitesmoke;
}

.userMenu-item-custom {
  width: 100%;
  display: flex;
  align-items: center;
  padding: 4px 12px;
  font-size: 14px;
  justify-content: space-between;
}

.toolbars-menuOverlay-infoWrapper {
  position: relative;
  display: grid;
  width: 100%;
  grid-template-columns: 60px 1fr;
  grid-template-rows: 1fr 1fr;
  place-items: center;
}
.toolbars-menuOverlay-infoWrapper h4,
.toolbars-menuOverlay-infoWrapper p {
  white-space: nowrap;
  justify-self: start;
  margin-bottom: 0;
}
.toolbars-menuOverlay-infoWrapper p {
  opacity: 70%;
  font-size: 12px;
}
</style>

<style lang="scss">
.toolbars-dropdown-menu-item {
  margin-right: 12px;
  .ant-dropdown-menu-title-content {
    display: flex;
    align-items: center;
  }
}
.ant-dropdown-menu {
  flex-direction: column;
}
</style>
