package com.zj365.dc.wxapi

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelpay.PayResp
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.xty.common.LogUtils
import com.xty.common.app.MyApp
import com.xty.common.event.WxPayEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class  WXPayEntryActivity : AppCompatActivity(), IWXAPIEventHandler {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MyApp.instance!!.api!!.handleIntent(this.intent,this)

    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }


    override fun onReq(p0: BaseReq?) {
        println("")
    }

    // 请求结果回调
    override fun onResp(resp: BaseResp?) {
        LogUtils.i("onResp ${resp?.type}")
        when (resp!!.errCode) {
            BaseResp.ErrCode.ERR_OK -> {
                if (resp is PayResp) {
                    LogUtils.i("onResp 支付成功")
                    EventBus.getDefault().post(WxPayEvent(BaseResp.ErrCode.ERR_OK, resp.extData))
                }
            }
            BaseResp.ErrCode.ERR_USER_CANCEL -> {
                if (resp is PayResp) {
                    LogUtils.w("onResp 支付取消")
                    EventBus.getDefault()
                        .post(WxPayEvent(BaseResp.ErrCode.ERR_USER_CANCEL, resp.extData))
                }
            }
            BaseResp.ErrCode.ERR_AUTH_DENIED -> {
                if (resp is PayResp) {
                    LogUtils.w("onResp 支付失败")
                    EventBus.getDefault()
                        .post(WxPayEvent(BaseResp.ErrCode.ERR_AUTH_DENIED, resp.extData))
                }
            }
            else -> {
            }
        }
        finish()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun event(any:Any){
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}