import { useRtcStore } from "/@/store/modules/rtc";
import consola from 'consola'
import type { ConsolaLogObject } from 'consola'
import { handleActions, handleSync, handlePaint } from './controls'

// 所有socket业务事件集中点

export function initSocketEvent(socket: SocketIOClient.Socket): void {
    if (socket) {
        socket.on('connect', () => {
            const rtcStore = useRtcStore();
            let params = {
                userId: rtcStore.userId,
                roomId: rtcStore.roomId,
                role: rtcStore.role || "leader",
                avatar: rtcStore.avatar,
                nickname: rtcStore.nickname,
            };
            socket.emit('join', params)
        })


        socket.on('action', (data: any) => {
            const actionLog: ConsolaLogObject = {
                message: data,
                tag: `action-${data.type || ''}`,
                level: 3
            }
            consola.info(actionLog)
            handleActions(data)
        })
        // 同屏帶看
        socket.on('sync', handleSync)
        // 畫筆
        socket.on('paint', handlePaint)

        socket.on('onAny', (event: any) => {
            console.error('onAny:-->', event)
        })
    } else {
        throw new Error('socket没有初始化！')
    }
}