import {InstructionsCacheType, InstructionsMessageType} from "$utils/request";
import user from "$config/user";
import Shield from '$utils/control/user/const/shield';
import {RowWheat} from "@/pages/talking/const";
import {AgoraRTMIMStatus} from "$utils/socket/agoraRTM/const/status";


export default <LibMixins>{


    data(){
        return {
            Shield,
            // 房间信息
            roomInfo:{},
            // 麦位信息
            microInfo:{}
        }
    },

    computed:{
      // 用户是否为房主
      isHomeowners(){
          let uid = user.uid();
          return this.roomInfo.room_owner_uid === uid;
      },
      // 用户目前是否为 管理员
      isAdmin(){
          let uid = user.uid();
          return (this.roomInfo.room_admin_uid || '').indexOf(uid) >= 0 && uid || this.isHomeowners;
      },
      // 主持麦位是否有人
      hostMicroIsHave(){
          return !!(this.microInfo.host_info && this.microInfo.host_info.mc_user_info && this.microInfo.host_info.mc_user_info.uid);
      },
      // 当前人是否为主持
      isHostMicro(){
          return !!(this.microInfo.host_info && this.microInfo.host_info.mc_user_info && this.microInfo.host_info.mc_user_info.uid === user.uid());
      },
      // 自己是否在麦上
      inWheat(){
          return this.wheat_status === RowWheat.wheat;
      }
    },

    methods:{

        // 获取房间信息
        getRoomInfo(){
            Promise.all([this.fetchRoomInfo(false),this.getUserAdmission()]).then(([roomInfo,data])=>{
                this.setUserAddAdmission(data);
            });
        },

        // 房间信息
        fetchRoomInfo(updated:boolean=true){

            return  new Promise((resolve, reject)=>{
                if (this.$params.infoData) {
                    this.setRoomInfo(JSON.parse(this.$params.infoData),resolve,reject);
                    return resolve(this.$params.infoData);
                } else {
                    return this.$request({
                        url:'room/enter_room_info',
                        data:{
                            rid: this.$params.rid,
                            password: this.$params.password
                        },
                        token:true,
                        cache:{
                            type: InstructionsCacheType.storage,
                            updated,
                            id:'enter_room_info-'+this.$params.rid
                        },
                        failMessage:true,
                        message:InstructionsMessageType.other
                    }).then((data)=>{

                        if (data.isCache) {
                            this.fetchRoomInfo(true);
                        }

                        if (data.isSuccess) {
                            this.setRoomInfo(data.data,resolve,reject);
                        } else {
                            return this.$router.back();
                        }

                    }).catch(reject);
                }
            });

        },

        setRoomInfo(data,resolve,reject){
            this.roomInfo = data;

            if (!this.setRoomInfoStatus && this.agoraRTMIM.status !== AgoraRTMIMStatus.join) {
                this.setRoomInfoStatus = true;
                // 安装 socket
                this.agoraRTMIM.install(this.$params.rid.toString()).then(()=>{
                    this.sendSystem(data.room_welcome);
                    resolve();
                }).catch(reject);
            }

        },

        // 获取用户排麦状态
        getUserRowWheat(updated:boolean=true){
            return this.$request({
                url:'room/get_user_mc_queue_status',
                data:{
                    rid: this.$params.rid
                },
                cache:{
                    type: InstructionsCacheType.storage,
                    updated
                },
                token:true
            }).then((data)=>{
                if (data.isSuccess && data.data.in_mc_queue === 1) {
                    this.setMicroStatus(RowWheat.queuing);
                }
                if (data.isCache) {
                    this.getUserRowWheat();
                }
            });
        },

        // 获取用户进入房间特权信息
        getUserAdmission(){
            return this.$request({
                url:'room/get_room_user_vip',
                data:{
                    rid: this.$params.rid
                },
                cache:{
                    type: InstructionsCacheType.storage,
                    id:'get_room_user_vip',
                    first:true,
                    updated:true
                },
                token:true
            });
        },

        // 设置用户进入房间特权
        setUserAddAdmission(data){
            this.user = data.data;
            return this.addAdmission(data.data);
        },

        // 执行 调用加入房间接口
        userJoinRoom(){
            return new Promise((resolve)=>{
                return this.$request({
                    url:'room/user_enter_room',
                    data:{
                        rid: this.$params.rid,
                        uid: user.uid()
                    },
                    token:true
                }).then(resolve).catch(resolve);
            })
        },

        // 获取麦位详情
        getRoomMicroInfo(updated:boolean=true){

            if (this.roomMicroStatus) return ;
            return this.$request({
                url:'room/get_room_micro_info',
                data:{
                    rid: this.$params.rid
                },
                token:true,
                next:({status})=> this.roomMicroStatus = status,
                failMessage:true,
                cache:{
                    type: InstructionsCacheType.storage,
                    updated,
                    id:'get_room_micro_info-'+this.$params.rid
                },
                message:InstructionsMessageType.other
            }).then((data)=>{
                this.microInfo = data.data;
                this.installMicroInfo();
                if (data.isCache) {
                    this.getRoomMicroInfo(true);
                }
                // 设置当前排麦状态
                if (this.hasMicroInfo()) {
                    this.downMicro();
                } else {
                    // 否则移除订阅
                    this.unPublish();
                }

            })
        },

        // 安装
        install(){
            // 请求房间信息
            this.getRoomInfo();
            // 请求麦位信息
            this.getRoomMicroInfo(false);
            // 获取用户排麦信息
            this.getUserRowWheat(false);
            // // 获取用户入场特效
            // this.getUserAdmission();
        },
    },

    mounted(){
        this.install();
    },

    created(){
        // new WebIM().install(this.$store.state.user.user);
        this.roomInfo = this.$params || {};


    }


}
