<script setup lang="ts">
import { computed, ref } from 'vue';
import type { VoteStamp } from '@gala/shared/types';
import stampImage from '../assets/images/stamp-horse-2026.png';
import Postmark from './Postmark.vue';

// 票据类型名称映射
const TICKET_TYPE_NAMES: Record<string, string> = {
  creative: '最佳创意',
  visual: '最佳视觉',
  atmosphere: '最佳氛围',
  performance: '最佳表演',
  teamwork: '最佳团队',
  popularity: '最受欢迎',
  potential: '最具潜力',
};

interface Props {
  id: string;
  name: string;
  teamName: string;
  order: number;
  votes: number;
  stamps?: VoteStamp[];
  slogan?: string;
}

const props = withDefaults(defineProps<Props>(), {
  slogan: 'With all our passion',
  stamps: () => [],
});

// Generate zip code display: 2|0|2|6|0|[order]
const zipCodes = computed(() => {
  const orderStr = String(props.order).padStart(1, '0');
  return ['2', '0', '2', '6', '0', orderStr];
});

// Limit displayed stamps to avoid overcrowding
const displayedStamps = computed(() => {
  return props.stamps?.slice(-12) || []; // Show last 12 stamps max
});

// Random position for new stamps (avoiding center content area)
function getStampStyle(stamp: VoteStamp) {
  const isNew = Date.now() - stamp.timestamp < 1000;
  
  // For fly-in animation
  const flyX = (Math.random() - 0.5) * 1000; // -500 to 500
  const flyY = (Math.random() - 0.5) * 1000;
  const flyRotate = (Math.random() - 0.5) * 180;
  
  return {
    left: `${stamp.x}%`,
    top: `${stamp.y}%`,
    transform: isNew ? undefined : `translate(-50%, -50%) rotate(${stamp.rotation}deg)`,
    '--fly-x': `${flyX}px`,
    '--fly-y': `${flyY}px`,
    '--fly-rotate': `${flyRotate}deg`,
  };
}

// Stamp target ref for particle animation
const stampTargetRef = ref<HTMLElement | null>(null);

// Award name mapping for stamps
const awardNames: Record<string, string> = {
  creative: '最佳创意奖',
  visual: '最佳视觉奖',
  atmosphere: '最佳气氛奖',
  performance: '最佳表演奖',
  teamwork: '最佳团队奖',
  popularity: '最受欢迎奖',
  potential: '最具潜力奖',
};

function getAwardName(type: string) {
  return awardNames[type] || '优秀节目奖';
}

defineExpose({
  stampTargetRef,
});
</script>

<template>
  <div class="postcard">
    <!-- Accumulated Stamps Layer (on top) -->
    <div class="stamps-layer">
      <div
        v-for="stamp in displayedStamps"
        :key="stamp.id"
        class="postmark-wrapper"
        :class="{ 'is-new': Date.now() - stamp.timestamp < 1000 }"
        :style="getStampStyle(stamp)"
      >
        <Postmark
          :award-name="getAwardName(stamp.ticketType)"
          :user-name="stamp.userName"
          color="red"
        />
      </div>
    </div>

    <!-- Top Row: Zip codes left, Stamp right -->
    <div class="top-row">
      <div class="zip-codes">
        <div v-for="(code, idx) in zipCodes" :key="idx" class="zip-box">
          {{ code }}
        </div>
      </div>
      <div ref="stampTargetRef" class="stamp-box">
        <img :src="stampImage" alt="2026马年邮票" class="stamp-image" />
      </div>
    </div>

    <!-- Content Row: Left content, Right address -->
    <div class="content-row">
      <!-- Left Side: Title + Slogan -->
      <div class="content-left">
        <h2 class="program-name">{{ name }}</h2>
        <div class="slogan-box">
          <span class="slogan-text">{{ slogan }}</span>
        </div>
      </div>

      <!-- Right Side: Address lines -->
      <div class="content-right">
        <div class="address-zone">
          <div class="address-line">
            <span class="label">寄：</span>
            <span class="value">{{ teamName }}</span>
          </div>
          <div class="address-line">
            <span class="label">收：</span>
            <span class="value">2026全体家人</span>
          </div>
        </div>
      </div>
    </div>

    <!-- Footer -->
    <div class="footer-zone">
      <span class="vote-count">{{ votes }} 票</span>
    </div>
  </div>
</template>

<style lang="scss" scoped>
// Postcard base - Chinese postcard style (wider)
.postcard {
  position: relative;
  width: 100%;
  aspect-ratio: 1.5 / 1;
  background: #FDFBF7;
  border: 1px solid #2c2c2c;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  // Paper texture
  &::before {
    content: '';
    position: absolute;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%' height='100%' filter='url(%23noise)'/%3E%3C/svg%3E");
    opacity: 0.03;
    pointer-events: none;
  }
}

// Stamps layer (accumulated postmarks)
.stamps-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

.postmark-wrapper {
  position: absolute;
  width: 80px;
  height: 80px;
  
  &.is-new {
    animation: stamp-fly-in 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
  }
}

@keyframes stamp-fly-in {
  0% {
    transform: translate(-50%, -50%) translate(var(--fly-x, -200px), var(--fly-y, -200px)) scale(3) rotate(var(--fly-rotate, -45deg));
    opacity: 0;
  }
  70% {
    transform: translate(-50%, -50%) scale(0.9);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0.9;
  }
}

// Top Row: Zip codes + Stamp
.top-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  position: relative;
  z-index: 2;
  margin-bottom: 12px;
}

.zip-codes {
  display: flex;
  gap: 3px;
}

.zip-box {
  width: 22px;
  height: 26px;
  border: 1.5px solid #c41e3a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  font-weight: bold;
  color: #2c2c2c;
  background: #fff;
}

.stamp-box {
  width: 90px;
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid #ddd;
}

.stamp-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

// Content Row
.content-row {
  flex: 1;
  display: flex;
  justify-content: space-between;
  position: relative;
  z-index: 2;
}

// Left Side: Title + Slogan
.content-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding-right: 20px;
}

.program-name {
  font-family: 'SimSun', 'Songti SC', 'STSong', serif;
  font-size: 28px;
  font-weight: bold;
  color: #c41e3a;
  margin: 0 0 12px 0;
  letter-spacing: 6px;
}

.slogan-box {
  display: inline-block;
  border: 1px solid #ccc;
  padding: 6px 14px;
  align-self: flex-start;
  background: #fff;
}

.slogan-text {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: 12px;
  font-style: italic;
  color: #666;
}

// Right Side: Address
.content-right {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-start;
  min-width: 120px;
}

.address-zone {
  text-align: left;
  border-bottom: 1px solid #999;
  padding-bottom: 8px;
  margin-left: -25px;
  margin-top: -40px;
  width: 100%;
}

.address-line {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 6px;

  &:last-child {
    margin-bottom: 0;
  }

  .label {
    font-family: 'SimSun', 'Songti SC', serif;
    font-size: 11px;
    color: #666;
    min-width: 24px;
  }

  .value {
    font-family: 'Kaiti', 'STKaiti', serif;
    font-size: 13px;
    color: #2c2c2c;
  }
}

// Footer Zone
.footer-zone {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: auto;
  padding-top: 8px;
  position: relative;
  z-index: 2;

  .vote-count {
    font-family: 'SimSun', 'Songti SC', serif;
    font-size: 14px;
    font-weight: bold;
    color: #c41e3a;
    padding: 4px 12px;
    background: rgba(196, 30, 58, 0.08);
    border: 1px solid rgba(196, 30, 58, 0.2);
  }
}
</style>
