import popup from "$utils/tool/popup";
import {PopupExportComponent} from "$popup/popup-export/const";

import user from '$config/user';

import {RowWheat} from '../const';
import {InstructionsCacheType, InstructionsMessageType} from "$utils/request";
import {WheatUseStatus} from "@/pages/talking/const/wheat";

export default <LibMixins>{

    data(){
        return {
            // 当前进行排麦的状态
            wheat_status: RowWheat.none
        }
    },

    provide(){
        return {
            // 打开用户信息
            openUserInfo:(event,userInfo)=> this.openUserInfo(event,userInfo),
            // 申请上麦
            applyForWheat:(index:number|undefined,host:boolean=false)=> this.applyForWheat(index,host),
            // 下麦
            exitForWheat:()=> this.exitForWheat(),
            // 取消排麦
            cancelForWheat:()=> this.cancelForWheat(),
            // 获取房间信息
            getRoomInfo: ()=> this.roomInfo,
            // 获取通话对象
            getUseAgora:()=> this.useAgora || {},
            // 获取聊天对象
            getAgoraRTMIM:()=> this.agoraRTMIM || {},
            // 锁定麦位
            lockForWheat:(index)=>this.lockForWheat(index),
            // 解锁麦位
            unLockForWheat:(index)=>this.unLockForWheat(index),
            // 抱人上麦
            allowForWheat:(user,index=undefined)=> this.allowForWheat(user,index)
        }
    },

    methods:{

        // 打开用户信息
        openUserInfo(event,userInfo){
            if (userInfo && userInfo.uid) {
                if (this.openUserStatus) return;

                popup.$popup.open(PopupExportComponent.user,{
                    el:event,
                    value:true,
                    params:{
                        rid: this.$params.rid,
                        uid: userInfo.uid
                    },
                    item:userInfo,
                    giveGift:(user)=>{
                        popup.$popup.open(PopupExportComponent.user);
                        if (user.uid) {
                            this.triggerChangeGiveUserId(user.uid,user);
                        }
                    }
                });

                // this.$request({
                //     url:'room/get_room_user_info',
                //     data:{
                //         rid: this.$params.rid,
                //         uid: userInfo.uid
                //     },
                //     token:true,
                //     cache:{
                //         type: InstructionsCacheType.memory
                //     },
                //     next:({status})=> this.openUserStatus = status,
                //     loading:'加载中'
                // }).then((data)=>{
                //     popup.$popup.open(PopupExportComponent.user,{
                //         el:event,
                //         value:true,
                //         item:data.data,
                //         giveGift:(user)=>{
                //             popup.$popup.open(PopupExportComponent.user);
                //             if (user.uid) {
                //                 this.triggerChangeGiveUserId(user.uid,user);
                //             }
                //         }
                //     });
                // })

            }

        },

        // 允许某人上麦
        allowForWheat(user,index=undefined){

            return  new Promise<void>((resolve, reject)=>{

                if (this.allowForWheatStatus) return  reject();

                index = typeof index === 'number' ? index : this.getMicroIndex();

                if (index !== null ) {
                    return  popup.$confirm({
                        title:'是否操作'+user.nick_name+'上麦?',
                        successAsyncText:'操作成功',
                        confirm:()=>{
                            return new Promise( (c_resolve, c_reject) =>{

                                this.getUserWheatInfo(user.uid).then((wheatInfo)=>{

                                    this.$request({
                                        url:'room/room_owner_up_micro',
                                        data:{
                                            micro_id:index,
                                            uid:user.uid,
                                            rid: this.$params.rid
                                        },
                                        token:true,
                                        failMessage:true,
                                        next:({status})=> this.allowForWheatStatus = status,
                                        message: InstructionsMessageType.other
                                    }).then((data)=>{
                                        if (data.isSuccess) {
                                            this.updateMicro(index,wheatInfo);
                                            this.sendWheatQueueInfo(wheatInfo,false);
                                            resolve(data);
                                            c_resolve(true);
                                        } else {
                                            reject();
                                            return c_resolve(false);
                                        }

                                    }).catch(()=>{
                                        c_reject();
                                        reject();
                                    });
                                }).catch(c_reject);


                            })
                        },
                        cancel:function () {
                            return reject();
                        }
                    });

                } else {
                    popup.$toast('暂无可用麦位');
                    return  reject();
                }
            });


        },

        // 申请上麦
        applyForWheat(index:number|undefined,host:boolean=false){

            if (this.wheat_status === RowWheat.queuing) {
                return  popup.$toast('正在排麦中');
            } else if (!this.applyForWheatStatus) {
                // 如果为管理员触发
                if (this.isAdmin) {
                    return popup.$confirm({
                        title: host ? '是否上主持位?' : this.wheat_status === RowWheat.none ? '是否上麦?' :'是否切换麦位?',
                        successAsyncText:this.wheat_status === RowWheat.none ? '上麦成功' : '切换成功',
                        confirm:()=>{

                            index = typeof index === 'number' ? index : this.getMicroIndex();

                            if (index != null) {
                                return new Promise<boolean>( (resolve, reject)=> {
                                    this.getUserWheatInfo(user.uid()).then((wheatInfo)=>{
                                        return this.$request({
                                            url:host ? 'room/host_up_micro' : 'room/user_up_micro',
                                            data:{
                                                rid: this.$params.rid,
                                                micro_id:typeof index === 'number'?index:''
                                            },
                                            token:true,
                                            failMessage:true,
                                            next:({status})=> this.applyForWheatStatus = status,
                                            message: InstructionsMessageType.other
                                        }).then((data)=>{
                                            if (data.isSuccess) {
                                                resolve(true);
                                                if (host) {
                                                    this.updateHostMicro(wheatInfo);
                                                } else {
                                                    this.downMicro({
                                                        index,
                                                        user:wheatInfo
                                                    });
                                                }
                                            } else {
                                                resolve(false);
                                            }
                                        }).catch(reject);
                                    }).catch(reject);

                                });
                            } else {
                                return  popup.$toast('暂无可用麦位');
                            }

                        }
                    });
                } else {
                    if (this.hostMicroIsHave) {
                        return popup.$confirm({
                            title: '是否申请上麦?',
                            successAsyncText:'申请成功',
                            confirm:()=>{
                                return new Promise<boolean>( (resolve, reject)=> {
                                    return this.$request({
                                        url:'room/enter_room_mc_queue',
                                        data:{
                                            rid: this.$params.rid
                                        },
                                        token:true,
                                        failMessage:true,
                                        next:({status})=> this.applyForWheatStatus = status,
                                        message: InstructionsMessageType.other
                                    }).then((data)=>{
                                        if (data.isSuccess) {
                                            // 设置状态为排麦中
                                            this.setMicroStatus(RowWheat.queuing);
                                            // 向其他人广播 目前个人处于排麦中
                                            this.sendWheatQueueInfo(user.user,true);
                                            resolve(true);
                                        } else {
                                            resolve(false);
                                        }
                                    }).catch(reject);
                                });
                            }
                        });
                    } else {
                        return popup.$toast('主持麦位暂无用户');
                    }
                }

            }
        },

        // 获取用户上麦信息
        getUserWheatInfo(uid){
            return new Promise((resolve, reject)=>{
                return this.$request({
                    url:'hxuser/get_base_user_info',
                    data:{
                        uid
                    },
                    token:true,
                    failMessage:true,
                    message: InstructionsMessageType.other
                }).then((data)=>{
                    if (data.isSuccess) {
                        data = data.data;
                        resolve({
                            avatar_base_image: data.avatar_base_image,
                            avatar_play_image: data.avatar_play_image,
                            charm_value: data.charm_level_info.now_value,
                            uid: data.uid,
                            nick_name:data.nick_name,
                            sex: data.sex,
                            head_pic: data.head_pic
                        });
                    } else {
                        reject();
                    }

                }).catch(reject);
            });

        },

        // 下麦
        exitForWheat(){
            if (this.hasMicroInfo(user.uid())) {
                if (this.wheat_status === RowWheat.wheat && !this.exitForWheatStatus) {
                    return popup.$confirm({
                        title:'是否执行下麦操作?',
                        successAsyncText:'下麦成功',
                        confirm:()=>{
                            return new Promise<boolean>((resolve, reject)=>{
                                this.$request({
                                    url: this.isHostMicro ? 'room/host_down_micro' :'room/user_down_micro',
                                    data:{
                                        rid: this.$params.rid
                                    },
                                    token:true,
                                    failMessage:true,
                                    next:({status})=> this.exitForWheatStatus = status,
                                    message: InstructionsMessageType.other
                                }).then((data)=>{
                                    this.upMicro({
                                        uid:user.uid()
                                    });
                                    resolve(true);
                                }).catch(reject);
                            })
                        }
                    })
                }
            }
        },

        // 取消排麦
        cancelForWheat(){
            if (this.wheat_status === RowWheat.queuing && !this.cancelForWheatStatus) {
                popup.$confirm({
                    title: '是否取消排麦?',
                    successAsyncText: '取消成功',
                    confirm:()=>{
                        return new Promise((resolve, reject)=>{
                            return this.$request({
                                url:'room/quit_room_mc_queue',
                                data:{
                                    rid: this.$params.rid
                                },
                                token:true,
                                failMessage:true,
                                next:({status})=> this.cancelForWheatStatus = status,
                                message: InstructionsMessageType.other
                            }).then((data)=>{
                                if (data.isSuccess) {
                                    this.setMicroStatus(RowWheat.none);
                                    this.sendWheatQueueInfo(user.user,false);
                                    resolve(true);
                                } else {
                                    resolve(false);
                                }
                            }).catch(reject);
                        });
                    }
                });
            }
        },

        // 是否锁麦
        lockForWheat(index:number=0){

            if (this.lockForWheatStatus) return;

            // 麦位上不允许有人
            if (index !== undefined && this.getMicroEmptyStatus(index)) {
                popup.$confirm({
                    title: '是否锁麦?',
                    successAsyncText: '锁麦成功',
                    confirm:()=>{
                        return new Promise((resolve, reject)=>{
                            return this.$request({
                                url:'room/lock_room_micro',
                                data:{
                                    rid: this.$params.rid,
                                    micro_id:index
                                },
                                token:true,
                                failMessage:true,
                                next:({status})=> this.lockForWheatStatus = status,
                                message: InstructionsMessageType.other
                            }).then((data)=>{
                                if (data.isSuccess) {
                                    this.setMicroMcStatus(index,WheatUseStatus.lock);
                                    resolve(true);
                                } else {
                                    resolve(false);
                                }
                            }).catch(reject);
                        });
                    }
                });
            }


        },

        // 解锁
        unLockForWheat(index:number=0) {
            if (this.unlockForWheatStatus) return;
            // 同样麦位不允许有人
            if (index !== undefined && this.getMicroEmptyStatus(index)) {
                popup.$confirm({
                    title: '是否开放麦位?',
                    successAsyncText: '解锁成功',
                    confirm:()=>{
                        return new Promise((resolve, reject)=>{
                            return this.$request({
                                url:'room/unlock_room_micro',
                                data:{
                                    rid: this.$params.rid,
                                    micro_id:index
                                },
                                token:true,
                                failMessage:true,
                                next:({status})=> this.unlockForWheatStatus = status,
                                message: InstructionsMessageType.other
                            }).then((data)=>{
                                if (data.isSuccess) {
                                    this.setMicroMcStatus(index,WheatUseStatus.normal);
                                    resolve(true);
                                } else {
                                    resolve(false);
                                }
                            }).catch(reject);
                        });
                    }
                });
            }
        },



    }



}
