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

import { Avatar } from 'ant-design-vue';

import { useChatStore } from '../stores/chat.ts';
import { useThemeStore } from '../stores/theme.ts';

const chatStore = useChatStore();
const themeStore = useThemeStore();

const isFriendOnline = ref(true);
const showMoreActions = ref(false);

// 获取当前通话状态
const callStatus = computed(() => {
  if (!chatStore.currentFriend) return null;

  // 检查是否正在与当前好友通话
  if (
    chatStore.currentCall &&
    chatStore.currentCall.peerId === chatStore.currentFriend.id
  ) {
    return chatStore.callConnectionStatus || '通话中';
  }

  return null;
});

// 获取通话状态文本
const callStatusText = computed(() => {
  if (!callStatus.value) return '';

  switch (callStatus.value) {
    case '对方已拒绝': {
      return '❌ 已拒绝';
    }
    case '对方忙线中': {
      return '🚫 忙线中';
    }
    case '对方无人接听': {
      return '🕒 未接听';
    }
    case '等待对方接听...': {
      return '📞 呼叫中...';
    }
    case '通话中': {
      return '📞 通话中';
    }
    default: {
      return `📞 ${callStatus.value}`;
    }
  }
});

// 点击外部关闭菜单
const handleClickOutside = (e) => {
  if (
    !e.target.closest('.more-btn') &&
    !e.target.closest('.more-actions-menu')
  ) {
    showMoreActions.value = false;
  }
};

document.addEventListener('click', handleClickOutside);

// 开始语音通话
const startVoiceCall = () => {
  if (!chatStore.currentFriend) {
    alert('请先选择一个好友');
    return;
  }

  chatStore.startCall(chatStore.currentFriend.id, 'audio');
};

// 开始视频通话
const startVideoCall = () => {
  if (!chatStore.currentFriend) {
    alert('请先选择一个好友');
    return;
  }

  chatStore.startCall(chatStore.currentFriend.id, 'video');
};

// 清空聊天记录
const clearChatHistory = () => {
  if (confirm('确定要清空与该好友的聊天记录吗？')) {
    chatStore.messages = [];
    showMoreActions.value = false;
    console.log('聊天记录已清空');
  }
};

// 查看好友资料
const viewFriendProfile = () => {
  showMoreActions.value = false;
  console.log('查看好友资料:', chatStore.currentFriend);
};
</script>

<template>
  <div class="chat-header bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700 px-6 py-4">
    <div class="header-content">
      <div class="friend-info">
        <div
          :style="{ background: chatStore.currentFriend?.color }"
          class="friend-avatar"
        >
          <Avatar
            v-if="chatStore.currentFriend?.avatar"
            :size="48"
            :src="chatStore.currentFriend?.avatar"
          />
          <Avatar v-else :size="48">
            {{ chatStore.currentFriend?.nick_name.charAt(0) }}
          </Avatar>
        </div>

        <div class="friend-details">
          <h3 class="friend-name">{{ chatStore.currentFriend?.nick_name }}</h3>
          <p class="friend-status">
            <span
              :class="isFriendOnline ? 'online' : 'offline'"
              class="status-dot"
            ></span>
            <span class="status-text">
              {{ isFriendOnline ? '在线' : '离线' }}
            </span>
            <span v-if="callStatus" class="call-status-text text-blue-500 dark:text-blue-300 bg-blue-50 dark:bg-blue-900/20">
              {{ callStatusText }}
            </span>
          </p>
        </div>
      </div>

      <div class="header-actions">
<!--        &lt;!&ndash; 语音通话按钮 &ndash;&gt;-->
<!--        <button-->
<!--          class="action-btn voice-call-btn"-->
<!--          title="语音通话"-->
<!--          @click="startVoiceCall"-->
<!--        >-->
<!--          <i class="fas fa-phone"></i>-->
<!--        </button>-->

<!--        &lt;!&ndash; 视频通话按钮 &ndash;&gt;-->
<!--        <button-->
<!--          class="action-btn video-call-btn"-->
<!--          title="视频通话"-->
<!--          @click="startVideoCall"-->
<!--        >-->
<!--          <i class="fas fa-video"></i>-->
<!--        </button>-->

        <!-- 更多操作 -->
<!--        <button-->
<!--          class="action-btn more-btn"-->
<!--          title="更多操作"-->
<!--          @click="showMoreActions = !showMoreActions"-->
<!--        >-->
<!--          <i class="fas fa-ellipsis-v"></i>-->
<!--        </button>-->

<!--        &lt;!&ndash; 更多操作菜单 &ndash;&gt;-->
<!--        <div v-if="showMoreActions" class="more-actions-menu">-->
<!--&lt;!&ndash;          <div class="menu-item" @click="clearChatHistory">&ndash;&gt;-->
<!--&lt;!&ndash;            <i class="fas fa-trash"></i>&ndash;&gt;-->
<!--&lt;!&ndash;            <span>清空聊天记录</span>&ndash;&gt;-->
<!--&lt;!&ndash;          </div>&ndash;&gt;-->
<!--          <div class="menu-item" @click="viewFriendProfile">-->
<!--            <i class="fas fa-user"></i>-->
<!--            <span>查看资料</span>-->
<!--          </div>-->
<!--        </div>-->
      </div>
    </div>
  </div>
</template>

<style scoped>
.chat-header {
  position: relative;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.friend-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.friend-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 18px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.friend-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.friend-name {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.friend-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-size: 14px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.status-dot.online {
  background: #10b981;
}

.status-dot.offline {
  background: #ef4444;
}

.status-text {
  /* 颜色已在模板中使用 Tailwind 类 */
}

.status-text.online {
  color: #10b981;
}

.status-text.offline {
  color: #ef4444;
}

/* 添加通话状态样式 */
.call-status-text {
  margin-left: 8px;
  font-size: 13px;
  font-weight: 500;
  padding: 2px 6px;
  border-radius: 4px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
}

.action-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 16px;
  position: relative;
}

.voice-call-btn {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.voice-call-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.4);
}

.video-call-btn {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.video-call-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
}

.more-btn {
  background: #f1f5f9;
  color: #64748b;
  border: 1px solid #e2e8f0;
}

.more-btn:hover {
  background: #e2e8f0;
  color: #475569;
}

.dark .more-btn {
  background: #374151;
  color: #9ca3af;
  border-color: #4b5563;
}

.dark .more-btn:hover {
  background: #4b5563;
  color: #d1d5db;
}

.more-actions-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 8px 0;
  min-width: 160px;
  z-index: 1000;
  margin-top: 8px;
}

.dark .more-actions-menu {
  background: #374151;
  border-color: #4b5563;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #374151;
  font-size: 14px;
}

.menu-item:hover {
  background: #f8fafc;
}

.dark .menu-item {
  color: #d1d5db;
}

.dark .menu-item:hover {
  background: #4b5563;
}

.menu-item i {
  width: 16px;
  font-size: 14px;
  color: #64748b;
}

.dark .menu-item i {
  color: #9ca3af;
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.7;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-header {
    @apply px-4 py-3;
  }

  .friend-avatar {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  .friend-name {
    font-size: 16px;
  }

  .action-btn {
    width: 36px;
    height: 36px;
    font-size: 14px;
  }

  .header-actions {
    gap: 8px;
  }

  .call-status-text {
    font-size: 11px;
  }
}
</style>
