package com.zswc.creativity.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageInfo
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.ysnows.base.utils.Toasts
import com.zswc.creativity.model.OrderPay

/**
 *Created by shihuiyun
 *on 2020/9/10
 */
 class WeChatUtil {


    companion object {

        //微信id
        //正式 wx57c846e4b0571008
        //测试 wxa045c3e9012bdd56
        var WECHAT_APP_ID = "wxabf67ad4ef2c67e4" //正式的
        var iwxapi: IWXAPI? = null

        fun regToWx(mContext: Context) {

            val mWXApi: IWXAPI = WXAPIFactory.createWXAPI(mContext, WECHAT_APP_ID, true)
            mWXApi.registerApp(WECHAT_APP_ID)
            if (null == iwxapi) {
                iwxapi = mWXApi
            }
            //建议动态监听微信启动广播进行注册到微信
            mContext.registerReceiver(object : BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent) {
                    // 将该app注册到微信
                    iwxapi!!.registerApp(WECHAT_APP_ID)
                }
            }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
        }

        /**
         * 绑定微信登录
         *
         * @param context
         */
        fun setWXLoigin(context: Context?) {
            if (isWeixinAvilible(context)) {

                if (null == iwxapi) {
                    val mWXApi: IWXAPI = WXAPIFactory.createWXAPI(context, null)
                    mWXApi.registerApp(WECHAT_APP_ID)
                    iwxapi = mWXApi
                }

                val req = SendAuth.Req()
                req.scope = "snsapi_userinfo"
                req.state = "wechat_sdk_demo_test"
                iwxapi!!.sendReq(req)
            } else {
                Toasts.toast(context, "您未安装微信或微信版本过低，无法分享")
            }
        }


        /**
         * 判断是否安装了微信
         *
         * @param context
         *
         * @return
         */
        fun isWeixinAvilible(context: Context?): Boolean {

            if (null == iwxapi) {
                val mWXApi: IWXAPI = WXAPIFactory.createWXAPI(context, null)
                mWXApi.registerApp(WECHAT_APP_ID)
                iwxapi = mWXApi
            }
            return iwxapi!!.isWXAppInstalled
        }




        /* 调支付的方法
    * <p>
    * 注意： 每次调用微信支付的时候都会校验 appid 、包名 和 应用签名的。 这三个必须保持一致才能够成功调起微信
    *
    * @param wxPayBean
    */
        fun toWXPay(context: Context?, bean: OrderPay) {

            var mWXApi: IWXAPI
            if (iwxapi != null) {
                mWXApi = iwxapi!!
            } else {
                mWXApi = WXAPIFactory.createWXAPI(context, null)
                mWXApi.registerApp(WECHAT_APP_ID)
            }

            //这里的bean，是服务器返回的json生成的bean
            val payRequest = PayReq()
            payRequest.appId = WECHAT_APP_ID
            payRequest.partnerId = bean.wechat.partnerid
            payRequest.prepayId = bean.wechat.prepayid
            payRequest.packageValue = "Sign=WXPay" //固定值

            payRequest.nonceStr = bean.wechat.noncestr
            payRequest.timeStamp = bean.wechat.timestamp
            payRequest.sign = bean.wechat.sign

            //发起请求，调起微信前去支付
            mWXApi.sendReq(payRequest)


        }




    }

}
