package com.yushu.web.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

/**
 * 消息通知服务
 * 负责通过WebSocket推送消息给用户
 */
@Component
public class MessageNotifier {
    
    private static final Logger log = LoggerFactory.getLogger(MessageNotifier.class);
    
    /**
     * 发送新消息通知给指定用户
     */
    public void notifyNewMessage(Long userId, Object messageData, int unreadCount) {
        Map<String, Object> notification = Map.of(
            "type", "new_message",
            "payload", messageData,
            "unreadCount", unreadCount,
            "timestamp", System.currentTimeMillis()
        );
        
        MessageWebSocketServer.sendToUser(userId, notification);
        log.debug("[MessageNotifier] 发送新消息通知给用户: {}", userId);
    }
    
    /**
     * 批量发送新消息通知
     */
    public void notifyNewMessage(Collection<Long> userIds, Object messageData) {
        if (userIds == null || userIds.isEmpty()) {
            return;
        }
        
        Map<String, Object> notification = Map.of(
            "type", "new_message",
            "payload", messageData,
            "timestamp", System.currentTimeMillis()
        );
        
        MessageWebSocketServer.sendToUsers(userIds, notification);
        log.info("[MessageNotifier] 批量发送新消息通知给 {} 个用户: {}", userIds.size(), userIds);
    }
    
    /**
     * 发送消息已读通知
     */
    public void notifyMessageRead(Long userId, Collection<Long> messageIds) {
        Map<String, Object> notification = Map.of(
            "type", "message_read",
            "payload", Map.of("messageIds", messageIds),
            "timestamp", System.currentTimeMillis()
        );
        
        MessageWebSocketServer.sendToUser(userId, notification);
        log.debug("[MessageNotifier] 发送消息已读通知给用户: {}", userId);
    }
    
    /**
     * 发送未读数更新通知
     */
    public void notifyUnreadCountUpdate(Long userId, int unreadCount) {
        Map<String, Object> notification = Map.of(
            "type", "unread_count_update",
            "payload", Map.of("unreadCount", unreadCount),
            "timestamp", System.currentTimeMillis()
        );
        
        MessageWebSocketServer.sendToUser(userId, notification);
        log.debug("[MessageNotifier] 发送未读数更新通知给用户: {} ({})", userId, unreadCount);
    }
    
    /**
     * 发送系统通知
     */
    public void notifySystemMessage(Collection<Long> userIds, String title, String content, String priority) {
        if (userIds == null || userIds.isEmpty()) {
            return;
        }
        
        Map<String, Object> systemMsg = Map.of(
            "title", title,
            "content", content,
            "priority", priority,
            "messageType", "system"
        );
        
        Map<String, Object> notification = Map.of(
            "type", "system_notification",
            "payload", systemMsg,
            "timestamp", System.currentTimeMillis()
        );
        
        MessageWebSocketServer.sendToUsers(userIds, notification);
        log.info("[MessageNotifier] 发送系统通知给 {} 个用户: {}", userIds.size(), title);
    }
    
    /**
     * 获取当前在线用户数
     */
    public int getOnlineUserCount() {
        return MessageWebSocketServer.getOnlineCount();
    }
    
    /**
     * 获取在线用户列表
     */
    public Collection<Long> getOnlineUsers() {
        return MessageWebSocketServer.getOnlineUsers();
    }
}
