<script setup lang="ts">
import { computed, ref, watch, onUnmounted } from 'vue'

const props = defineProps<{
  visible: boolean
  prompt: string
  keys: string[]
  total: number
  remaining: number
  result: 'none' | 'success' | 'fail'
}>()

const emit = defineEmits<{
  done: []
}>()

const ringRadius = 60
const ringStroke = 4
const normalizedRadius = ringRadius - ringStroke * 2
const circumference = normalizedRadius * 2 * Math.PI

const progress = computed(() => {
  if (props.total <= 0) return circumference
  return (props.remaining / props.total) * circumference
})

const keyLabels = computed(() => props.keys.map(displayKey))

function displayKey(key: string): string {
  const map: Record<string, string> = {
    ' ': '空格',
    ArrowUp: '↑',
    ArrowDown: '↓',
    ArrowLeft: '←',
    ArrowRight: '→',
    Enter: '回车',
    Escape: 'Esc',
  }
  return map[key] ?? key.toUpperCase()
}

watch(() => props.result, (val) => {
  if (val !== 'none') {
    setTimeout(() => emit('done'), 800)
  }
})
</script>

<template>
  <div class="qte-overlay" v-if="visible">
    <div class="qte-center">
      <div class="qte-ring" :class="{ 'qte-success': result === 'success', 'qte-fail': result === 'fail' }">
        <svg width="140" height="140" viewBox="0 0 140 140">
          <circle
            stroke="rgba(255,255,255,0.15)"
            fill="none"
            :stroke-width="ringStroke"
            :r="normalizedRadius"
            :cx="ringRadius + ringStroke"
            :cy="ringRadius + ringStroke"
          />
          <circle
            class="ring-progress"
            stroke="#fff"
            fill="none"
            :stroke-width="ringStroke"
            :r="normalizedRadius"
            :cx="ringRadius + ringStroke"
            :cy="ringRadius + ringStroke"
            :stroke-dasharray="circumference + ' ' + circumference"
            :stroke-dashoffset="circumference - progress"
            stroke-linecap="round"
            transform="rotate(-90 64 64)"
          />
        </svg>
        <div class="qte-key">{{ keyLabels[0] }}</div>
        <div class="qte-time">{{ remaining.toFixed(1) }}s</div>
      </div>
      <div class="qte-prompt">{{ prompt }}</div>
      <div class="qte-result-text" v-if="result === 'success'">成功</div>
      <div class="qte-result-text fail-text" v-else-if="result === 'fail'">失败</div>
    </div>
  </div>
</template>

<style scoped>
.qte-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
}

.qte-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.qte-ring {
  position: relative;
  width: 140px;
  height: 140px;
}

.qte-ring .ring-progress {
  transition: stroke-dashoffset 50ms linear, stroke 0.3s;
}

.qte-ring.qte-success .ring-progress {
  stroke: #4caf50;
}

.qte-ring.qte-fail .ring-progress {
  stroke: #e74c3c;
}

.qte-key {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -60%);
  font-size: 28px;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 0 12px rgba(255,255,255,0.4);
}

.qte-time {
  position: absolute;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: #aaa;
}

.qte-prompt {
  font-size: 18px;
  color: #ddd;
  letter-spacing: 2px;
}

.qte-result-text {
  font-size: 32px;
  letter-spacing: 4px;
  color: #4caf50;
  font-weight: bold;
}

.qte-result-text.fail-text {
  color: #e74c3c;
}
</style>
