import {InstructionsCacheType} from "$utils/request";
import socket from "$utils/socket";
import {NoticeType} from "$utils/socket/const";
import Throttle from '$utils/tool/throttle';
import personalDetail from "$utils/control/personal-detail";


export default {

    data(){
        return {
            // 是否关注
            follow:false,
            // 是否处于加载中
            loading:true
        }
    },

    watch:{

        hostUser(){
            this.getUserInfo(this.hostUser);
        }

    },

    inject:['getRoomInfo'],

    methods:{

        getUserInfo(){

            if (this.user.uid !== this.hostUser.mc_user_info.uid && this.now_use_host_id !== this.hostUser.mc_user_info.uid) {
                this.now_use_host_id = this.hostUser.mc_user_info.uid;
                if (this.now_use_host_id) {
                    this.loading = true;
                    this.$request({
                        url:'room/get_room_user_info',
                        data:{
                            rid: this.room_info.rid,
                            uid: this.now_use_host_id
                        },
                        token:true,
                        cache:{
                            type: InstructionsCacheType.memory
                        }
                    }).then((data)=>{
                        data = data.data || {};
                        this.loading = false;
                        this.follow = data.is_follow === 2;
                    })
                }
            }

        },
        toggleFollow(){
            this.follow = !this.follow;
            personalDetail.updateCache({
                uid: this.now_use_host_id,
                status: this.follow ? 1 : 0
            },['get_room_user_info-'+this.now_use_host_id+'-'+this.room_info.rid]);

            socket.triggerNotice(NoticeType.follow, {
                uid: this.now_use_host_id,
                status: this.follow ? 1 : 0,
                followUserInfo:this.userInfo
            });

            // 设置是否关注
            return this.triggerFollow();
        },

        triggerFollow(){
            if(this.follow !== this._followStatus) {
                this._followStatus = this.follow;
                return this.$request({
                    url:this._followStatus ? 'user/follow_user' : '/user/unfollow_user',
                    data:{
                        follow_uid: this.now_use_host_id
                    },
                    token:true
                });
            }
        }

    },

    created() {
        this.room_info = this.getRoomInfo();
        this.Throttle = new Throttle({
            delay:1000,
            first:false,
            call:this,
            handle: this.triggerFollow
        });

        socket.on('talking-wheat',(params)=>{

            if (params.uid) {
                let status = !!params.status;
                if (this.follow !== status) {
                    this.follow = status;
                }
            }

        },NoticeType.follow);

        this.triggerFollow = this.Throttle.supper;
    },

    beforeUnmount() {
        this.Throttle && this.Throttle.destroy();
        socket.off('talking-wheat');
    }

}
