/**
 * WSClient - WebSocket连接管理
 * 只负责WebSocket连接管理，提供事件驱动架构
 */

import {
  wsService,
  WSChatMessage,
  WSGroupChatMessage,
  WSReadMessage,
  WSGroupReadMessage,
  WSRecallMessage,
  WSGroupRecallMessage,
  WSGroupTypingMessage,
  WSGroupNoticeMessage,
  WSMessageType,
} from '@/services/core';

// 事件处理器类型
type EventHandler<T> = (data: T) => void;

// WS事件类型
export interface WSEvents {
  'chat': WSChatMessage;
  'group_message': WSGroupChatMessage;
  'read': WSReadMessage;
  'group_read': WSGroupReadMessage;
  'recall': WSRecallMessage;
  'group_recall': WSGroupRecallMessage;
  'group_typing': WSGroupTypingMessage;
  'group_notice': WSGroupNoticeMessage;
  'connected': void;
  'disconnected': void;
  'error': Error;
}

export type WSEventType = keyof WSEvents;

class WSClient {
  private handlers: Map<WSEventType, Set<EventHandler<any>>> = new Map();
  private unsubscribeFns: Array<() => void> = [];
  private isInitialized = false;

  /**
   * 初始化WebSocket监听
   */
  initialize(): void {
    if (this.isInitialized) return;

    // 监听私聊消息
    const unsubChat = wsService.on('chat', (message) => {
      this.emit('chat', message);
    });

    // 监听群聊消息
    const unsubGroupMessage = wsService.on('group_message', (message) => {
      this.emit('group_message', message);
    });

    // 监听私聊已读回执
    const unsubRead = wsService.on('read', (message) => {
      this.emit('read', message);
    });

    // 监听群聊已读回执
    const unsubGroupRead = wsService.on('group_read', (message) => {
      this.emit('group_read', message);
    });

    // 监听私聊消息撤回
    const unsubRecall = wsService.on('recall', (message) => {
      this.emit('recall', message);
    });

    // 监听群聊消息撤回
    const unsubGroupRecall = wsService.on('group_recall', (message) => {
      this.emit('group_recall', message);
    });

    // 监听群聊输入状态
    const unsubGroupTyping = wsService.on('group_typing', (message) => {
      this.emit('group_typing', message);
    });

    // 监听群通知
    const unsubGroupNotice = wsService.on('group_notice', (message) => {
      this.emit('group_notice', message);
    });

    // 监听连接状态
    const unsubConnect = wsService.onConnect(() => {
      this.emit('connected', undefined);
    });

    const unsubDisconnect = wsService.onDisconnect(() => {
      this.emit('disconnected', undefined);
    });

    this.unsubscribeFns = [
      unsubChat,
      unsubGroupMessage,
      unsubRead,
      unsubGroupRead,
      unsubRecall,
      unsubGroupRecall,
      unsubGroupTyping,
      unsubGroupNotice,
      unsubConnect,
      unsubDisconnect,
    ];

    this.isInitialized = true;
  }

  /**
   * 销毁WebSocket监听
   */
  destroy(): void {
    this.unsubscribeFns.forEach((fn) => fn());
    this.unsubscribeFns = [];
    this.handlers.clear();
    this.isInitialized = false;
  }

  /**
   * 订阅事件
   */
  on<T extends WSEventType>(
    event: T,
    handler: EventHandler<WSEvents[T]>
  ): () => void {
    if (!this.handlers.has(event)) {
      this.handlers.set(event, new Set());
    }
    this.handlers.get(event)!.add(handler);

    return () => {
      this.handlers.get(event)?.delete(handler);
    };
  }

  /**
   * 触发事件
   */
  private emit<T extends WSEventType>(
    event: T,
    data: WSEvents[T]
  ): void {
    const handlers = this.handlers.get(event);
    if (handlers) {
      handlers.forEach((handler) => {
        try {
          handler(data);
        } catch (error) {
          console.error(`[WSClient] 事件处理器执行失败: ${event}`, error);
        }
      });
    }
  }

  /**
   * 检查连接状态
   */
  isConnected(): boolean {
    return wsService.isConnected();
  }

  /**
   * 启动WebSocket连接
   */
  async connect(): Promise<boolean> {
    return wsService.start();
  }

  /**
   * 断开WebSocket连接
   */
  disconnect(): void {
    wsService.stop();
  }
}

export const wsClient = new WSClient();
export default wsClient;