import { useRtcStore } from "/@/store/modules/rtc";
import type { ChatContentType, UserInfoType } from "/@/store/modules/rtc";
import Dialog from '/@/components/basic/dialog'
import { getApp, useApp } from "/@/hooks/userApp";
import consola from 'consola'
import { useSocket } from "/@/hooks/userSocket";
import { useRoom } from "/@/hooks/useRoom";


interface ActionDataType {
    type: string;
    data: any,
    members?: UserInfoType[],
    open?: boolean,
    user?: UserInfoType
}

export function handleActions({ data, type, members, open, user }: ActionDataType) {
    switch (type) {
        case 'user-init':
            handleUserInit()
            break;
        case 'danmumsg':
            handleDanMumSg(data)
            break;

        case "changeScene":
            handleChangeScene(data)
            break;

        case 'user-join':
            handleUserJoin(members, user)
            break;
        case "user-leave":
            break;

        case "users-muted":
            break

        case "users-words":
            break;
        case "leader-dismiss":
            break;
        case "user-paint":
            handleUserPaint(open)
            break;
        case 'tagclick':
            break;
        case 'tagclose':
            break;
        case 'tag-image-index':
            break;

        case 'ask-currentscene':
            break;
        default:
            break;
    }

}

//被动处理初始化
async function handleUserInit() {
    const rtcStore = useRtcStore();
    if (rtcStore.role === 'leader') {
        const app = await useApp();
        app.Connect.follow.sync();
        // setTimeout(() => {
        //     socket.value.emit("action", {
        //       type: "leader-avatar",
        //       data: { avatar: leaderAvatar.value },
        //     });
        //     if (tagclick.value && tagclick.value.type == "goodlist") {
        //       socket.value.emit("action", {
        //         type: "tagclick",
        //         data: {
        //           sid: tagclick.value.data.sid,
        //         },
        //       });
        //       if (tagImageIndex.value != null) {
        //         setTimeout(() => {
        //           socket.value.emit("action", {
        //             type: "tag-image-index",
        //             data: {
        //               index: tagImageIndex.value,
        //             },
        //           });
        //         }, 200);
        //       }
        //     }
        //   }, 200);
    }

}

// 被动处理弹幕
function handleDanMumSg(data: ChatContentType) {
    const rtcStore = useRtcStore();
    if (data.role == "leader") {
        data.Nickname = "主持人";
    }
    rtcStore.addToChatList(data)
}
// 其他用户进入
function handleUserJoin(members?: UserInfoType[], user?: UserInfoType) {

    consola.info({
        tag: 'socket',
        message: '有人进来了'
    })
    const rtcStore = useRtcStore();

    members && rtcStore.setMemberList(members)
    if (user) {
        let name = user.Nickname;
        if (user.Role === 'leader') {
            const socket = useSocket()
            name = "主持人";
            Dialog.toast({ content: `主持人进入房间` });
            socket.emit("action", { type: "user-init" });
        }
        let data: ChatContentType = {
            role: user?.Role,
            mode: rtcStore.mode,
            Nickname: name,
            UserId: user?.UserId,
            text: "进入房间",
        };
        if (rtcStore.isLeader) {
            rtcStore.addToChatList(data)
        }
    }

}

// 其他用户开起画笔通知

async function handleUserPaint(open: boolean | undefined) {
    const app = await useApp();
    const rtcStore = useRtcStore();
    if (!rtcStore.isLeader) {
        if (open) {
            console.log(open)
            app.Connect.paint.show({
                role: rtcStore.role,
                paint: false,
            })
            Dialog.toast({ content: `主持人开启画笔` });
        } else {
            Dialog.toast({ content: `主持人关闭画笔` });
            app.Connect.paint.hide();
        }
    }

}

const handleChangeScene = (data) => {
    const { changeScene } = useRoom()
    // debugger
    changeScene(data)

}