import unit from '$utils/unit/unit';

import tabData from '../data/tab';

import Throttle from '$utils/tool/throttle';

import personalDetail from '$utils/control/personal-detail';

import socket from '$utils/socket';
import {NoticeType} from "$utils/socket/const";

export default <LibMixins>{

    data(){
        return {
            swiperOption:{
                slidesPerView:'auto',
                spaceBetween:unit.unitPx(12)
            },
            gameOption:{
                slidesPerView:'auto',
                spaceBetween:unit.unitPx(15)
            },
            banner:0,
            game:0,
            tabData,
            followStatus:false,
            followNum:0,
        }
    },

    watch:{
      option(){
          if (this.option.uid) {
              this.install();
          }
      }
    },

    computed:{
        useFollowNum(){
            return this.followNum + (this.followStatus ? 1:0)
        }
    },

    methods:{
        closeModal(){
            personalDetail.openUnique = '';
            return this.close();
        },

        bannerPrev(){
            let banner = this.banner;
            banner--;
            if (banner >= 0) {
                this.banner = banner;
            }
        },

        bannerNext(){
            let banner = this.banner;
            banner++;

            if (banner < this.option.images.length) {
                this.banner = banner;
            }

        },

        gamePrev(){
            let game = this.game;
            game--;
            if (game >= 0) {
                this.game = game;
            }
        },

        gameNext(){
            let game = this.game;
            game++;

            if (game < this.option.gameList.length) {
                this.game = game;
            }
        },

        // 关注
        followUser(){
            // 设置当前的关注状态
            this.followStatus = this.followStatus ? 1 : 0;

            personalDetail.updateCache({
                uid: this.option.uid,
                status: this.followStatus
            });

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

        },

        // 设置关注
        triggerFollow(){
           return this.triggerFollowImplement();
        },

        triggerFollowImplement(){
            if(this.followStatus !== this._followStatus) {
                this._followStatus = this.followStatus;

                return this.$request({
                    url:this.followStatus ? 'user/follow_user' : '/user/unfollow_user',
                    data:{
                        follow_uid: this.option.uid
                    },
                    token:true
                });
            }
        },

        install(){
            this.followStatus = this.option.is_follow === 2;
            this._followStatus = this.followStatus;

            let followNumber = this.followStatus ? this.option.fans_num -1 :this.option.fans_num;
            this.followNum = followNumber < 0 ? 0 : followNumber;
        }

    },


    created() {

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

        this.triggerFollow = this.Throttle.supper;

    },

    beforeUnmount() {
        this.Throttle && this.Throttle.destroy();
        if(this.followStatus !== this.option.is_follow) {
            this.triggerFollowImplement();

            return socket.triggerNotice(NoticeType.follow, {
                uid: this.option.uid,
                status:this.followStatus,
                followUserInfo:this.option
            });
        }

    }

}
