<template>
  <div class="call-invite-overlay" :class="{ 'dark': themeStore.isDarkMode }">
    <div class="call-invite-dialog">
      <!-- 来电信息 -->
      <div class="caller-info">
        <div class="caller-avatar" :style="{ background: callerInfo.color }">
          {{ callerInfo.nick_name.charAt(0) }}
        </div>
        <div class="caller-details">
          <h3 class="caller-name">{{ callerInfo.nick_name }}</h3>
          <p class="call-type">{{ callType === 'video' ? '视频通话' : '语音通话' }}</p>
        </div>
      </div>

      <!-- 通话状态 -->
      <div class="call-status">
        <div class="status-text">{{ statusText }}</div>
        <div class="call-animation">
          <div class="pulse-ring"></div>
          <div class="pulse-ring delay-1"></div>
          <div class="pulse-ring delay-2"></div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="call-actions">
        <button
          v-if="isVisible && isIncoming"
          class="action-btn accept-btn"
          @click="acceptCall"
          :disabled="isProcessing"
        >
          <i class="fas fa-phone"></i>
          <span>接听</span>
        </button>

        <button
          class="action-btn reject-btn"
          @click="rejectCall"
          :disabled="isProcessing"
        >
          <i class="fas fa-phone-slash"></i>
          <span>{{ isVisible && isIncoming ? '拒绝' : '取消' }}</span>
        </button>
      </div>

      <!-- 等待状态 -->
      <div v-if="isVisible && isProcessing" class="processing-overlay">
        <div class="spinner"></div>
        <div class="processing-text">{{ processingText }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, computed, onMounted, onUnmounted} from 'vue'
import {useThemeStore} from '../stores/theme.ts'

const themeStore = useThemeStore()

const props = defineProps({
  isVisible: {
    type: Boolean,
    default: false
  },
  callerInfo: {
    type: Object,
    required: true
  },
  callType: {
    type: String,
    default: 'video',
    validator: (value) => ['video', 'audio'].includes(value)
  },
  isIncoming: {
    type: Boolean,
    default: true
  },
  callId: {
    type: String,
    required: true
  }
})

const emit = defineEmits(['accept', 'reject', 'timeout'])

const isProcessing = ref(false)
const timeoutTimer = ref(null)
const callDuration = ref(0)
const durationTimer = ref(null)

const statusText = computed(() => {
  if (isProcessing.value) {
    return '连接中...'
  }

  if (props.isIncoming) {
    return '邀请您进行通话'
  }

  return '等待对方接听...'
})

const processingText = computed(() => {
  if (props.isIncoming) {
    return '正在接听...'
  }
  return '正在取消...'
})

const acceptCall = async () => {
  if (isProcessing.value) return

  isProcessing.value = true

  try {
    await emit('accept', {
      callId: props.callId,
      callType: props.callType
    })
  } catch (error) {
    console.error('接听通话失败:', error)
    isProcessing.value = false
  }
}

const rejectCall = async () => {
  if (isProcessing.value) return

  isProcessing.value = true

  try {
    await emit('reject', {
      callId: props.callId,
      callType: props.callType,
      reason: props.isIncoming ? '用户拒绝' : '用户取消'
    })
  } catch (error) {
    console.error('拒绝通话失败:', error)
    isProcessing.value = false
  }
}

const startTimeout = () => {
  // 来电30秒超时，拨出60秒超时
  const timeout = props.isIncoming ? 30000 : 60000

  timeoutTimer.value = setTimeout(() => {
    emit('timeout', {
      callId: props.callId,
      reason: '超时未响应'
    })
  }, timeout)
}

const startDurationTimer = () => {
  durationTimer.value = setInterval(() => {
    callDuration.value++
  }, 1000)
}

const clearTimers = () => {
  if (timeoutTimer.value) {
    clearTimeout(timeoutTimer.value)
    timeoutTimer.value = null
  }

  if (durationTimer.value) {
    clearInterval(durationTimer.value)
    durationTimer.value = null
  }
}

// 键盘事件处理
const handleKeydown = (event) => {
  if (!props.isVisible) return

  switch (event.key) {
    case 'Enter':
      if (props.isIncoming) {
        acceptCall()
      }
      break
    case 'Escape':
      rejectCall()
      break
  }
}

onMounted(() => {
  if (props.isVisible) {
    startTimeout()
    document.addEventListener('keydown', handleKeydown)

    // 请求通知权限
    if ('Notification' in window && Notification.permission === 'default') {
      Notification.requestPermission()
    }
  }
})

onUnmounted(() => {
  clearTimers()
  document.removeEventListener('keydown', handleKeydown)
})
</script>

<style scoped>
.call-invite-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  animation: fadeIn 0.3s ease;
}

.call-invite-overlay.dark {
  background: rgba(0, 0, 0, 0.9);
}

.call-invite-dialog {
  background: white;
  border-radius: 24px;
  padding: 32px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  text-align: center;
  min-width: 320px;
  max-width: 400px;
  position: relative;
  animation: slideUp 0.3s ease;
}

.call-invite-overlay.dark .call-invite-dialog {
  background: #2d2d2d;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

.caller-info {
  margin-bottom: 32px;
}

.caller-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin: 0 auto 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 32px;
  font-weight: bold;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.caller-name {
  font-size: 24px;
  font-weight: 600;
  color: #1a202c;
  margin: 0 0 8px 0;
}

.call-invite-overlay.dark .caller-name {
  color: #f7fafc;
}

.call-type {
  font-size: 16px;
  color: #64748b;
  margin: 0;
}

.call-invite-overlay.dark .call-type {
  color: #9ca3af;
}

.call-status {
  margin-bottom: 32px;
  position: relative;
}

.status-text {
  font-size: 18px;
  color: #374151;
  margin-bottom: 24px;
  font-weight: 500;
}

.call-invite-overlay.dark .status-text {
  color: #d1d5db;
}

.call-animation {
  position: relative;
  width: 60px;
  height: 60px;
  margin: 0 auto;
}

.pulse-ring {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 60px;
  border: 2px solid #4361ee;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.pulse-ring.delay-1 {
  animation-delay: 0.5s;
}

.pulse-ring.delay-2 {
  animation-delay: 1s;
}

.call-actions {
  display: flex;
  gap: 24px;
  justify-content: center;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px;
  border: none;
  border-radius: 50%;
  width: 80px;
  height: 80px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 24px;
  position: relative;
  overflow: hidden;
}

.action-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.action-btn span {
  font-size: 12px;
  font-weight: 500;
  position: absolute;
  bottom: -24px;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.accept-btn {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
  box-shadow: 0 8px 24px rgba(16, 185, 129, 0.4);
}

.accept-btn:hover:not(:disabled) {
  transform: translateY(-4px);
  box-shadow: 0 12px 32px rgba(16, 185, 129, 0.5);
}

.reject-btn {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  color: white;
  box-shadow: 0 8px 24px rgba(239, 68, 68, 0.4);
}

.reject-btn:hover:not(:disabled) {
  transform: translateY(-4px);
  box-shadow: 0 12px 32px rgba(239, 68, 68, 0.5);
}

.processing-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.call-invite-overlay.dark .processing-overlay {
  background: rgba(45, 45, 45, 0.9);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e2e8f0;
  border-top: 3px solid #4361ee;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.processing-text {
  font-size: 16px;
  color: #374151;
  font-weight: 500;
}

.call-invite-overlay.dark .processing-text {
  color: #d1d5db;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(40px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(1.5);
    opacity: 0;
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .call-invite-dialog {
    margin: 20px;
    padding: 24px;
    min-width: unset;
    width: calc(100% - 40px);
  }

  .caller-avatar {
    width: 60px;
    height: 60px;
    font-size: 24px;
  }

  .caller-name {
    font-size: 20px;
  }

  .action-btn {
    width: 60px;
    height: 60px;
    font-size: 20px;
  }

  .call-actions {
    gap: 16px;
  }
}
</style>
