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

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

export function handleActions({
  data,
  type,
  members,
  open,
  user,
  userId,
  words,
  muted,
}: 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':
      handleUserLeave(user, members);
      break;

    case 'users-muted':
      handleUserMuted(userId, muted, members);
      break;

    case 'users-words':
      handleUserSetWords(userId, words, members);
      break;
    case 'leader-dismiss':
      handleLeaderDismiss();
      break;
    case 'user-paint':
      handleUserPaint(open);
      break;
    case 'tagclick':
      break;
    case 'tagclose':
      break;
    case 'tag-image-index':
      handleTagImageIndex(data);
      break;
    case 'ask-currentscene':
      handleAskCurrentscene(data);
      break;
    case 'error':
      // Dialog.toast({ content: `房间未找到`, type: 'error' });
      handleError();
    default:
      break;
  }
  if (members?.length) {
    console.log('发烧的', members);
  }
}

//被动处理初始化
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();
  // const { closeSocket } = useSocket();
  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' });
    }
    const 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: SceneItemType) => {
  const { changeScene } = useRoom();
  changeScene(data);
};
//
const handleAskCurrentscene = (data) => {
  console.log('data', data);
};

const handleTagImageIndex = (data) => {
  console.log('data', data);
};

const handleUserSetWords = (UserId?: string, words?: boolean, members?: UserInfoType[]) => {
  console.log('members', members);
  const rtcStore = useRtcStore();
  console.log('handleUserSetWords', UserId, words);
  UserId &&
    rtcStore.updateMemberDatabyId(UserId, {
      IsWords: words,
    });
  if (UserId == rtcStore.userId) {
    Dialog.toast({
      content: !words ? `主持人设置了禁言` : `主持人已解除禁言`,
    });
  }
};
const handleUserMuted = (UserId?: string, muted?: boolean, members?: UserInfoType[]) => {
  const rtcStore = useRtcStore();
  console.log('members', members);
  console.log('handleUserSetWords', UserId, muted);
  UserId &&
    rtcStore.updateMemberDatabyId(UserId, {
      IsMuted: muted,
    });
  if (UserId == rtcStore.userId) {
    rtcStore.setMute(!!muted);
  }
};

//被动处理用离开
const handleUserLeave = (user?: UserInfoType, members?: UserInfoType[]) => {
  console.log('有人离开了', user?.UserId);
  const rtcStore = useRtcStore();
  if (user) {
    let name = user.Nickname;
    if (user.Role == 'leader') {
      name = '主持人';
      Dialog.toast({ content: `主持人离开了房间` });
    }

    const data = {
      role: user.Role,
      mode: '',
      Nickname: name,
      UserId: user.UserId,
      text: '离开房间',
    };

    console.log('members', user, members);
    rtcStore.addToChatList(data);
    members && rtcStore.setMemberList(members);
  }
};
//被动处理用离开 全员解散
const handleLeaderDismiss = () => {
  const { closeSocket } = useSocket();
  Dialog.toast({ content: `主持人已解散房间` });
  closeSocket();
};

//

const handleError = () => {
  const rtcStore = useRtcStore();
  rtcStore.showBaseDialog(
    {
      title: '温馨提示',
      desc: '带看已结束',
      okTxt: '确定',
      closeTxt: '取消',
      isSingle: true,
    },
    () => {
      const { closeSocket } = useSocket();
      closeSocket();
    },
  );
}