<template>
  <div class="sponsor-danmaku" v-if="memberPrizes.length > 0">
    <transition name="danmaku-fade">
      <div 
        v-if="currentIndex >= 0 && currentIndex < memberPrizes.length"
        :key="memberPrizes[currentIndex].id"
        class="danmaku-item"
      >
        <div class="danmaku-card">
          <div class="danmaku-avatar">
            <span class="avatar-placeholder">{{ memberPrizes[currentIndex].sponsor.charAt(0) }}</span>
          </div>
          <div class="danmaku-content">
            <span class="thank-text">感谢</span>
            <span class="sponsor-name">{{ memberPrizes[currentIndex].sponsor }}</span>
            <span class="sponsor-action">赞助的</span>
            <span class="sponsor-item">{{ memberPrizes[currentIndex].name }}</span>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from 'vue';

const props = defineProps({
  prizes: {
    type: Array,
    default: () => []
  }
});

const memberPrizes = computed(() => {
  return props.prizes.filter(p => p.source === 'member' && p.sponsor);
});

const currentIndex = ref(-1);
let timer = null;

const startDanmaku = () => {
  if (memberPrizes.value.length === 0) return;
  
  currentIndex.value = 0;
  
  timer = setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % memberPrizes.value.length;
  }, 15000); // 每15秒切换一条
};

const stopDanmaku = () => {
  if (timer) {
    clearInterval(timer);
    timer = null;
  }
};

watch(() => memberPrizes.value.length, (newLen) => {
  stopDanmaku();
  if (newLen > 0) {
    startDanmaku();
  }
});

onMounted(() => {
  if (memberPrizes.value.length > 0) {
    startDanmaku();
  }
});

onUnmounted(() => {
  stopDanmaku();
});
</script>

<style scoped>
.sponsor-danmaku {
  position: fixed;
  top: 80px;
  left: 0;
  width: 100%;
  height: 70px;
  overflow: hidden;
  pointer-events: none;
  z-index: 50;
}

.danmaku-item {
  position: absolute;
  top: 10px;
  right: -100%;
  will-change: transform;
  pointer-events: auto;
  filter: drop-shadow(0 8px 30px rgba(212, 175, 55, 0.6));
  animation: danmaku-move 15s linear forwards;
}

.danmaku-fade-enter-active {
  animation: danmaku-move 15s linear forwards;
}

.danmaku-fade-leave-active {
  opacity: 0;
  transition: opacity 0.5s;
}

.danmaku-item:hover {
  animation-play-state: paused;
}

@keyframes danmaku-move {
  0% {
    right: -100%;
    opacity: 0;
  }
  5% {
    opacity: 1;
  }
  95% {
    opacity: 1;
  }
  100% {
    right: 100%;
    opacity: 0;
  }
}

.danmaku-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 24px;
  border-radius: 35px;
  background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 50%, #1a1a1a 100%);
  border: 2px solid #d4af37;
  box-shadow: 
    0 0 20px rgba(212, 175, 55, 0.5),
    0 0 40px rgba(212, 175, 55, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    inset 0 -1px 0 rgba(0, 0, 0, 0.5);
  transition: all 0.3s ease;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
}

.danmaku-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(212, 175, 55, 0.3), transparent);
  animation: shine 3s infinite;
}

@keyframes shine {
  0% {
    left: -100%;
  }
  50%, 100% {
    left: 100%;
  }
}

.danmaku-item:hover .danmaku-card {
  transform: scale(1.1);
  box-shadow: 
    0 0 30px rgba(212, 175, 55, 0.8),
    0 0 60px rgba(212, 175, 55, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.2),
    inset 0 -1px 0 rgba(0, 0, 0, 0.5);
}

.danmaku-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #d4af37 0%, #f4e5a1 50%, #d4af37 100%);
  flex-shrink: 0;
  border: 3px solid #d4af37;
  box-shadow: 
    0 0 15px rgba(212, 175, 55, 0.8),
    inset 0 0 10px rgba(0, 0, 0, 0.3);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.danmaku-avatar::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.3) 50%, transparent 70%);
  animation: avatar-shine 4s infinite;
  pointer-events: none;
}

@keyframes avatar-shine {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.avatar-placeholder {
  font-size: 20px;
  font-weight: bold;
  color: #1a1a1a;
  position: relative;
  z-index: 1;
}

.danmaku-content {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 15px;
  font-weight: 600;
  position: relative;
  z-index: 1;
}

.thank-text {
  font-size: 14px;
  color: #d4af37;
  font-weight: 500;
  text-shadow: 0 0 10px rgba(212, 175, 55, 0.8);
}

.sponsor-name {
  font-weight: 800;
  font-size: 17px;
  background: linear-gradient(135deg, #d4af37 0%, #f4e5a1 50%, #d4af37 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 0 20px rgba(212, 175, 55, 0.8);
  letter-spacing: 1px;
}

.sponsor-action {
  color: #c0c0c0;
  font-size: 14px;
  font-weight: 500;
}

.sponsor-item {
  font-weight: 700;
  padding: 4px 12px;
  background: linear-gradient(135deg, #d4af37 0%, #f4e5a1 50%, #d4af37 100%);
  border-radius: 15px;
  font-size: 14px;
  color: #1a1a1a;
  box-shadow: 
    0 0 15px rgba(212, 175, 55, 0.6),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
  letter-spacing: 0.5px;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 768px) {
  .sponsor-danmaku {
    top: 60px;
    height: 60px;
  }
  
  .danmaku-item {
    top: 5px;
  }
  
  .danmaku-card {
    padding: 8px 18px;
    gap: 10px;
  }
  
  .danmaku-avatar {
    width: 36px;
    height: 36px;
    border: 2.5px solid #d4af37;
  }
  
  .avatar-placeholder {
    font-size: 16px;
  }
  .danmaku-content {
    font-size: 13px;
    gap: 6px;
  }
  
  .thank-text {
    font-size: 12px;
  }
  
  .sponsor-name {
    font-size: 15px;
  }
  
  .sponsor-action {
    font-size: 12px;
  }
  
  .sponsor-item {
    font-size: 12px;
    padding: 3px 10px;
    max-width: 200px;
  }
}</style>
