package com.zxbw.modulemain.presenter

import android.text.TextUtils
import com.zxbw.modulemain.contract.CardDetailContract
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.utils.UserDataUtils
import org.json.JSONObject

class CardDetailPresenter : CardDetailContract.Presenter {


    private val mView: CardDetailContract.View


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


    override fun requestRightsAliPay(goodsId: String, payType: String, phone: String,type:String,skill:Boolean?,outTradeNo:String?) {
        ApiClient.userInfoAPi.aliPayRights(goodsId, payType, phone,type,skill,outTradeNo)
            .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 == 1) {
                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 == 2) {
                val json = JSONObject(order)
                val tradeOrder = ""
                val outTradeOrder = json.getString("extData")
                requestVerify(outTradeOrder, tradeOrder, payType)
            } else if (payType == 11) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else if (payType == 12) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            } else if (payType == 13) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                }
            }else if (payType == 21) {
                if (!TextUtils.isEmpty(order)) {
                    requestVerify(order ?: "", "", payType)
                } else {
                    mView?.verifyOrderFail("支付成功后 订单id为null")
                }
            }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}")
                }
            })
    }


}