package com.zxbw.modulemain.presenter

import android.text.TextUtils
import com.zxbw.modulemain.contract.RightsPayContract
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.uc.AliPayEntity
import com.zxhl.cms.net.model.uc.PayVerifyEntity
import com.zxhl.cms.pay.PaymentConfig
import com.zxhl.cms.utils.UserDataUtils
import org.json.JSONObject

/**
 */
class RightsPayPresenter : RightsPayContract.Presenter {


    private val mView: RightsPayContract.View


    constructor(mView: RightsPayContract.View) {
        this.mView = mView
    }


    override fun requestRightsAliPay(
        jumpScheme: String,
        goodsId: String,
        payType: String,
        phone: String
    ) {
        ApiClient.userInfoAPi.aliPayRights(jumpScheme, goodsId, payType, phone, "50", false, "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AliPayEntity>() {
                override fun onSuccess(result: AliPayEntity?) {
                    if (result != null) {
                        mView?.requestOrderSuc(result)
                    } else {
                        mView?.requestOrderFail("zfb订单支付请求成功 数据为空")
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    mView?.verifyOrderFail("zfb订单支付请求失败 code:${code} msg:${errorMsg}")
                }
            })
    }

    override fun verifyPay(order: String?, payType: Int) {
        try {
            if (payType == PaymentConfig.ALI_PAY) {
                val json = JSONObject(order)
                val obj = json.getJSONObject("alipay_trade_app_pay_response")
                val tradeOrder = obj.getString("trade_no")
                val outTradeOrder = obj.getString("out_trade_no")
                requestVerify(outTradeOrder, tradeOrder, payType)
            } else if (payType == PaymentConfig.WECHAT_PAY) {
                val json = JSONObject(order)
                val tradeOrder = ""
                val outTradeOrder = json.getString("extData")
                requestVerify(outTradeOrder, tradeOrder, payType)
            } else if (payType == PaymentConfig.MINI_PROGRAM) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else if (payType == 12) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else if (payType == PaymentConfig.MIN_PAY_PROGRAM) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else if (payType == PaymentConfig.H5_PAY) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                } else {
                    mView?.verifyOrderFail("支付成功后 订单id为null")
                }
            } else if (payType == PaymentConfig.SD_PAY) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else if (payType == PaymentConfig.ZFB_H5_PAY||payType == PaymentConfig.WX_H5_PAY|| payType == PaymentConfig.HUI_FU_H5_PAY1|| payType == PaymentConfig.HUI_FU_H5_PAY2) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else {
                mView.verifyOrderFail("支付成功后 开始校验订单异常 e:支付方式异常$payType")
            }
        } catch (e: Exception) {
            mView.verifyOrderFail("支付成功后 开始校验订单异常 e:${e.cause} msg:${e.message}")
        }
    }

    private fun requestVerify(outTradeOrder: String, tradeOrder: String, payType: Int) {
        ApiClient.userInfoAPi.verifyPay(outTradeOrder, tradeOrder, payType)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<PayVerifyEntity>() {
                override fun onSuccess(result: PayVerifyEntity) {
                    UserDataUtils.updateUserInfo(null)
                    if (TextUtils.equals("200", result.status)) {
                        mView?.verifyOrderSuc(result.payStatus)
                    } else {
                        mView?.verifyOrderFail("订单校验请求成功 校验异常${result?.status ?: -1}")
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    mView?.verifyOrderFail("订单校验请求失败：code:${code} msg:${errorMsg}")
                }
            })
    }


}