import money from "$utils/tool/money";
import user from '$config/user';
import popup from "$utils/tool/popup";
import store from '@/store/index';
import Throttle from '$utils/tool/throttle';
let result = {

    // 获取用户余额
    getUserBalance():number{
        if (user.user && user.user.integral) {
            return money.getNumberPrice(money.checkIntMoney(user.user.integral || 0) || 0);
        } else {
            return 0;
        }
    },

    // 查看余额是否够赠送
    checkBalance(integral:number | string,toast:boolean=false){
        // 用户 余额
        let userBalance = this.getUserBalance();
        // 当前商品余额
        integral = money.getNumberPrice(integral);
        // 获取结果
        let result:boolean = userBalance >= integral;
        // 如果 余额不够提示
        toast && !result && this.balanceNotEnough();
        // 返回结果
        return result;
    },

    // 购物余额不够
    balanceNotEnough(){
        return popup.$toast('余额不足');
    },

    // 赠送成功减余额
    reduceBalance(integral:number | string,update:boolean=true){
        // 需要减掉的余额
        integral = money.getNumberPrice(integral);
        // 获取用户余额
        let userBalance = this.getUserBalance();
        // 得到剩余余额
        let surplusIntegral = userBalance - integral;
        // 兼容剩余余额不可小于 0 (虽然不大可能，防止 万一被恶意篡改，虽说前端恶意篡改了金额也没用)
        surplusIntegral = surplusIntegral < 0 ? 0 : surplusIntegral;
        // 设置用户余额
        this.setUserInfo({
            integral: surplusIntegral
        });
        // 向服务器同步
        return update && this.updateUserMoney();
    },

    // 加余额
    plusBalance(integral:number | string,update:boolean=true){
        // 需要减掉的余额
        integral = money.getNumberPrice(integral);
        // 获取用户余额
        let userBalance = this.getUserBalance();
        // 得到剩余余额
        let surplusIntegral = userBalance + integral;
        // 兼容剩余余额不可小于 0 (虽然不大可能，防止 万一被恶意篡改，虽说前端恶意篡改了金额也没用)
        surplusIntegral = surplusIntegral < 0 ? 0 : surplusIntegral;
        // 设置用户余额
        this.setUserInfo({
            integral: surplusIntegral
        });
        // 向服务器同步
        return update && this.updateUserMoney();
    },

    // 设置用户信息
    setUserInfo(value:Record<string, any>){

        let resultData:Record<string, any> = value;

        if (user.user) {
            let useUserData = JSON.parse(JSON.stringify(user.user));
            resultData = {
                ...useUserData,
                ...resultData
            }
        }

        return store.commit('setUserInfo',resultData);
    },


    // 当前是否处于更新用户金额方面
    updateUserMoneyStatus:false,

    // 向服务器同步用户剩余余额之类的东西
    updateUserMoney(){
        if (this.updateUserMoneyStatus) return;
        this.updateUserMoneyStatus = true;
        store.dispatch('updateUserMoneyPromise').then(()=>{
            this.updateUserMoneyStatus = false;
        }).catch(()=>{
            this.updateUserMoneyStatus = false;
        });
    },

    throttle:Throttle


}

result.throttle = new Throttle({
    first:false,
    call:result,
    handle: result.updateUserMoney,
    delay:500
});


result.updateUserMoney = result.throttle.supper;

export default result;