import popup from "$utils/tool/popup";
import {PopupExportComponent} from "$popup/popup-export/const";
import {InstructionsMessageType} from "$utils/request";

export default {

    methods:{
        // 触发 微信的回调
        triggerPayWeChat(data){
            this.triggerPayWeChatPopupUnique = popup.$popup && popup.$popup.open(PopupExportComponent.wxPay,{
                src:data.result,
                onclose:()=>{
                    this.destroyWeChatPayPool();
                    if (!this.nowPayStatus) {
                        return this.openPayWeChatConfirm();
                    }
                }
            });

            return this.setWeChatWaitPattern();
        },

        // 设置等待模式
        setWeChatWaitPattern(){

            this.payWeChatWaitTime = Math.floor(new Date().getTime() / 1000);

            // 设置终止时间
            this.payWeChatTypeTime = setInterval(()=>{

                let diff =  Math.floor(new Date().getTime()/1000) - this.payWeChatWaitTime;

                // 如果满足任意一种主动释放 本次循环
                if (this.nowPayStatus || diff >= 300 || this.triggerPayWeChatPopupUnique === undefined) {
                    return this.destroyWeChatPayPool();
                } else {
                    this.queryWeChatPayStatus();
                }

            },5000);

        },

        // 设置查询支付状态
        queryWeChatPayStatus(){
            if (this.nowPayStatus) return;
            this.queryWeChatPayStatusRequest().then((data)=>{
                if (data.data == 2) {
                    return this.payWeChatSuccess();
                }
            });
        },

        // 确认框查询
        queryWeChatPayConfirmStatus(){
            if (this.nowPayStatus) return;
            popup.closeConfirm();
            this.queryWeChatPayStatusRequest({
                loading:'查询中',
                message:InstructionsMessageType.other,
                failMessage:true
            }).then((data)=>{
                if (data.isSuccess) {
                    if (data.data == 2) {
                        return this.payWeChatSuccess();
                    } else {
                        popup.$toast('支付未成功');
                    }
                }
                return this.openPayWeChatConfirm();

            }).catch(()=> this.openPayWeChatConfirm());
        },

        openPayWeChatConfirm() {
            return  this.openPayInfoConfirm(()=> this.queryWeChatPayConfirmStatus(),()=>{
                if (!this.nowPayStatus && this.storagePayReuslt) {
                    this.triggerPayWeChat(this.storagePayReuslt);
                }
            });
        },

        // 校验
        queryWeChatPayStatusRequest(options?:Record<string, any>){

            let requestData:LibRequestOptions = {
                url:'hxpay/user_recharge_info',
                data:{
                    oid: this.storagePayReuslt.oid
                },
                token:true
            };

            if (options) {
                requestData = {
                    ...requestData,
                    ...options,
                }
            }

            return this.$request(requestData);
        },

        // 设置支付成功
        payWeChatSuccess(){
            this.destroyWeChatPayPool();
            this.closePayWeChatPopup();
            this.nowPayStatus = true;
            popup.closeConfirm();
            popup.$toast('支付成功');
            this.$store.dispatch('updateUserMoneyPromise');

            return this.paySuccessCallback && this.paySuccessCallback();
        },

        // 释放递归请求
        destroyWeChatPayPool(){
            clearTimeout(this.payWeChatTypeTime);
            this.triggerPayWeChatPopupUnique = undefined;
        },

        // 关闭微信弹窗
        closePayWeChatPopup(){
            clearTimeout(this.payWeChatTypeTime);
            return popup.$popup && popup.$popup.close(this.triggerPayWeChatPopupUnique);
        }
    }

}