package com.zxbw.modulemain.presenter

import android.app.Activity
import android.text.TextUtils
import android.util.Log
import com.zxbw.modulemain.contract.RightsPayContract
import com.zxhl.cms.AppContext
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.AppInEntity
import com.zxhl.cms.net.model.box.BoxInfoEntity
import com.zxhl.cms.net.model.box.VerifacationEntity
import com.zxhl.cms.net.model.uc.AliPayEntity
import com.zxhl.cms.net.model.uc.PayMinEntity
import com.zxhl.cms.net.model.uc.PayVerifyEntity
import com.zxhl.cms.net.model.uc.WeChatPayEntity
import com.zxhl.cms.net.model.video.MemberEntity
import com.zxhl.cms.pay.alipay.AlipayServer
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.UserDataUtils
import com.zxhl.cms.utils.WeChatPay
import org.json.JSONObject

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


    private val mView: RightsPayContract.View


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


    override fun requestRightsAliPay(goodsId: String, payType: String, phone: String) {
        ApiClient.userInfoAPi.aliPayRights(goodsId, payType, phone, "50")
            .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}")
                }
            })
    }


}