import { PayStatusEnum } from '@/enums/appEnums'
import { handleClientEvent } from '../client'
//#ifdef H5
import wechatOa from '../wechat'
//#endif
export class Wechat {
    init(name: string, pay: any) {
        pay[name] = this
    }

    async run(options: any) {
        try {
            const res = await handleClientEvent({
                MP_WEIXIN: () => {
                    return new Promise((resolve) => {
                        uni.requestPayment({
                            orderInfo: '',
                            provider: 'wxpay',
                            timeStamp: options.timeStamp,
                            nonceStr: options.nonceStr,
                            package: options.packageValue,
                            paySign: options.paySign,
                            signType: options.signType,
                            success() {
                                resolve(PayStatusEnum.SUCCESS)
                            },
                            fail(e) {
                                console.log(e)
                                resolve(PayStatusEnum.FAIL)
                            }
                        })
                    })
                },
                OA_WEIXIN: () => {
                    return new Promise((resolve) => {
                        wechatOa
                            .pay(options)
                            .then(() => {
                                resolve(PayStatusEnum.SUCCESS)
                            })
                            .catch((e) => {
                                resolve(PayStatusEnum.FAIL)
                            })
                    })
                },
                H5: () => {
                    return new Promise((resolve) => {
                        window.open(options.url, '_self')
                        resolve(PayStatusEnum.PENDING)
                    })
                },
                ANDROID: () => {
                    const config = {
                        appid: options.appid,
                        partnerid: options.partnerId,
                        prepayid: options.prepayId,
                        package: options.packageValue,
                        noncestr: options.noncestr,
                        timestamp: options.timestamp,
                        sign: options.sign
                    }
                    return new Promise((resolve, reject) => {
                        uni.requestPayment({
                            provider: 'wxpay',
                            orderInfo: config,
                            success() {
                                resolve(PayStatusEnum.SUCCESS)
                            },
                            fail(error) {
                                console.log(error)
                                resolve(PayStatusEnum.FAIL)
                            }
                        })
                    })
                },
                IOS: () => {
                    const config = {
                        appid: options.appid,
                        partnerid: options.partnerId,
                        prepayid: options.prepayId,
                        package: options.packageValue,
                        noncestr: options.noncestr,
                        timestamp: options.timestamp,
                        sign: options.sign
                    }
                    return new Promise((resolve, reject) => {
                        options.partnerId = String(options.partnerId)
                        uni.requestPayment({
                            provider: 'wxpay',
                            orderInfo: config,
                            success() {
                                resolve(PayStatusEnum.SUCCESS)
                            },
                            fail(err) {
                                console.log(err)
                                resolve(PayStatusEnum.FAIL)
                            }
                        })
                    })
                }
            })
            return res
        } catch (error) {
            return Promise.reject(error)
        }
    }
}
