import user from '$config/user';
import {RowWheat} from "@/pages/talking/const";
import {WheatUseStatus} from "@/pages/talking/const/wheat";
import {cache} from "$utils/request/cache";

export default <LibMixins>{

    computed:{
        microUserInfo(){
            if (this.microInfo) {
                let data = [this.microInfo.host_info,...(this.microInfo.mc_list || [])];
                let resultData = [];
                // 循环处理
                data.map((item)=>{
                    if (item && item.mc_user_info && item.mc_user_info.uid) {
                        resultData.push({
                            label: item.mc_user_info.nick_name,
                            icon: item.mc_user_info.head_pic,
                            uid: item.mc_user_info.uid
                        });
                    }
                });
                return resultData;
            } else {
                return [];
            }

        }
    },

    methods:{

        // 初始化麦位信息之后触发
        installMicroInfo(){

            let data = [this.microInfo.host_info,...this.microInfo.mc_list];

            let resultData = {};
            // 循环处理
            data.map((item)=>{
                if (item && item.mc_user_info && item.mc_user_info.uid) {
                    resultData[item.mc_user_info.uid] = item;
                }
            });

            this.micro_info = resultData;

            this.updateMicroCache();
        },

        // 执行对比麦位信息，如果不一致进行update 更新麦位不同点
        contrastWheatInfo(microInfo){

            if (microInfo) {
                let data = [microInfo.host_info,...microInfo.mc_list];

                let checkData = [this.microInfo.host_info,...this.microInfo.mc_list];
                let resultData = {};
                checkData.map((item,index)=>{
                    let contrastItem = data[index];
                    if (item.mc_user_info && !contrastItem.mc_user_info || item.mc_user_info.uid !== contrastItem.mc_user_info.uid) {
                        item.mc_user_info = contrastItem.mc_user_info;
                    }

                    if (item.mc_status !== contrastItem.mc_status) {
                        item.mc_status = contrastItem.mc_status;
                    }

                    if (item && item.mc_user_info && item.mc_user_info.uid) {
                        resultData[item.mc_user_info.uid] = item;
                    }
                });

                this.micro_info = resultData;

            }

        },

        // 更新某个用户的魅力值
        updateUserIntegral(uid,integral){
            let microInfo = this.getMicroUserInfo(uid);
            if (microInfo) {
                microInfo.charm_value = parseFloat(microInfo.charm_value) + parseFloat(integral);
                this.updateMicroCache(false);
            }
        },

        // 获取某一个用户信息
        getMicroUserInfo(uid){
            let info = this.micro_info[uid];
            if (info && info.mc_user_info && info.mc_user_info.uid) {
                return  info.mc_user_info;
            } else {
                return  null;
            }
        },


        // 更新麦位缓存
        updateMicroCache(message:boolean=true){

            clearTimeout(this.cacheMicroTime);
            this.cacheMicroTime = setTimeout(()=>{
                let cacheSign =cache.getSign('get_room_micro_info-'+this.$params.rid);

                let cacheData = cache.getCache(cacheSign);

                // 更新缓存
                if (cacheData && cacheData.data.data) {
                    cacheData.data.data.data = this.microInfo;
                    cache.updateCache(cacheSign,cacheData.data);
                }

                // 执行公布麦位消息
                message && this.sendWheatInfo();

            },10);


        },

        // 查看当前麦位是否空缺
        getMicroEmptyStatus(index:number) {
            let mcList = this.microInfo.mc_list || [];

            return mcList[index] && !(mcList[index].mc_user_info && mcList[index].mc_user_info.uid);
        },

        // 查看用户是否出现在麦位信息中
        hasMicroInfo(){
            if (user.isLogin()) {
                return this.micro_info[user.uid()];
            }
        },

        // 设置麦位的麦位状态
        setMicroMcStatus(index,status:WheatUseStatus=WheatUseStatus.normal){
            if (typeof index === 'number' && this.microInfo.mc_list) {
                if (this.microInfo.mc_list[index] && this.microInfo.mc_list[index].mc_status !== status) {
                    this.microInfo.mc_list[index].mc_status = status;
                    this.updateMicroCache();
                }
            }
        },

        // 获取空缺麦位的位置
        getMicroIndex(uid =undefined){
            let mcList = this.microInfo.mc_list || [];
            for (let i=0,count=mcList.length;i<count;i++) {
                if (uid) {
                    if (mcList[i] && mcList[i].mc_user_info && mcList[i].mc_user_info.uid === uid) {
                        return  i;
                    }
                } else {
                    if (!mcList[i] || !mcList[i].mc_user_info || !mcList[i].mc_user_info.uid) {
                        return i;
                    }
                }

            }

            return null;
        },

        // 进入麦位坐下
        updateMicro(index:number | undefined,user){

            if (index === undefined) index = this.getMicroIndex();

            if (typeof index === 'number' && this.microInfo.mc_list) {

                // 如果存在于其他麦位直接退出
                this.exitMicro(user.uid);

                this.setMicroMcStatus(index,WheatUseStatus.normal);
                this.microInfo.mc_list[index].mc_user_info = user;

                // 更新麦位信息
                this.installMicroInfo();
            }

        },

        // 进入主持麦位
        updateHostMicro(user){
            // 如果满足以下条件触发
            if (this.microInfo && this.microInfo.host_info && !this.microInfo.host_info.uid) {
                // 如果存在于其他麦位直接退出
                this.exitMicro(user.uid);
                this.microInfo.host_info.mc_status = 3;
                this.microInfo.host_info.mc_user_info = user;

                this.setMicroStatus(RowWheat.wheat);
                // 更新麦位信息
                this.installMicroInfo();
            }
        },

        // 设置坐下麦位
        downMicro({
            index,
            host,
            user
        }:Record<string, any> = {}){

            if (user) {
                if (host) {
                    this.updateHostMicro(user);
                } else {
                    this.updateMicro(index,user);
                }
            }

            this.setMicroStatus(RowWheat.wheat);
            // return this.useAgora.joinPublished();
        },

        // 离开麦位
        upMicro({
            uid
        } = {}){
            if (uid) {
                this.exitMicro(uid);
            }
            this.setMicroStatus(RowWheat.none);
            return this.unPublish();
        },

        // 断开语音
        unPublish(){
            return this.useAgora && this.useAgora.unPublish();
        },

        exitMicro(uid){
            if (uid) {
                let item = this.micro_info[uid];
                if (item) {
                    // item.mc_status = WheatUseStatus.normal;
                    item.mc_user_info = {};
                    // 更新麦位信息
                    this.installMicroInfo();
                }
            }

        },

        setMicroStatus(status){
            if (this.wheat_status !== status) {
                this.wheat_status = status;
            }
        }

    },

    created(){
        this.micro_info = {};
    }

}
