package com.ym.game.activity

import android.content.Intent
import android.view.View
import com.umeng.analytics.MobclickAgent
import com.ym.game.GameActivity
import com.ym.library.net.*
import com.ym.library.utils.*
import com.ym.library.utils.EventUtils.onEvent
import com.ym.xynt.R
import kotlinx.android.synthetic.main.activity_wx_login.*
import org.jetbrains.anko.act

/**
 * @author (wangXuewei)
 * @datetime 2020-10-23 18:19 GMT+8
 * @detail :
 */
class WxLoginActivity : MyBaseActivity(), WxImplUtils.IWxLoginCallback {
    override fun layoutID(): Int {
        return R.layout.activity_wx_login
    }

    override fun init() {
//        startActivity(
//            Intent(this@WxLoginActivity, GameActivity::class.java)
//        )
//        finish()
        onEvent("login_show", "登录页")
        if (SettingPreference.getIsShowGuide()) {
            SettingPreference.setIsShowGuide(false)
        }
        id_btn_wx_login.setOnClickListener(View.OnClickListener {
            wxLogin();
        })
        id_tv_User_agreement.setOnClickListener(View.OnClickListener {
            JumpUtils.h5Jump("用户协议", NetConfig.H5.WEB_URL_USER, true,act)
        })
        id_tv_privacy_policy.setOnClickListener(View.OnClickListener {
            JumpUtils.h5Jump("隐私政策", NetConfig.H5.WEB_URL_PRIVACY, true,act)
        })
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

    fun wxLogin() {
        onEvent("login_click", "点击微信登录")
        WxImplUtils().onAuth(act, this)
//        WxImplUtils().onAuth(
//            act,this
//        ) { token, openid ->
//            Log.i("WxImplUtils", "onWxSuccess")
//            Utils.showToast(act, "登录成功")
//            onEvent("login_wx", "登录授权成功")
//            startActivity(
//                Intent(
//                    this@WxLoginActivity,
//                    GameActivity::class.java
//                )
//            )
//            finish()
//        }

//        val token =AdPreference.getToken()
//        Log.d("wxw","token"+token)
//        UserApiClient.userApi.getIsBindWithdrawWx(token)
//            .compose(RxSchedulers.observableIO2Main())
//            .subscribe(object : BaseObserver<UserInfoEntity>() {
//                override fun onSuccess(data: UserInfoEntity) {
//                    act?.runOnUiThread {
//                        if (data?.isBind == true) {
//                            onEvent("login_net", "已绑定微信")
//                            startActivity(Intent(this@WxLoginActivity, GameActivity::class.java))
//                            finish()
//                        } else {
//                            onEvent("login_net", "未绑定微信,吊起微信授权")
//                            WxImplUtils().onAuth(
//                                act
//                            ) { token, openid ->
//                                Log.i("WxImplUtils", "onWxSuccess")
//                                Utils.showToast(act, "登录成功")
//                                onEvent("login_wx", "登录授权成功")
//                                startActivity(
//                                    Intent(
//                                        this@WxLoginActivity,
//                                        GameActivity::class.java
//                                    )
//                                )
//                                finish()
//                            }
//                        }
//                    }
//                }
//
//                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
//                    if (null != errorMsg) {
//                        onEvent("login_net", "登录接口失败")
//                        // mView.wxBindFailure(errorMsg?:"")
//
//                        Utils.showToast(act, ""+errorMsg)
//                    }
//                }
//            })
    }

    override fun onWxSuccess(token: String?, openid: String?) {
        onEvent("login_success", "登录成功")
        startActivity(
            Intent(this@WxLoginActivity, GameActivity::class.java)
        )
        finish()
    }

    override fun onWxFail(mes: String?) {
        onEvent("login_fail", "登录失败$mes")
        Utils.showToast(act, mes)
    }


}