package com.yidianling.ydlcommon.custom.pay

import android.app.Activity
import android.content.Context
import com.alipay.sdk.app.PayTask
import com.tencent.mm.opensdk.constants.Build
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.yidianling.ydlcommon.core.base.BaseApplication
import com.yidianling.ydlcommon.custom.http.CustomThrowable
import com.yidianling.ydlcommon.custom.http.response.WXPay
import de.greenrobot.event.EventBus
import io.reactivex.Emitter
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/04/18
 */
object RxPay {

    init {
        EventBus.getDefault().register(this)
    }

    private var emitter: Emitter<PayStatus>? = null

    // 微信支付
    fun wxPay(context: Context, option: WXPay.WXOption): Observable<PayStatus> {
        return Observable.create<PayStatus> { e ->
            val api = WXAPIFactory.createWXAPI(context, null)
            if (api.wxAppSupportAPI < Build.PAY_SUPPORTED_SDK_INT) {
                e.onError(CustomThrowable("目前您的微信版本过低或未安装微信，需要安装微信才能使用"))
                e.onComplete()
            }
            emitter = e

            api.registerApp(option.appid)
            val request = PayReq()
            request.appId = option.appid
            request.partnerId = option.partnerid
            request.prepayId = option.prepayid
            request.packageValue = "Sign=WXPay"
            request.nonceStr = option.noncestr
            request.timeStamp = option.timestamp
            request.sign = option.sign
            api.sendReq(request)
        }.subscribeOn(Schedulers.io())
    }

    // 支付宝支付
    fun aliPay(activity: Activity, aliSign: String): Observable<PayStatus> {
        return Observable.create<PayStatus> {
            val alipay = PayTask(activity)
            val result = alipay.payV2(aliSign, true)
            val payResult = com.yidianling.ydlcommon.custom.pay.PayResult(result)
            when {
                "9000" == payResult.resultStatus -> it.onNext(PayStatus(true))
                else -> it.onError(CustomThrowable(payResult.memo))
            }
            it.onComplete()
        }

    }

    fun onEvent(event: WXPayEvent) {
        if (event.status.success) {
            emitter?.onNext(event.status)
        } else {
            emitter?.onError(Throwable("支付失败", null))
        }
        emitter?.onComplete()
    }

    private fun getRegisterString(): String {
        val packageName = BaseApplication.getApp().packageName
        return when {
            packageName.endsWith("yidianling") -> "wx57a9d930270498c7"
            packageName.endsWith("yidianling_atk_3") -> "wx306289ecfe20f6ae"
            packageName.endsWith("yidianling_atk4") -> "wx82d801f5762fc66d"
            packageName.endsWith("yidianling_atk5") -> "wxf95b97fc0d11ae5e"
            packageName.endsWith("yidianling_atk6") -> "wx7fe55ba2ae53c047"
            packageName.endsWith("yidianling_atk7") -> "wx35596e07c394b2ed"
            packageName.endsWith("xinlizixun") -> "wx1c6af5a11b5f531f"
            else -> "wx57a9d930270498c7"
        }
    }
}