<template>
  <nav class="app-navbar">
    <div class="nav-container">
      <router-link to="/dashboard" class="nav-brand">
        <img
          src="/icons/xiaoyugan.png"
          alt="XYZW"
          class="brand-logo"
        >
        <span class="brand-text">XYZW 控制台</span>
      </router-link>

      <div class="nav-menu">
        <router-link
          to="/dashboard"
          class="nav-item"
          active-class="active"
        >
          <n-icon><Home /></n-icon>
          <span>首页</span>
        </router-link>
        <router-link
          to="/game-features"
          class="nav-item"
          active-class="active"
        >
          <n-icon><Cube /></n-icon>
          <span>游戏功能</span>
        </router-link>
        <router-link
          to="/tokens"
          class="nav-item"
          active-class="active"
        >
          <n-icon><PersonCircle /></n-icon>
          <span>Token管理</span>
        </router-link>
        <router-link
          to="/daily-tasks"
          class="nav-item"
          active-class="active"
        >
          <n-icon><CheckmarkCircle /></n-icon>
          <span>任务管理</span>
        </router-link>
        <router-link
          to="/message-test"
          class="nav-item"
          active-class="active"
        >
          <n-icon><Chatbubbles /></n-icon>
          <span>消息测试</span>
        </router-link>
        <router-link
          to="/profile"
          class="nav-item"
          active-class="active"
        >
          <n-icon><Settings /></n-icon>
          <span>个人设置</span>
        </router-link>
      </div>

      <div class="nav-user">
        <!-- Token选择器 -->
        <div class="token-selector-wrapper">
          <span class="token-label">当前账号</span>
          <n-select
            v-model:value="selectedTokenId"
            :options="tokenOptions"
            :placeholder="tokenOptions.length > 0 ? '请选择Token' : '请先在Token管理中添加Token'"
            class="token-selector"
            size="medium"
            @update:value="handleTokenChange"
          >
            <template #arrow>
              <n-icon><ChevronDown /></n-icon>
            </template>
          </n-select>
        </div>

        <!-- 主题切换按钮 -->
        <ThemeToggle />
        
        <n-dropdown
          :options="userMenuOptions"
          @select="handleUserAction"
        >
          <div class="user-info">
            <n-avatar
              size="medium"
              fallback-src="/icons/xiaoyugan.png"
            />
            <span class="username">{{ tokenStore.selectedToken?.name || '未选择Token' }}</span>
            <n-icon><ChevronDown /></n-icon>
          </div>
        </n-dropdown>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { h, ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useMessage, NIcon } from 'naive-ui'
import { useTokenStore } from '@/stores/tokenStore'
import ThemeToggle from '@/components/ThemeToggle.vue'
import {
  Home,
  Cube,
  PersonCircle,
  CheckmarkCircle,
  Chatbubbles,
  Settings,
  ChevronDown,
  LogOut,
  Person
} from '@vicons/ionicons5'

const router = useRouter()
const message = useMessage()
const tokenStore = useTokenStore()

// Token选择器
const selectedTokenId = ref(tokenStore.selectedToken?.id || null)

// Token选项列表
const tokenOptions = computed(() => {
  return tokenStore.gameTokens.map(token => ({
    label: token.name,
    value: token.id
  }))
})

// 处理Token切换
const handleTokenChange = (tokenId) => {
  if (tokenId) {
    tokenStore.selectToken(tokenId)
    message.success(`已切换到: ${tokenStore.selectedToken?.name}`)
  }
}

// 监听tokenStore的变化，同步selectedTokenId
watch(() => tokenStore.selectedToken, (newToken) => {
  if (newToken) {
    selectedTokenId.value = newToken.id
  }
}, { immediate: true })

// 用户菜单选项
const userMenuOptions = [
  {
    label: '个人设置',
    key: 'profile',
    icon: () => h(NIcon, null, { default: () => h(Person) })
  },
  {
    label: 'Token管理',
    key: 'tokens',
    icon: () => h(NIcon, null, { default: () => h(PersonCircle) })
  },
  {
    type: 'divider'
  },
  {
    label: '退出登录',
    key: 'logout',
    icon: () => h(NIcon, null, { default: () => h(LogOut) })
  }
]

// 处理用户菜单操作
const handleUserAction = (key) => {
  switch (key) {
    case 'profile':
      router.push('/profile')
      break
    case 'tokens':
      router.push('/tokens')
      break
    case 'logout':
      tokenStore.clearTokens()
      message.success('已退出登录')
      router.push('/tokens')
      break
  }
}
</script>

<style scoped lang="scss">
.app-navbar {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;

  [data-theme="dark"] &,
  html.dark & {
    background: rgba(26, 32, 44, 0.95);
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }
}

.nav-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  transition: all 0.3s ease;
  padding: 8px 12px;
  border-radius: 8px;

  &:hover {
    background: rgba(0, 0, 0, 0.05);

    [data-theme="dark"] &,
    html.dark & {
      background: rgba(255, 255, 255, 0.1);
    }
  }
}

.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 8px;
}

.brand-text {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary, #2d3748);
  letter-spacing: -0.5px;

  [data-theme="dark"] &,
  html.dark & {
    color: #ffffff;
  }
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  justify-content: center;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text-secondary, #718096);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  position: relative;

  [data-theme="dark"] &,
  html.dark & {
    color: rgba(255, 255, 255, 0.7);
  }

  &:hover {
    background: rgba(0, 0, 0, 0.04);
    color: var(--text-primary, #2d3748);

    [data-theme="dark"] &,
    html.dark & {
      background: rgba(255, 255, 255, 0.1);
      color: #ffffff;
    }
  }

  &.active {
    background: var(--primary-color, #18a058);
    color: white;

    &:hover {
      background: var(--primary-color-hover, #16a058);
    }
  }

  .n-icon {
    font-size: 18px;
  }
}

.nav-user {
  display: flex;
  align-items: center;
  gap: 16px;
}

// Token选择器包装容器
.token-selector-wrapper {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 12px;
  background: linear-gradient(135deg, rgba(24, 160, 88, 0.1), rgba(54, 173, 106, 0.05));
  border-radius: 12px;
  border: 1.5px solid rgba(24, 160, 88, 0.2);
  box-shadow: 0 2px 8px rgba(24, 160, 88, 0.15);
  transition: all 0.3s ease;

  [data-theme="dark"] &,
  html.dark & {
    background: linear-gradient(135deg, rgba(24, 160, 88, 0.15), rgba(54, 173, 106, 0.1));
    border-color: rgba(24, 160, 88, 0.3);
    box-shadow: 0 2px 12px rgba(24, 160, 88, 0.25);
  }

  &:hover {
    border-color: rgba(24, 160, 88, 0.4);
    box-shadow: 0 4px 16px rgba(24, 160, 88, 0.25);
    transform: translateY(-1px);

    [data-theme="dark"] &,
    html.dark & {
      border-color: rgba(24, 160, 88, 0.5);
      box-shadow: 0 4px 20px rgba(24, 160, 88, 0.35);
    }
  }
}

.token-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--primary-color, #18a058);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding-left: 2px;

  [data-theme="dark"] &,
  html.dark & {
    color: rgba(24, 160, 88, 1);
  }
}

.token-selector {
  min-width: 180px;
  
  :deep(.n-base-selection) {
    background: var(--bg-primary, #ffffff);
    border-radius: 8px;
    transition: all 0.2s ease;
    border: 1px solid rgba(24, 160, 88, 0.15) !important;

    [data-theme="dark"] &,
    html.dark & {
      background: rgba(0, 0, 0, 0.2);
      border-color: rgba(24, 160, 88, 0.3) !important;
    }
  }

  :deep(.n-base-selection-label) {
    font-weight: 600 !important;
    color: var(--text-primary, #2d3748);

    [data-theme="dark"] &,
    html.dark & {
      color: #ffffff !important;
    }
  }

  :deep(.n-base-selection-placeholder) {
    font-size: 13px;
    color: var(--text-tertiary, #a0aec0);

    [data-theme="dark"] &,
    html.dark & {
      color: rgba(255, 255, 255, 0.5);
    }
  }

  :deep(.n-base-selection__border),
  :deep(.n-base-selection__state-border) {
    border: none !important;
  }

  :deep(.n-base-suffix) {
    color: var(--primary-color, #18a058);
  }
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(0, 0, 0, 0.04);

    [data-theme="dark"] &,
    html.dark & {
      background: rgba(255, 255, 255, 0.1);
    }
  }
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary, #2d3748);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  [data-theme="dark"] &,
  html.dark & {
    color: #ffffff;
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .nav-menu {
    gap: 2px;
  }

  .nav-item {
    padding: 8px 12px;
    font-size: 13px;

    span {
      display: none;
    }
  }

  .token-selector-wrapper {
    padding: 6px 10px;
  }

  .token-label {
    font-size: 10px;
  }

  .token-selector {
    min-width: 140px;
  }

  .username {
    max-width: 80px;
  }
}

@media (max-width: 768px) {
  .nav-container {
    padding: 0 16px;
  }

  .brand-text {
    display: none;
  }

  .nav-menu {
    gap: 0;
  }

  .nav-item {
    padding: 8px 10px;

    .n-icon {
      font-size: 20px;
    }
  }

  .token-selector-wrapper {
    padding: 6px 8px;
  }

  .token-label {
    font-size: 9px;
  }

  .token-selector {
    min-width: 120px;
    max-width: 120px;
  }

  .username {
    display: none;
  }

  .user-info {
    padding: 6px 8px;
  }
}
</style>

