package com.abbidot.tracker.util

import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.MyApplication
import com.abbidot.tracker.base.BaseActivity
import com.hjq.toast.Toaster
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory

/**
 *Created  by .yzq on 2022/7/8/008.
 * @link
 * @description:第三方授权工具类，微信和支付宝
 */
class ThirdPartyUtil {

    companion object {
        // APP_ID 替换为你的应用从官方网站申请到的合法appID
        const val WXAppID = "wx19e9c16c9af43924"

        //IWXAPI单例模式, 是第三方 app 和微信通信的 openApi 接口
        val mApi: IWXAPI by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            LogUtil.e("创建微信Api")
            // 通过 WXAPIFactory 工厂，获取 IWXAPI 的实例
            WXAPIFactory.createWXAPI(MyApplication.instance, WXAppID, true)
        }

        val instance: ThirdPartyUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ThirdPartyUtil()
        }
    }

    /**
     * 开始微信登录
     */
    fun startWXLogin(activity: BaseActivity<*>) {
        if (!mApi.isWXAppInstalled) {
            Toaster.show("没有安装微信")
            return
        }
        activity.showLoading(true)
        // send oauth request
        val req = SendAuth.Req()
        req.scope = "snsapi_userinfo"
        req.state = "wechat_sdk_demo_test"
        mApi.sendReq(req)
    }
}