package com.abbidot.tracker.wxapi

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.util.ThirdPartyUtil
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler

/**
 *Created  by .yzq on 2022/7/11/011.
 * @link
 * @description:封装微信Activity类
 */
open class WXBaseActivity : FragmentActivity(), IWXAPIEventHandler {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 使用微信SDK官方 handleIntent 校验，这是最可靠的合法性判断
        // handleIntent 内部会校验 AppID、签名、Intent 数据格式等
        val handled = ThirdPartyUtil.mApi.handleIntent(intent, this)
        if (!handled) {
            LogUtil.e("WXBaseActivity", "onCreate 收到非法微信回调 Intent，直接结束")
            finish()
            return
        }
        initData()
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        // singleTask/singleTop 模式下新 Intent 到达，同样走官方校验
        val handled = ThirdPartyUtil.mApi.handleIntent(intent, this)
        if (!handled) {
            LogUtil.e("WXBaseActivity", "onNewIntent 收到非法微信回调 Intent，直接结束")
            finish()
        }
    }

    override fun onReq(baseReq: BaseReq?) {
        baseReq?.let {
            onWXReq(it)
        }
    }

    override fun onResp(baseResp: BaseResp?) {
        baseResp?.let {
            onWXResp(it)
        }
    }

    open fun initData() {}

    /**
     * 处理微信请求回调
     */
    open fun onWXReq(baseReq: BaseReq) {}

    /**
     * 处理微信请求后返回回调
     */
    open fun onWXResp(baseResp: BaseResp) {}
}