import popup from "$utils/tool/popup";

import userIntegral from '$utils/control/integral';

import Throttle from '$utils/tool/throttle';
import {InstructionsMessageType} from "$utils/request";
import {nextTick} from "vue";
import {FlatListReload} from "$components/flat-list/const";

export default {

    data(){
      return {
          // 当前是否处于点击状态
          pressStatus: false,
          // 当前点击次数
          pressNumber:0

      }
    },

    inject:['getRoomInfo','sendGiveUserGift','sendGift'],

    methods:{

        giveUserGift(){

            // 获取礼物
            let gift = this.giftData[this.giftSelect];

            if (!gift) return popup.$toast('请选择赠送礼物');

            if (!this.giveUserValue) {
                return popup.$confirm({
                    title:'暂未选择赠送人,是否赠送全部麦序',
                    confirm:()=>{
                        this.giveUserValue = 'all';
                    }
                });
            }


            // 查看赠送用户
            let giveUserId = this.giveUserValue;
            // 如果为全部麦序触发
            if (giveUserId === 'all') {
                giveUserId = [];
                this.userData.map((item)=>{
                    giveUserId.push(item.uid);
                });

                if (giveUserId.length <=0) return  popup.$toast('未有人上麦');
            }

            if (!(giveUserId instanceof Array)) {
                giveUserId = [giveUserId];
            }

            // 计算本次需要使用的积分
            let integral = giveUserId.length * (parseFloat(gift.gift_price) || 0) * this.number;

            let resultConfig = {
                giveUserId,
                integral,
                number:this.number,
                gift,
                item:gift
            };

            // 查看用户是否可以赠送
            if (this.useGiftType.methods.check(resultConfig)) {
                gift = JSON.parse(JSON.stringify(gift));
                if (this.$refs.banner && this.$refs.banner.swiper) {
                    gift.translate = this.$refs.banner.swiper.translate;
                }

                resultConfig.gift = gift;

                // 减余额
                this.useGiftType.methods.reduce(resultConfig,this);

                // 设置当前的标识符
                let unique = new Date().getTime() + '-'+ Math.ceil(Math.random() * 10000);

                this.requestUnique = unique;

                /* 设置持续点击的动画 */
                if (!this.pressStatus) {
                    this.pressStatus = true;
                }

                this.pressNumber+=1;

                this.lastTrigger = {
                    resultConfig,
                    unique
                };
                return this.giveChangeNumber(resultConfig,unique);
            }

        },

        giveChangeNumber(resultConfig,unique){
            if (this.pressNumber > 0 && this.pressStatus && this.requestUnique === unique) {
                this.giveTriggerChangeNumber(resultConfig,unique);
            }
        },

        giveTriggerChangeNumber(resultConfig,unique){
            this.sendGiveGift(resultConfig,unique);
            this.pressStatus = false;
            this.pressNumber = 0;
        },

        // 向服务器同步赠送
        sendGiveGift(resultConfig,unique){
            if (this.requestUnique === unique) {
                resultConfig.number = this.number * this.pressNumber;
                resultConfig.integral = resultConfig.giveUserId.length * (parseFloat(resultConfig.gift.gift_price) || 0) * resultConfig.number;

                // 赠送礼物效果展示
                this.sendGiveUserGift(resultConfig.gift,resultConfig.giveUserId,resultConfig.number * (parseFloat(resultConfig.gift.gift_price) || 0));
                this.sendGift(resultConfig);

                this.$request({
                    url:'gift/send_gift',
                    data:{
                        to_uid_list: resultConfig.giveUserId.join(','),
                        num: resultConfig.number,
                        pack:this.giftType,
                        rid: this.roomInfo.rid,
                        gid: resultConfig.gift.gid
                    },
                    token:true,
                    message:InstructionsMessageType.other,
                    failMessage:true
                }).then((data)=>{
                    if (data.isSuccess) {
                        this.sendGift(resultConfig,true);
                    } else {
                        this.useGiftType.methods.plus(resultConfig,this);
                    }

                    this.useGiftType.methods.checkUpdate && this.useGiftType.methods.checkUpdate(resultConfig,this,unique);

                }).catch(()=>{
                    this.useGiftType.methods.plus(resultConfig,this);
                    this.useGiftType.methods.checkUpdate && this.useGiftType.methods.checkUpdate(resultConfig,this,unique);
                });
            }
        },

        updateBanner(){
          nextTick(()=>{
             this.$refs.banner && this.$refs.banner.update();

             if (!this.giftData[this.giftSelect] || this.giftData[this.giftSelect].num <= 0) {
                 for (let i=0,count=this.giftData.length;i<count;i++) {
                     if (this.giftData[i].num > 0) {
                         this.giftSelect = i;
                         break;
                     }
                 }

                 if (this.giftData[this.giftSelect] === undefined || this.giftData[this.giftSelect].num<=0) {
                     this.giftSelect = 0;
                     return this.$refs.fetch && this.$refs.fetch.reload(FlatListReload.start);
                 }
             }

          });
        },

        checkUpdateUserIntegral(unique){
            if (this.requestUnique === unique) {
                this.requestUnique = null;
                // 向服务端同步
                return userIntegral.updateUserMoney();
            }
        },

    },

    created(){
        this.roomInfo = this.getRoomInfo();
        this.giveChangeNumberThrottle = new Throttle({
            call:this,
            handle:this.giveChangeNumber,
            first:false,
            delay:1000
        });
        this.giveChangeNumber = this.giveChangeNumberThrottle.supper;
    },

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

}
