<script setup lang="ts">
import { onMounted, onUnmounted, watch } from 'vue'
import type { Word } from '../logic/wordlist'

const props = defineProps<{
  show: boolean
  word: Word | null
  isComplete: boolean
}>()

const emit = defineEmits<{
  close: []
  correct: []
  pass: []
}>()

function onKey(e: KeyboardEvent) {
  if (!props.show) return
  if (e.key === 'Enter') {
    e.preventDefault()
    emit('correct')
  } else if (e.key === ' ') {
    e.preventDefault()
    emit('pass')
  } else if (e.key === 'Escape') {
    e.preventDefault()
    emit('close')
  }
}

watch(
  () => props.show,
  (s) => {
    if (s) window.addEventListener('keydown', onKey)
    else window.removeEventListener('keydown', onKey)
  },
)

onMounted(() => {
  if (props.show) window.addEventListener('keydown', onKey)
})
onUnmounted(() => window.removeEventListener('keydown', onKey))
</script>

<template>
  <div v-if="show" class="overlay" @click="$emit('close')">
    <button class="close" @click.stop="$emit('close')">✕</button>
    <div class="word" v-if="!isComplete && word">
      <div class="chinese">{{ word.chinese }}</div>
      <div class="english">{{ word.english }}</div>
    </div>
    <div v-else class="done">
      <div class="emoji">🎉</div>
      <p>所有单词已完成</p>
    </div>
    <div class="hint">Enter 猜对 · Space 跳过 · Esc 关闭</div>
  </div>
</template>

<style scoped>
.overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  flex-direction: column;
}
.close {
  position: absolute;
  top: 20px;
  right: 20px;
  background: rgba(255, 255, 255, 0.15);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  font-size: clamp(20px, 3vw, 32px);
  padding: 10px 18px;
  border-radius: 8px;
}
.word {
  text-align: center;
  padding: 0 20px;
  max-width: 100%;
}
.chinese {
  font-size: clamp(56px, 20vw, 300px);
  font-weight: bold;
  line-height: 1.1;
  margin-bottom: clamp(16px, 5vh, 60px);
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  word-break: break-word;
}
.english {
  font-size: clamp(36px, 12vw, 200px);
  font-weight: 600;
  opacity: 0.9;
  line-height: 1.1;
  word-break: break-word;
}
.done { text-align: center; }
.done .emoji { font-size: 80px; margin-bottom: 12px; }
.hint {
  position: absolute;
  bottom: 16px;
  color: rgba(255, 255, 255, 0.4);
  font-size: 14px;
}
@media (max-width: 768px) {
  .hint { font-size: 12px; }
}
</style>
