import Event from"../../common/chat/eventTypes";import VideoChat from"../../common/videoChat/videoChat";import computedBehavior from"../../common/computed/index";Component({behaviors:[computedBehavior],data:{pushUrl:"",userNumber:0,pullUrl:[],resizeHeight:0,chatEventBindThis:{},muteVideo:!1,muteAudio:!1},properties:{getChatInstance:{type:Function,observer(t){}},initEvent:{type:Object,observer(t){}},camera:Boolean,skin:{type:String,value:"black",observer(t){"black"!==t&&"white"!==t&&this.setData({skin:"black"})}},aspect:{type:String,value:"3:4"}},computed:{widthAndTop(){const{userNumber:t}=this.data;let e="33.33%";t>0&&t<=4&&(e="50%");let i="0";return t<3?i=18.25:t>4&&t<7&&(i=12.5),{width:e,top:i+"%"}}},lifetimes:{ready(){const t=this.data.getChatInstance();if(this.chat=t,!t)return;const e=this.getChatEvent()||{};this.listenEvent(e),this.videoChat=new VideoChat({roomName:this.chat.options.roomId,micUserId:this.chat.micUserId}),this.initPushPlayer()}},methods:{getChatEvent:()=>({[Event.MICROPHONE](t,e){const i=this.videoChat;"close"!==e.status||e.userId&&e.userId/1!==i.uid||this.leave(!e.userId)},[Event.MUTE_USER_MICRO](t,e){const i=this.videoChat;e.mute?i.muteLocal(e.type):i.unmuteLocal(e.type)}}),getVideoEvent:()=>[{type:"stream-init",handler(){this.videoChat.publishStream().then((t=>{const e=t,i=this.data.userNumber+1;this.setData({pushUrl:e,userNumber:i}),setTimeout((()=>{this.computedResizeHeight(i)}),1e3)}))}},{type:"stream-added",handler(t){this.videoChat.subscribe(t)}},{type:"stream-subscribed",handler(t){const e=Object.assign({},t);console.info("stream-subscribed:",e),e.isTeacher=this.chat.teacherData.userId/1===t.uid;const i=this.data.userNumber+1,s=this.data.pullUrl;s.push(e),this.setData({userNumber:i}),setTimeout((()=>{this.setData({pullUrl:s})}),0),setTimeout((()=>{this.computedResizeHeight(i)}),1e3)}},{type:"stream-removed",handler(t){let e=this.data.pullUrl,i=this.data.userNumber;const s=t.uid[0];e.find((t=>t.uid===s))&&(i-=1),this.setData({userNumber:i}),setTimeout((()=>{e=e.filter((t=>t.uid!==s)),this.setData({pullUrl:e})}),0),setTimeout((()=>{this.computedResizeHeight(i)}),1e3)}},{type:"mute-local",handler(t){console.info(">>>muteLocal:",t),"success"===t.status&&("video"===t.type?this.setData({muteVideo:!0}):this.setData({muteAudio:!0}))}},{type:"unmute-local",handler(t){console.info(">>>unmuteLocal:",t),"success"===t.status&&("video"===t.type?this.setData({muteVideo:!1}):this.setData({muteAudio:!1}))}}],computedResizeHeight(t){this.setData({resizeHeight:t%2+1+"px"})},listenEvent(t={}){const e=this.chat;return!!e&&(Object.keys(t).forEach((i=>{this.data.chatEventBindThis[i]=t[i].bind(this),e.on(i,this.data.chatEventBindThis[i])})),!0)},recorderStateChange(t){t.detail.code,-1307===t.detail.code&&(this.chat.cancelJoinChannel(),this.leave())},async initPushPlayer(){const t=await this.videoChat.initClient();try{await this.videoChat.joinChannel(t)}catch(t){return void this.leave()}this.chat.joinChannelSuccess(),this.videoChat.initEvent(),this.getVideoEvent().forEach((t=>{this.videoChat.on(t.type,t.handler.bind(this))})),this.videoChat.addStream()},leave(t=!1){this.videoChat.leave(),this.triggerEvent("leave",t),this.videoChat=null,Object.keys(this.data.chatEventBindThis).forEach((t=>{this.chat.off(t,this.data.chatEventBindThis[t])}))}}});