import Throttle from '$utils/tool/throttle';
import user from "$config/user";

import socket from '$utils/socket';
import {NoticeType} from "$utils/socket/const";
import popup, {PopupExportComponent} from "$utils/tool/popup";
import {cache} from "$utils/request/cache";

export default <LibMixins>{

    data(){
        return {
            collect:0
        }
    },

    watch:{
      'info.is_collect':function () {
          return this.setCollect(this.info.is_collect === 2 ? 1 : 0,false);
      }
    },

    methods:{

        // 举报
        report(){
            popup.$open(PopupExportComponent.report,{
                uid: this.info.room_host_uid
            });
        },

        // 触发更改关注
        triggerFollow(){
            return this.setCollect(this.collect?0:1,true);
        },

        // 设置是否关注
        setCollect(collect,update:boolean=true){
            if (user.verificationLogin()) {
                if (this.info.room_background_id !== undefined) {
                    if(this.collect !== collect){
                        this.collect = collect;
                        if (this._collect === undefined) {
                            this._collect = collect;
                        }

                        return this.triggerCollect();
                    }
                }
            }
        },

        updateRoomInfo(collect){

            let cacheSign = cache.getSign('enter_room_info-'+this.info.rid);

            let useCacheData = cache.getCache(cacheSign);

            if (useCacheData && useCacheData.data) {
                useCacheData.data.data.data.is_collect = collect ? 2 : 1;

                cache.updateCache(cacheSign,useCacheData.data);
            }

        },

        // 请求接口设置
        triggerCollect(){
            if (this._collect !== this.collect) {
                this._collect = this.collect;
                this.updateRoomInfo(this._collect);
                this.$request({
                    url: this._collect ? 'Usercollectroom/collect_room':'Usercollectroom/un_collect_room',
                    data:{
                        rid: this.info.rid
                    },
                    token:true
                });
                socket.triggerNotice(NoticeType.followRoom, {
                    rid: this.info.rid,
                    status: this._collect,
                    data: this.info
                });

            }
        }

    },

    created(){
        this.triggerCollectThrottle = new Throttle({
           call:this,
            first:false,
            handle:this.triggerCollect,
            delay:500
        });
        this.triggerCollect = this.triggerCollectThrottle.supper;
    },

    beforeUnmount() {
        this.triggerCollectThrottle && this.triggerCollectThrottle.destroy();
    }


}
