import consolaGlobalInstance from 'consola';
import Dialog from '/@/components/basic/dialog';
// import { getApp } from '/@/hooks/userApp';
import { useRoom } from '/@/hooks/useRoom';
import { useSocket } from '/@/hooks/userSocket';
import { UserInfoType, useRtcStore } from '/@/store/modules/rtc';
const { enterRoom } = useRoom();

// 自已join的方法
export function handleJoin(data: any) {
  // const app = getApp();
  const { socket } = useSocket();
  const members = data?.members as any as UserInfoType[];
  const user = data?.user as any as UserInfoType;
  enterRoom();

  const rtcStore = useRtcStore();
  rtcStore.setIsJoined(true);
  consolaGlobalInstance.info({
    message: data,
    tag: 'socket:enter',
  });
  if (user) {
    console.log('gem1', rtcStore.userId);
    console.log('gem2', rtcStore.memberList);
    const userExsit = rtcStore.checkUserInMemberList(rtcStore.userId);
    console.log('gem3', userExsit, user.Id !== userExsit?.Id);
    if (userExsit) {
      // Dialog.confirm({
      //   showCloseIcon: false,
      //   okText: '我知道了',
      //   content:
      //     "<span style='font-size: 16px; line-height: 1.5;'>您已有登录端在登录，请不要重复登录<span/>",
      //   title: '隱私條款：',
      //   single: true,
      //   func: (state) => {},
      // });
    }
  } else {
  }

  members && rtcStore.setMemberList(members);
  if (!rtcStore.isLeader) {
    setTimeout(() => {
      socket.emit('action', {
        type: 'user-init',
      });
    }, 1500);
  }
}
