<template>
  <div class="chat-header" :class="{ 'dark': themeStore.isDarkMode }">
    <div class="header-content">
      <div class="friend-info">
        <div
            class="friend-avatar"
            :style="{ background: chatStore.currentFriend?.color }"
        >
          {{ chatStore.currentFriend?.name.charAt(0) }}
        </div>

        <div class="friend-details">
          <h3 class="friend-name">{{ chatStore.currentFriend?.name }}</h3>
          <p class="friend-status">
                        <span
                            class="status-dot"
                            :class="isFriendOnline ? 'online' : 'offline'"
                        ></span>
            <span class="status-text">
                            {{ isFriendOnline ? '在线' : '离线' }}
                        </span>
          </p>
        </div>
      </div>

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

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

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

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

    <!-- 视频通话组件 -->
    <VideoCallComponent
        v-if="showVideoCall"
        :caller-info="chatStore.currentFriend"
        :call-type="callType"
        :is-incoming="false"
        @end-call="endCall"
    />
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useChatStore } from '@/stores/chat';
import { useThemeStore } from '@/stores/theme';
import VideoCallComponent from './VideoCallComponent.vue';

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

const isFriendOnline = ref(true);
const showMoreActions = ref(false);
const showVideoCall = ref(false);
const callType = ref('video');

// 点击外部关闭菜单
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;
  }

  callType.value = 'audio';
  showVideoCall.value = true;
  console.log('开始语音通话:', chatStore.currentFriend.name);
};

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

  callType.value = 'video';
  showVideoCall.value = true;
  console.log('开始视频通话:', chatStore.currentFriend.name);
};

// 结束通话
const endCall = () => {
  showVideoCall.value = false;
  console.log('通话结束');
};

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

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

<style scoped>
.chat-header {
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  padding: 16px 24px;
  position: relative;
}

.chat-header.dark {
  background: #2d2d2d;
  border-bottom-color: #374151;
}

.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;
  color: #1a202c;
  margin: 0;
}

.chat-header.dark .friend-name {
  color: #f7fafc;
}

.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 {
  color: #64748b;
}

.chat-header.dark .status-text {
  color: #9ca3af;
}

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

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

.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;
}

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

.chat-header.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;
}

.chat-header.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;
}

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

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

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

.chat-header.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 {
    padding: 12px 16px;
  }

  .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;
  }
}
</style>
