import Event from"../utils/event.js";import User from"../model/user.js";import Stream from"../model/stream.js";import{EVENT}from"../common/constants.js";const TAG_NAME="UserController";class UserController{constructor(s){this.userMap=new Map,this.userList=[],this.streamList=[],this._emitter=new Event,this.componentContext=s,this.isNewVersion=s.isNewVersion}userEventHandler(s){const e=s.detail.code;let t;if(!s.detail.message||"string"!=typeof s.detail.message)return console.warn(TAG_NAME,"userEventHandler 数据格式错误"),!1;try{t=JSON.parse(s.detail.message)}catch(s){return console.warn(TAG_NAME,"userEventHandler 数据格式错误",s),!1}switch(e){case 1020:this.isNewVersion;break;case 1031:this.addUser(t);break;case 1032:this.removeUser(t);break;case 1033:this.updateUserVideo(t);break;case 1034:this.updateUserAudio(t)}}addUser(s){const e=s.userlist,t=this.userMap;Array.isArray(e)&&e.length>0&&e.forEach((s=>{const e=s.userid;let r=this.getUser(e);r||(r=new User({userID:e}),this.userList.push({userID:e})),t.set(e,r),this._emitter.emit(EVENT.REMOTE_USER_JOIN,{userID:e,userList:this.userList})}))}removeUser(s){const e=s.userlist;Array.isArray(e)&&e.length>0&&e.forEach((s=>{const e=s.userid;let t=this.getUser(e);t&&t.streams&&(this._removeUserAndStream(e),t.streams.main&&t.streams.main.reset(),t.streams.aux&&t.streams.aux.reset(),this._emitter.emit(EVENT.REMOTE_USER_LEAVE,{userID:e,userList:this.userList,streamList:this.streamList}),t=void 0,this.userMap.delete(e))}))}updateUserVideo(s){const e=s.userlist;Array.isArray(e)&&e.length>0&&e.forEach((s=>{const e=s.userid,t=s.streamtype,r=e+"_"+t,i=s.hasvideo,a=s.playurl,m=this.getUser(e);if(m){let s=m.streams[t];s?(s.setProperty({hasVideo:i}),i||s.hasAudio||this._removeStream(s)):(m.streams[t]=s=new Stream({userID:e,streamID:r,hasVideo:i,src:a,streamType:t}),this._addStream(s)),"aux"===t&&(i?(s.objectFit="contain",this._addStream(s)):this._removeStream(s)),this.userList.find((s=>{if(s.userID===e)return s[`has${t.replace(/^\S/,(s=>s.toUpperCase()))}Video`]=i,!0}));const o=i?EVENT.REMOTE_VIDEO_ADD:EVENT.REMOTE_VIDEO_REMOVE;this._emitter.emit(o,{stream:s,streamList:this.streamList,userList:this.userList})}}))}updateUserAudio(s){const e=s.userlist;Array.isArray(e)&&e.length>0&&e.forEach((s=>{const e=s.userid,t="main",r=e+"_"+t,i=s.hasaudio,a=s.playurl,m=this.getUser(e);if(m){let s=m.streams.main;s?(s.setProperty({hasAudio:i}),i||s.hasVideo||this._removeStream(s)):(m.streams.main=s=new Stream({userID:e,streamID:r,hasAudio:i,src:a,streamType:t}),this._addStream(s)),this.userList.find((s=>{if(s.userID===e)return s[`has${t.replace(/^\S/,(s=>s.toUpperCase()))}Audio`]=i,!0}));const o=i?EVENT.REMOTE_AUDIO_ADD:EVENT.REMOTE_AUDIO_REMOVE;this._emitter.emit(o,{stream:s,streamList:this.streamList,userList:this.userList})}}))}getUser(s){return this.userMap.get(s)}getStream({userID:s,streamType:e}){const t=this.userMap.get(s);if(t)return t.streams[e]}getUserList(){return this.userList}getStreamList(){return this.streamList}reset(){return this.streamList.forEach((s=>{s.reset()})),this.streamList=[],this.userList=[],this.userMap.clear(),{userList:this.userList,streamList:this.streamList}}on(s,e,t){this._emitter.on(s,e,t)}off(s,e){this._emitter.off(s,e)}_removeUserAndStream(s){this.streamList=this.streamList.filter((e=>e.userID!==s&&""!==e.userID)),this.userList=this.userList.filter((e=>e.userID!==s))}_addStream(s){this.streamList.includes(s)||this.streamList.push(s)}_removeStream(s){this.streamList=this.streamList.filter((e=>e.userID!==s.userID||e.streamType!==s.streamType));this.getUser(s.userID).streams[s.streamType]=void 0}}export default UserController;