package com.abbidot.tracker.wxapi

import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.deprecated.ui.fragment.vm.LoginCNViewModel
import com.hjq.toast.Toaster
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 *Created  by .yzq on 2022/7/8/008.
 * @link
 * @description:微信登录回调Activity
 */
@AndroidEntryPoint
class WXEntryActivity : WXBaseActivity() {
    @Inject
    lateinit var mLoginCNViewModel: LoginCNViewModel

    override fun initData() {
        mLoginCNViewModel.mWXLoginResultLiveData.observe(this) {
            LogUtil.e(it.toString())
            XEventBus.post(EventName.WXLoginResult, it)
            finish()
        }
    }

    override fun onWXResp(baseResp: BaseResp) {
        if (baseResp.type == ConstantsAPI.COMMAND_SENDAUTH) {
            val result: String
            val r = baseResp as SendAuth.Resp
            when (baseResp.errCode) {
                BaseResp.ErrCode.ERR_OK -> {
                    result = "登录成功"
                    val code = r.code
                    LogUtil.e("${result}TAG_WECHAT_CODE", code)
                    mLoginCNViewModel.wxLogin(code)
                }
                BaseResp.ErrCode.ERR_AUTH_DENIED -> {
                    result = "用户拒绝微信授权登录"
                    LogUtil.e("ERROR", result)
                    Toaster.show(result)
                    finish()
                }
                BaseResp.ErrCode.ERR_USER_CANCEL -> {
                    result = "用户取消微信授权登录"
                    LogUtil.e("ERROR", result)
                    Toaster.show(result)
                    finish()
                }
                else -> {
                    result = "微信授权登录失败,${baseResp.errStr},code=${r.code}"
                    LogUtil.e("ERROR", result)
                    Toaster.show(result)
                    finish()
                }
            }
        }
        //分享回调
        else if (baseResp.type == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
            when (baseResp.errCode) {
                BaseResp.ErrCode.ERR_OK -> Toaster.show("分享成功")
                BaseResp.ErrCode.ERR_AUTH_DENIED -> Toaster.show("分享出错")
                BaseResp.ErrCode.ERR_USER_CANCEL -> Toaster.show("取消分享")
            }
            finish()
        }
    }
}