import EventEmitter from"../ppt/eventEmitter";import EVENT from"./EVENT";import api from"../api/index";import chatApi from"../chat/api";export default class SocketEvent extends EventEmitter{constructor(t){super(),this.socket=t.socket,this.userId=t.userId,this.reciveSliceIdEvent=!1,this.setUid=t.uid;const s=t.userId;this.user={nick:t.nick,pic:t.pic,userId:t.uid||s,userType:t.userType||"slice"},this.roomInfo={roomId:t.roomId.toString(),type:"video",masterRoomId:t.masterRoomId},this.apiRoomId=this.roomInfo.masterRoomId||this.roomInfo.roomId,this.sockEventListener={},this.initSocketListener();const e=()=>{this.statusTimeout=setTimeout((()=>{this.initStatusPollingListener(),e()}),2e4)};this.initStatusPollingListener(),e()}trigger(t,s,...e){let i;s?(i=Object.assign({},s),i.EVENT=t):i={EVENT:t},i.EVENT=t,this.emit(t,i,...e)}emitSocketMessage(t,s,e=""){if(void 0===this.roomInfo)return;const{roomInfo:i,user:o}=this;let r=s;if("joinRequest"===t){const t=setTimeout((()=>{this.trigger(EVENT.JOIN_CHANNEL_TIMEOUT)}),2e4);r=e=>{clearTimeout(t),s(e)}}this.socket.emit(t,JSON.stringify({user:o,roomId:i.roomId,sessionId:i.sessionId}),r)}initSocketListener(){const t=this.sockEventListener;this.socket.on("message",t.message=async t=>{const s=JSON.parse(t);if("OPEN_MICROPHONE"===s.EVENT){const t={type:s.type,status:s.status};"open"===t.status?(this.micStatus=t.status,this.roomInfo.teacherId=s.teacherId,this.roomInfo.type=s.type,this.reciveSliceIdEvent||await this.setSessionId(),this.trigger(EVENT.OPEN_MICROPHONE,t)):"userId"in s&&s.userId===this.user.userId?this.trigger(EVENT.CLIENT_BANNED):"close"!==t.status||s.userId||(this.micStatus=t.status,this.trigger(EVENT.CLOSE_MICROPHONE,t))}else if("onSliceID"===s.EVENT||"onSliceStart"===s.EVENT){const t="onSliceID"===s.EVENT?s.data:s;t&&(this.roomInfo.sessionId=t.sessionId,this.user.sessionId=t.sessionId,this.reciveSliceIdEvent=!0)}else"O_TEACHER_INFO"===s.EVENT&&(this.roomInfo.teacherId=s.data.userId,this.trigger(EVENT.LOGIN),this.trigger(EVENT.O_TEACHER_INFO,s.data))}),this.socket.on("joinResponse",t.joinResponse=t=>{const s=JSON.parse(t);s&&this.trigger(EVENT.ALLOW_MICROPHONE,s)}),this.socket.on("joinSuccess",t.joinSuccess=t=>{const s=JSON.parse(t);s&&this.trigger(EVENT.SUCCESS_MICROPHONE,s)}),this.socket.on("MuteUserMedia",t.MuteUserMedia=t=>{const s=JSON.parse(t);s.EVENT=`MUTEUSER_${s.type.toLocaleUpperCase()}`,"video"===s.type?this.trigger(EVENT.MUTEUSER_VIDEO,s):this.trigger(EVENT.MUTEUSER_AUDIO,s)}),this.socket.on("switchView",t.switchView=t=>{const s=JSON.parse(t);this.trigger(EVENT.SWITCHVIEW,s)})}removeSocketListener(){this.statusTimeout&&clearTimeout(this.statusTimeout)}static async getSessionIdByChannelId(t){return(await api.getLatestSessionId({channelId:t})).data}async setSessionId(){const t=await SocketEvent.getSessionIdByChannelId(this.apiRoomId);this.roomInfo.sessionId=t,this.user.sessionId=t}async checkCurrentStatus(){return(await chatApi.checkCurrentStatus2(this.apiRoomId)).data}async initStatusPollingListener(){let t;try{t=await this.checkCurrentStatus()}catch(t){return void console.warn(t)}if(this.roomInfo.type=t.type,t.status===this.micStatus)return;const s="open"===t.status?EVENT.OPEN_MICROPHONE:EVENT.CLOSE_MICROPHONE;"open"!==t.status||this.reciveSliceIdEvent||await this.setSessionId(),this.micStatus=t.status,setTimeout((()=>{this.trigger(s,{...t})}),0)}}