package com.gh.gamecenter.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.gh.gamecenter.WechatPayProviderImpl
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.core.provider.IConfigProvider
import com.gh.gamecenter.core.provider.IWechatPayProvider
import com.gh.gamecenter.core.provider.IWechatPayResultProvider
import com.gh.gamecenter.feature.entity.OrderEntity
import com.gh.gamecenter.feature.eventbus.EBPayState
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.therouter.TheRouter

class WXPayEntryActivity : Activity(), IWXAPIEventHandler {

    private lateinit var api: IWXAPI

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val config = TheRouter.get(IConfigProvider::class.java)
        api = WXAPIFactory.createWXAPI(this, config!!.getWechatAppId(), false)
        api.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        api.handleIntent(intent, this)
    }

    override fun onReq(reqeust: BaseReq?) {
    }

    override fun onResp(response: BaseResp) {
        if (response.type == ConstantsAPI.COMMAND_PAY_BY_WX) {
            val payProvider = TheRouter.get(IWechatPayProvider::class.java)
            val order = payProvider?.order as? OrderEntity
            when (response.errCode) {
                PAY_RESULT_SUCCESSFULLY -> {

                    if (payProvider is WechatPayProviderImpl) {
                        val nonceStr = payProvider.orderNo
                        TheRouter.get(IWechatPayResultProvider::class.java)?.onPayComplete(nonceStr, order)
                    }
                }

                PAY_RESULT_FAILURE -> {
                    SensorsBridge.trackMemberRechargeResult(
                        PAYMENT_TYPE_WECHAT,
                        order?.setMenuName ?: "",
                        order?.paymentAmount ?: "",
                        RECHARGE_RESULT_FAILURE
                    )

                    org.greenrobot.eventbus.EventBus.getDefault().post(EBPayState.PayFail)
                }

                PAY_RESULT_CANCEL -> {
                    SensorsBridge.trackMemberRechargeResult(
                        PAYMENT_TYPE_WECHAT,
                        order?.setMenuName ?: "",
                        order?.paymentAmount ?: "",
                        RECHARGE_RESULT_CANCEL
                    )
                    org.greenrobot.eventbus.EventBus.getDefault().post(EBPayState.PayCancel)
                }
            }
            payProvider?.clear()
            finish()
        }
    }

    companion object {
        private const val PAY_RESULT_SUCCESSFULLY = 0
        private const val PAY_RESULT_FAILURE = -1
        private const val PAY_RESULT_CANCEL = -2
        private const val PAYMENT_TYPE_WECHAT = "微信"
        private const val RECHARGE_RESULT_CANCEL = "充值取消"
        private const val RECHARGE_RESULT_FAILURE = "充值失败"
    }
}