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.modelmsg.SendAuth
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.xty.common.event.WxLoginEvent
import com.xty.common.app.MyApp
import com.xty.common.util.CommonToastUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class  WXEntryActivity : AppCompatActivity(), IWXAPIEventHandler {

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

    }

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

    // 请求结果回调
    override fun onResp(resp: BaseResp?) {
        when (resp!!.errCode) {
            BaseResp.ErrCode.ERR_OK -> {
                if(resp is SendAuth.Resp){
                    EventBus.getDefault().post(WxLoginEvent(resp.code))
                }else if(resp is SendMessageToWX){
                    CommonToastUtils.showSucceedToast("分享完成")
                }
            }
            BaseResp.ErrCode.ERR_USER_CANCEL -> {
            }
            BaseResp.ErrCode.ERR_AUTH_DENIED -> {
            }
            else -> {
            }
        }
        finish()
    }

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


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