<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useMonsterStore } from '@/stores/monster'
import { useCharacterStore } from '@/stores/character'
import { ElMessage } from 'element-plus'
import Particles from '@/components/Particles/Particles.vue'
import defaultIcon from '@/assets/images/item-default.svg'

const router = useRouter()
const monsterStore = useMonsterStore()
const characterStore = useCharacterStore()

const getMonsterIcon = (iconName?: string) => {
  if (!iconName) return defaultIcon
  return `/src/assets/images/monster/${iconName}`
}

const getMonsterTypeLabel = (type: number) => {
  const labels: Record<number, string> = {
    1: '普通',
    2: '精英',
    3: '首领'
  }
  return labels[type] || '普通'
}

const getMonsterTypeClass = (type: number) => {
  const classes: Record<number, string> = {
    1: 'type-normal',
    2: 'type-elite',
    3: 'type-boss'
  }
  return classes[type] || 'type-normal'
}

const playerId = computed(() => characterStore.currentCharacter?.id || 0)
const playerLevel = computed(() => characterStore.currentCharacter?.levelId || 0)

const canChallenge = (monster: { levelId: number }) => {
  return playerLevel.value >= monster.levelId
}

const handleChallenge = async (monster: { id: number; levelId: number }) => {
  if (!canChallenge(monster)) {
    ElMessage.warning('等级不足，无法挑战')
    return
  }
  router.push(`/battle?id=${monster.id}`)
}

const handleGoBack = () => {
  router.push('/game')
}

onMounted(async () => {
  if (playerId.value) {
    await monsterStore.fetchMonsters(playerId.value)
  }
})
</script>

<template>
  <div class="monster-list-page">
    <Particles :particle-count="50" :particle-colors="['#ffffff', '#aaaaaa']" class="particles-bg" />
    <div class="page-container">
      <div class="page-header">
        <span class="back-btn" @click="handleGoBack">← 返回</span>
        <span class="title">怪物挑战</span>
        <span class="player-level">Lv.{{ playerLevel }}</span>
      </div>

      <div v-if="monsterStore.loading" class="loading">
        加载中...
      </div>

      <div v-else-if="monsterStore.monsters.length === 0" class="empty-state">
        <div class="empty-icon">👹</div>
        <div class="empty-text">暂无可挑战的怪物</div>
      </div>

      <div v-else class="monster-list">
        <div v-for="monster in monsterStore.monsters" :key="monster.id" class="monster-card"
          :class="[getMonsterTypeClass(monster.type), { disabled: !canChallenge(monster) }]"
          @click="handleChallenge(monster)">
          <div class="card-inner">
            <div class="card-header">
              <div class="card-title-row">
                <div class="card-icon">
                  <img :src="getMonsterIcon(monster.icon)" :alt="monster.name" />
                </div>
                <div class="card-info">
                  <span class="card-title">{{ monster.name }}</span>
                  <span class="card-level">等级 {{ monster.levelId }}</span>
                </div>
              </div>
              <span class="type-badge" :class="getMonsterTypeClass(monster.type)">
                {{ getMonsterTypeLabel(monster.type) }}
              </span>
            </div>

            <div class="card-desc">{{ monster.description }}</div>

            <div class="card-stats">
              <div class="stat-item">
                <span class="stat-label">生命</span>
                <span class="stat-value health">{{ monster.health }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">攻击</span>
                <span class="stat-value attack">{{ monster.attack }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">防御</span>
                <span class="stat-value defense">{{ monster.defense }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">暴击</span>
                <span class="stat-value">{{ (monster.criticalRate * 100).toFixed(0) }}%</span>
              </div>
            </div>

            <div class="card-rewards" v-if="monster.rewards && monster.rewards.length > 0">
              <span class="reward-label">击杀奖励:</span>
              <span v-for="reward in monster.rewards" :key="reward.id" class="reward-item">
                {{ reward.itemName }} ×{{ reward.count }}
              </span>
              <span v-if="monster.rewards.length > 2" class="reward-more">+{{ monster.rewards.length - 2 }}</span>
            </div>

            <div class="card-action">
              <span class="action-text">
                {{ canChallenge(monster) ? '点击挑战' : '等级不足' }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<style scoped lang="css">
.monster-list-page {
  min-height: 100vh;
  background: #000000;
  padding: 20px;
  position: relative;
}

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

.page-container {
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  z-index: 10;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  margin-bottom: 20px;
}

.back-btn {
  color: #666666;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 8px;
}

.back-btn:hover {
  color: #ffffff;
}

.title {
  color: #ffffff;
  font-size: 1.1rem;
  font-weight: 500;
}

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

.loading {
  text-align: center;
  color: #666666;
  padding: 40px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 16px;
}

.empty-text {
  color: #ffffff;
  font-size: 1.1rem;
}

.monster-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.monster-card {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  cursor: pointer;
}

.monster-card:hover {
  transform: translateY(-2px);
}

.monster-card.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.monster-card.disabled:hover {
  transform: none;
}

.type-normal {
  border-color: rgba(239, 68, 68, 0.3);
}

.type-elite {
  border-color: rgba(139, 92, 246, 0.4);
}

.type-boss {
  border-color: rgba(220, 38, 38, 0.5);
}

.card-inner {
  padding: 16px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.card-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.card-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 8px;
  flex-shrink: 0;
}

.card-icon img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.card-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.card-title {
  color: #ffffff;
  font-size: 1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-level {
  color: #888888;
  font-size: 0.75rem;
}

.type-badge {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 500;
  white-space: nowrap;
}

.type-badge.type-normal {
  background: rgba(100, 100, 100, 0.3);
  color: #888888;
}

.type-badge.type-elite {
  background: rgba(139, 92, 246, 0.3);
  color: #a78bfa;
}

.type-badge.type-boss {
  background: rgba(220, 38, 38, 0.3);
  color: #fca5a5;
}

.card-desc {
  color: #888888;
  font-size: 0.8rem;
  line-height: 1.4;
  margin-top: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 12px;
  padding: 10px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
}

.stat-item {
  text-align: center;
}

.stat-label {
  display: block;
  color: #666666;
  font-size: 0.7rem;
  margin-bottom: 2px;
}

.stat-value {
  display: block;
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: 500;
}

.stat-value.health {
  color: #ef4444;
}

.stat-value.attack {
  color: #f97316;
}

.stat-value.defense {
  color: #3b82f6;
}

.card-rewards {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  margin-top: 10px;
}

.reward-label {
  color: #666666;
  font-size: 0.7rem;
}

.reward-item {
  background: rgba(34, 197, 94, 0.1);
  border: 1px solid rgba(34, 197, 94, 0.2);
  color: #22c55e;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.7rem;
}

.reward-more {
  background: rgba(255, 255, 255, 0.1);
  color: #888888;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.7rem;
}

.card-action {
  margin-top: 12px;
  text-align: center;
}

.action-text {
  color: #ff8844;
  font-size: 0.85rem;
  font-weight: 500;
}

.monster-card.disabled .action-text {
  color: #666666;
}
</style>