import consolaGlobalInstance from 'consola';
// import sortBy from 'lodash-es/sortBy';
import { unref, ref } from 'vue';
import { useI18n } from '/@/hooks/useI18n';
// import { unref } from 'vue';
// import Dialog from '/@/components/basic/dialog';
// import { getApp } from '/@/hooks/userApp';
import { roomId, useRoom } from '/@/hooks/useRoom';
import { useSocket } from '/@/hooks/userSocket';
import { UserInfoType, useRtcStore } from '/@/store/modules/rtc';
import { useAppStore } from '/@/store/modules/app';
// import { useRtcSdk } from '/@/hooks/useTRTC';
// import { useMiniApp } from '/@/hooks/useMiniApp';
const { enterRoom } = useRoom();
const isInternational = ref(import.meta.env.VITE_IS_INTERNATIONAL);
const is404Page = ref(import.meta.env.VITE_404_PAGE);

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

  // const userExsit = rtcStore.checkUserInMemberList(rtcStore.userId);
  // banLaterEnterer(members, user);
  const res = await enterRoom();
  const { code } = res.data;

  if (code === 4003 && !roomId.includes('temp_') && unref(isInternational)) {
    location.href = is404Page.value;
    return;
  }

  rtcStore.setIsJoined(true);
  consolaGlobalInstance.info({
    message: data,
    tag: 'socket:enter',
  });

  const { room } = useRoom();
  const maxNumber = unref(room)?.maxMan || 10;
  console.log('maxMan:', maxNumber, members.length, Number(maxNumber));

  if (members.length > Number(maxNumber)) {
    if (rtcStore.role === 'customer') {
      const { t } = useI18n();
      rtcStore.showBaseDialog(
        {
          title: t('base.tips'),
          desc: t('base.maxRooms'),
          okTxt: t('base.confirm'),
          closeTxt: t('base.cancel'),
          isSingle: true,
        },
        () => {
          setTimeout(() => {
            closeSocket();
          }, 1500);
        },
      );
      setTimeout(() => {
        closeSocket();
      }, 3000);
    }
  } else {
    members && rtcStore.setMemberList(members);
  }

  rtcStore.setConnectStatus(1);

  setTimeout(() => {
    if (!rtcStore.isLeader) {
      socket.emit('action', {
        type: 'user-init',
      });
      socket.emit('action', {
        type: 'ask-currentscene',
      });
    } else {
      const { currentScene } = useRoom();
      console.log('currentScene', unref(currentScene));
      socket.emit('action', {
        type: 'changeScene',
        data: unref(currentScene),
      });
      console.log('进入，主持人禁音-1');
    }

    const appStore = useAppStore();
    console.log('检查是否输入过密码', appStore.isPassWordConfirm);
    if (appStore.isPassWordConfirm) {
      socket.emit('action', {
        type: 'confirm-password',
        data: {
          userId: rtcStore.userId,
        },
      });
    }
  }, 1500);
}

// const banLaterEnterer = (members: UserInfoType[], user: UserInfoType) => {
//   if (members && user) {
//     console.log('banLaterEnterer', members);
//     const sameIDsArray = members.some((item) => item.UserId === user.UserId);
//     const sortList = sortBy(sameIDsArray, ['InTime'], ['asc']);
//     console.log('banLaterEnterer', sortList);
//     // const beforeUSer =
//   }
// };
