<script setup lang="ts">
import { computed, ref, onMounted } from 'vue'
import { ElProgress } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import { useCharacterStore } from '@/stores/character'
import { useRouter } from 'vue-router'
import Particles from '@/components/Particles/Particles.vue'
import GlareHover from '@/components/GlareHover/GlareHover.vue'
import ChatBox from '@/components/ChatBox.vue'
import ShinyText from '@/components/ShinyText/ShinyText.vue'
import StarBorder from '@/components/StarBorder/StarBorder.vue'
import meditationIcon from '@/assets/images/meditation.svg'
import missionIcon from '@/assets/images/mission.svg'
import scrapIcon from '@/assets/images/scrap.svg'
import bagIcon from '@/assets/images/bag.svg'
import monsterIcon from '@/assets/images/monster.svg'
import shopIcon from '@/assets/images/shop.svg'
import catalogIcon from '@/assets/images/catalog.svg'

const authStore = useAuthStore()
const characterStore = useCharacterStore()
const router = useRouter()

const isTraining = computed(() => !!characterStore.currentCharacter?.trainingOn)

const canBreakthrough = computed(() => characterStore.currentCharacter?.canBreakthrough ?? false)
const currentLevelName = computed(() => characterStore.currentCharacter?.levelName ?? '')
const currentExp = computed(() => characterStore.currentCharacter?.currentExp ?? 0)
const nextLevelName = computed(() => characterStore.currentCharacter?.nextLevelName ?? '')
const nextLevelMinExp = computed(() => characterStore.currentCharacter?.nextLevelMinExp ?? 0)
const breakthroughRate = computed(() => characterStore.currentCharacter?.breakthroughRate ?? 0)
const requiredPillName = computed(() => characterStore.currentCharacter?.nextLevelRequiredPillName ?? '')
const requiredPillQuantity = computed(() => characterStore.currentCharacter?.nextLevelRequiredPillQuantity ?? 0)
const hasPillCost = computed(() => requiredPillQuantity.value > 0 && !!requiredPillName.value)

const expProgress = computed(() => {
  if (!nextLevelMinExp.value) return 0
  return Math.min(100, (currentExp.value / nextLevelMinExp.value) * 100)
})

const breakthroughMessage = ref('')
const showBreakthroughMessage = ref(false)

const menuItems = computed(() => [
  { label: '任务', icon: missionIcon, useImage: true },
  { label: isTraining.value ? '打坐中' : '打坐', icon: meditationIcon, useImage: true, isTraining: isTraining.value },
  { label: '背包', icon: bagIcon, useImage: true },
  { label: '捡垃圾', icon: scrapIcon, useImage: true },
  { label: '商店', icon: shopIcon, useImage: true },
  { label: '挑战', icon: monsterIcon, useImage: true },
  { label: '图鉴', icon: catalogIcon, useImage: true }
])

const formatNumber = (num: number) => {
  return Math.floor(num).toLocaleString('zh-CN')
}

const handleLogout = () => {
  authStore.logout()
  characterStore.clearCurrentCharacter()
  window.location.href = '/login'
}

const handleSwitchCharacter = () => {
  window.location.href = '/character'
}

const navigateToTutorial = () => {
  router.push('/tutorial')
}

const navigateTo = (item: { label: string }) => {
  if (item.label === '打坐' || item.label === '打坐中') {
    router.push('/training')
  } else if (item.label === '任务') {
    router.push('/daily-mission')
  } else if (item.label === '背包') {
    router.push('/bag')
  } else if (item.label === '捡垃圾') {
    router.push('/scrap')
  } else if (item.label === '挑战') {
    router.push('/monster-list')
  } else if (item.label === '商店') {
    router.push('/shop')
  } else if (item.label === '图鉴') {
    router.push('/catalog')
  }
}

const handleBreakthrough = async () => {
  const result = await characterStore.breakthrough()
  breakthroughMessage.value = result.message
  showBreakthroughMessage.value = true
  setTimeout(() => {
    showBreakthroughMessage.value = false
  }, 3000)
}

onMounted(async () => {
  await characterStore.fetchCharacters()
  const current = characterStore.characters.find(c => c.id === characterStore.currentCharacter?.id)
  if (current) {
    characterStore.currentCharacter = current
    localStorage.setItem('current_character', JSON.stringify(current))
  }
})
</script>

<template>
  <div class="game-page">
    <Particles :particle-count="50" :particle-colors="['#ffffff', '#cccccc']" class="particles-bg" />
    <div class="game-shell">
      <aside id="character-sidebar" class="character-sidebar">
        <div class="sidebar-card">
          <div class="sidebar-header">
            <div class="sidebar-title-block">
              <span class="sidebar-char-name">{{ characterStore.currentCharacter?.name || '未选择角色' }}</span>
              <span class="sidebar-char-level">{{ characterStore.currentCharacter?.levelName || '' }}</span>
            </div>
            <button type="button" class="sidebar-switch-btn" @click="handleSwitchCharacter">
              切换角色
            </button>
          </div>

          <div class="detail-section">
            <div class="section-title">基础信息</div>
            <div class="info-row">
              <span class="info-label">职业</span>
              <span class="info-value">{{ characterStore.currentCharacter?.professionName || '未选择' }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">灵石</span>
              <span class="info-value">{{ formatNumber(characterStore.currentCharacter?.money || 0) }}</span>
            </div>
          </div>

          <div class="detail-section">
            <div class="section-title">属性</div>
            <div class="attr-row">
              <span class="attr-label"><span class="attr-icon hp-icon">❤️</span>生命</span>
              <span class="attr-value">
                <span class="base-value">{{ formatNumber(characterStore.currentCharacter?.baseMaxHP || 0) }}</span>
                <span v-if="(characterStore.currentCharacter?.bonusMaxHP || 0) > 0" class="bonus-value">+{{
                  formatNumber(characterStore.currentCharacter?.bonusMaxHP || 0) }}</span>
              </span>
            </div>
            <div class="attr-row">
              <span class="attr-label"><span class="attr-icon atk-icon">⚔️</span>攻击</span>
              <span class="attr-value">
                <span class="base-value">{{ formatNumber(characterStore.currentCharacter?.baseAttack || 0) }}</span>
                <span v-if="(characterStore.currentCharacter?.bonusAttack || 0) > 0" class="bonus-value">+{{
                  formatNumber(characterStore.currentCharacter?.bonusAttack || 0) }}</span>
              </span>
            </div>
            <div class="attr-row">
              <span class="attr-label"><span class="attr-icon def-icon">🛡️</span>防御</span>
              <span class="attr-value">
                <span class="base-value">{{ formatNumber(characterStore.currentCharacter?.baseDefend || 0) }}</span>
                <span v-if="(characterStore.currentCharacter?.bonusDefend || 0) > 0" class="bonus-value">+{{
                  formatNumber(characterStore.currentCharacter?.bonusDefend || 0) }}</span>
              </span>
            </div>
            <div class="attr-row">
              <span class="attr-label"><span class="attr-icon crit-icon">💥</span>暴击率</span>
              <span class="attr-value">
                <span class="base-value">{{ (characterStore.currentCharacter?.baseCriticalRate || 0).toFixed(1) }}%</span>
                <span v-if="(characterStore.currentCharacter?.bonusCriticalRate || 0) > 0" class="bonus-value">+{{
                  (characterStore.currentCharacter?.bonusCriticalRate || 0).toFixed(1) }}%</span>
              </span>
            </div>
          </div>

          <div class="detail-section detail-section--last">
            <div class="section-title">经验</div>
            <div class="exp-row">
              <span class="exp-label">当前经验</span>
              <span class="exp-value">{{ formatNumber(characterStore.currentCharacter?.currentExp || 0) }}</span>
            </div>
            <div v-if="characterStore.currentCharacter?.nextLevelMinExp" class="exp-row">
              <span class="exp-label">升级所需</span>
              <span class="exp-value">{{ formatNumber(characterStore.currentCharacter?.nextLevelMinExp || 0) }}</span>
            </div>
          </div>
        </div>
      </aside>

      <div class="game-main">
        <div class="game-container">
          <div style="text-align: center;">
            <ShinyText class="welcome-text" text="✨ 欢迎来到我的世界" :speed="2" :delay="0.5" :disabled="false" :color="'#b5b5b5'"
              :shine-color="'#34fef1'" :spread="120" :direction="'left'" :yoyo="false" :pause-on-hover="false" />
          </div>
          <div style="text-align: center; margin-bottom: 16px;">
            <button class="tutorial-btn" @click="navigateToTutorial">游戏指南</button>
          </div>

          <div class="menu-grid">
            <GlareHover v-for="item in menuItems" :key="item.label" width="100%" height="100px"
              :background="item.isTraining ? 'rgba(255,136,68,0.1)' : 'rgba(255,255,255,0.02)'" border-radius="16px"
              :border-color="item.isTraining ? 'rgba(255,136,68,0.3)' : 'rgba(255,255,255,0.08)'"
              :glare-color="item.isTraining ? '#ff8844' : '#ffffff'" :glare-opacity="0.1" class="menu-card"
              :class="{ 'training-active': item.isTraining }" @click="navigateTo(item)">
              <div class="menu-content">
                <img v-if="item.useImage" :src="item.icon" class="menu-icon-img" />
                <span v-else class="menu-icon">{{ item.icon }}</span>
                <span class="menu-label">{{ item.label }}</span>
              </div>
            </GlareHover>
          </div>

          <div v-if="nextLevelName" class="breakthrough-section">
            <div class="breakthrough-header">
              <span class="breakthrough-title">境界突破</span>
            </div>
            <div class="breakthrough-info">
              <div class="level-info">
                <span class="current-level">{{ currentLevelName }}</span>
                <span class="arrow">→</span>
                <span class="next-level">{{ nextLevelName }}</span>
              </div>
              <div class="exp-info">
                <span class="exp-text">{{ formatNumber(currentExp) }} / {{ formatNumber(nextLevelMinExp) }} 经验</span>
                <el-progress :percentage="Math.floor(expProgress)" color="#ff8844" :show-text="false" :stroke-width="8" />
                <span class="exp-percent">{{ Math.floor(expProgress) }}%</span>
              </div>
              <div class="rate-info">
                <span class="rate-label">突破成功率</span>
                <el-progress :percentage="breakthroughRate" color="#22c55e" :show-text="false" :stroke-width="8" />
                <span class="exp-percent">{{ breakthroughRate + `%` }}</span>
              </div>
              <div v-if="hasPillCost" class="pill-cost-info">
                <span class="pill-cost-label">突破消耗</span>
                <span class="pill-cost-value">{{ requiredPillName }} ×{{ requiredPillQuantity }}</span>
              </div>
              <button class="breakthrough-btn" :class="{ 'can-breakthrough': canBreakthrough }" :disabled="!canBreakthrough"
                @click="handleBreakthrough">
                {{ canBreakthrough ? '突破' : '未满足突破条件' }}
              </button>
            </div>
          </div>

          <div v-if="showBreakthroughMessage" class="toast-message">
            {{ breakthroughMessage }}
          </div>

          <div class="page-footer">
            <StarBorder as="div" color="#e63d3d" speed="3s" :thickness="3">
              <div class="btn-out" @click="handleLogout">
                退出登录
              </div>
            </StarBorder>
          </div>
        </div>

        <div class="chat-panel">
          <ChatBox embedded />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.game-page {
  min-height: 100vh;
  background: #000000;
  padding: 16px;
  padding-bottom: 24px;
  position: relative;
  overflow-x: hidden;
}

.particles-bg {
  position: fixed !important;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

.game-shell {
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 20px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 48px);
}

.character-sidebar {
  flex: 0 0 280px;
  min-width: 0;
  align-self: flex-start;
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.sidebar-card {
  background: rgba(16, 18, 24, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-title-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-char-name {
  color: #ffffff;
  font-size: 1.1rem;
  font-weight: 600;
}

.sidebar-char-level {
  color: #22c55e;
  font-size: 0.8rem;
}

.sidebar-switch-btn {
  width: 100%;
  padding: 10px 12px;
  background: rgba(34, 197, 94, 0.12);
  border: 1px solid rgba(34, 197, 94, 0.35);
  border-radius: 10px;
  color: #86efac;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.sidebar-switch-btn:hover {
  background: rgba(34, 197, 94, 0.2);
  color: #bbf7d0;
}

.detail-section--last {
  margin-bottom: 0;
}

.game-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
}

.game-container {
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
  padding-top: 8px;
  position: relative;
  z-index: 10;
  flex-shrink: 0;
}

.chat-panel {
  flex: 1;
  min-height: 280px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

@media (max-width: 900px) {
  .game-shell {
    flex-direction: column;
    min-height: auto;
  }

  .character-sidebar {
    flex: none;
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
    position: relative;
    top: 0;
    max-height: none;
  }

  .chat-panel {
    min-height: 240px;
    max-width: 560px;
    margin: 0 auto;
    width: 100%;
  }
}

.welcome-text {
  font-size: 1.35rem;
  font-weight: 300;
  text-align: center;
  color: #ffffff;
  letter-spacing: 0.1em;
  margin-bottom: 20px;
  font-weight: bold;
}

.star-border {
  background: transparent;
  border: none;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 32px;
}

.tutorial-btn {
  padding: 8px 20px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  color: #888888;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tutorial-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  color: #ffffff;
}

.menu-card {
  cursor: pointer;
}

.menu-card.training-active .menu-label {
  color: #ff8844;
}

.menu-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.menu-icon {
  font-size: 1.75rem;
  opacity: 0.8;
}

.menu-icon-img {
  width: 3rem;
  height: 3rem;
  object-fit: contain;
}

.menu-label {
  color: #cccccc;
  font-size: 0.875rem;
  font-weight: 400;
  letter-spacing: 0.1em;
}

.breakthrough-section {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 24px;
}

.breakthrough-header {
  display: flex;
  justify-content: center;
  margin-bottom: 16px;
}

.breakthrough-title {
  color: #ff8844;
  font-size: 1rem;
  font-weight: 500;
  letter-spacing: 0.1em;
}

.breakthrough-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.level-info {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
}

.current-level {
  color: #888888;
  font-size: 0.9rem;
}

.arrow {
  color: #555555;
  font-size: 0.9rem;
}

.next-level {
  color: #ffffff;
  font-size: 0.9rem;
  font-weight: 500;
}

.exp-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.exp-text {
  color: #666666;
  font-size: 0.8rem;
  text-align: center;
}

.exp-percent {
  color: #888888;
  font-size: 0.75rem;
  text-align: center;
}

.rate-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rate-label {
  color: #666666;
  font-size: 0.75rem;
  white-space: nowrap;
}

.pill-cost-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: rgba(255, 68, 68, 0.1);
  border: 1px solid rgba(255, 68, 68, 0.2);
  border-radius: 8px;
}

.pill-cost-label {
  color: #888888;
  font-size: 0.85rem;
}

.pill-cost-value {
  color: #ff4444;
  font-size: 0.9rem;
  font-weight: 500;
}

.breakthrough-btn {
  width: 100%;
  padding: 14px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  border: none;
  background: rgba(255, 255, 255, 0.05);
  color: #555555;
}

.breakthrough-btn:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.breakthrough-btn.can-breakthrough {
  background: linear-gradient(135deg, #ff8844 0%, #ff6644 100%);
  color: #ffffff;
}

.breakthrough-btn.can-breakthrough:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 20px rgba(255, 136, 68, 0.4);
}

.toast-message {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.9);
  color: #000000;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 0.9rem;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-10px);
  }

  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.logout-button {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  cursor: pointer;
  width: 200px;
}

.page-footer {
  margin-top: 32px;
  display: flex;
  justify-content: center;
}

.btn-out {
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.03);
  border: 1px dashed red;
  border-radius: 15px;
  padding: 12px 16px;
  cursor: pointer;
  width: 200px;
}

.logout-text {
  color: #888888;
  font-size: 0.8rem;
  font-weight: 400;
  letter-spacing: 0.1em;
  transition: color 0.3s ease;
}

.logout-button:hover .logout-text {
  color: #ffffff;
}

.detail-section {
  margin-bottom: 16px;
}

.section-title {
  color: #22c55e;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 12px;
  letter-spacing: 0.05em;
}

.info-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.info-label {
  color: #888888;
  font-size: 0.85rem;
}

.info-value {
  color: #ffffff;
  font-size: 0.85rem;
}

.attr-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.attr-label {
  color: #888888;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 6px;
}

.attr-icon {
  font-size: 0.9rem;
}

.hp-icon {
  color: #ef4444;
}

.atk-icon {
  color: #f97316;
}

.def-icon {
  color: #3b82f6;
}

.crit-icon {
  color: #eab308;
}

.attr-value {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.base-value {
  color: #ffffff;
  font-size: 1rem;
  font-weight: 500;
}

.bonus-value {
  color: #fbbf24;
  font-size: 0.8rem;
}

.exp-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.exp-label {
  color: #888888;
  font-size: 0.85rem;
}

.exp-value {
  color: #cccccc;
  font-size: 0.85rem;
}
</style>
