import { AgoraRTMessage } from '$utils/socket/agoraRTM/agoraRTMIM';
export default {

    provide(){
        return {
            installSocketItem:(vm)=>{
                this.socketItem = vm;
            },
            sendGift:(data,send:boolean=false)=> this.sendGift(data,send)
        }
    },

    methods:{

        // 发送系统消息
        sendSystem(message){
            if (message && this.socketItem) {

                this.socketItem.triggerPushMessage(AgoraRTMessage.system,{
                    message
                });
            }
        },

        // 发送加入房间消息
        sendJoinRoom(message){
            if (message && this.socketItem) {
                let resultData = this.agoraRTMIM.addUserInfo({
                    message
                },true);
                this.socketItem.triggerPushMessage(AgoraRTMessage.joinHome,resultData);

                this.userJoinRoom().then(()=>{
                    this.agoraRTMIM.send(resultData,AgoraRTMessage.joinHome);
                });
            }
        },

        // 发送礼物消息
        sendGift(data,send:boolean=false){

            if (data) {

                let userInfo = [];
                data.giveUserId.map((item)=>{
                    let info = this.getMicroUserInfo(item);

                    if (!info && this.giveUser && this.giveUser.uid && this.giveUser.uid === item) {
                        info = this.giveUser;
                    }

                    if (info) {
                        userInfo.push({
                            uid:item,
                            nickname:info.nick_name,
                            toNick_color:'#000000'
                        });
                    }

                });

                let resultData = this.agoraRTMIM.addGiftInfo(this.agoraRTMIM.addUserInfo({}), {
                    userInfo:userInfo,
                    number: data.number,
                    gift: data.gift
                });

                if (send) {
                    this.agoraRTMIM.send(resultData,AgoraRTMessage.gift);
                } else {
                    this.socketItem.triggerPushMessage(AgoraRTMessage.gift,resultData);
                }
            }

        },

        // 向大家公布麦位信息
        sendWheatInfo(){
            // 提醒大家麦位信息得到了更新
            this.agoraRTMIM.send({
                wheatData: this.microInfo
            },AgoraRTMessage.wheat);
        },

        // 向大家公布申请排麦状态
        sendWheatQueueInfo(user,status:boolean=true){
            let resultData = this.agoraRTMIM.addQueueWheat({},user,status);

            // 向其他人广播
            this.agoraRTMIM.send(resultData,AgoraRTMessage.userRowWheat);

            // 执行通知自己
            this.agoraRTMIM.triggerMessage(resultData);

        }

    },
    mounted(){
        // 监听加入房间
        this.agoraRTMIM.on(AgoraRTMessage.joinHome,({data})=> this.addAdmission(data,false));

        // 监听收到礼物
        this.agoraRTMIM.on(AgoraRTMessage.gift,({data})=> {

            // 获取礼物信息
            let gift = this.$store.state.gift && this.$store.state.gift.giftObjectData && this.$store.state.gift.giftObjectData[data.giftId];
            if (!gift) {
                gift = {
                    gid: data.giftId,
                    gift_name: data.e_name,
                    gift_type: data.type,
                    play_image: data.show_gift_img,
                    pcplay_image: data.show_gift_pc_img,
                    base_image: data.show_img,
                    gift_price: data.gift_price || 0
                }
            }
            // 获取赠送的用户id
            let uid = (data.userInfo || []).map((item)=> item.uid);

            this.sendGiveUserGift(gift,uid, gift.gift_price * data.giftNum || 0);
        });

        // 监听麦位信息
        this.agoraRTMIM.on(AgoraRTMessage.wheat,({data})=>{

            if (data.wheatData) {
                this.contrastWheatInfo(data.wheatData);
                // 设置当前排麦状态
                if (this.hasMicroInfo()) {
                    this.downMicro();
                } else {
                    // 否则移除订阅
                    this.unPublish();
                }
            } else {
                this.getRoomMicroInfo(true);
            }

        });
    }

}
