package com.abbidot.tracker.deprecated.ui.fragment.login

import android.content.Context
import android.view.View
import androidx.fragment.app.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.bean.UserBean
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentLoginCnFirstBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.LoginCNViewModel
import com.abbidot.tracker.util.ThirdPartyUtil
import com.abbidot.tracker.util.ViewUtil

/**
 * 登录第一步
 */
class LoginCNFirstFragment :
    BaseFragment<FragmentLoginCnFirstBinding>(FragmentLoginCnFirstBinding::inflate) {

    private val mLoginCNViewModel: LoginCNViewModel by viewModels()

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = LoginCNFirstFragment().apply {
            mContext = context
        }
    }

    override fun initData() {
        super.initData()

        setOnClickListenerViews(mViewBinding.btnLoginCnWechat, mViewBinding.btnLoginCnAccount,
                mViewBinding.btnLoginCnCode, mViewBinding.llLoginCnPolicyTerms.loginCnPolicy,
                mViewBinding.llLoginCnPolicyTerms.loginCnTerms)
    }

    override fun liveDataObserve() {
        //微信登录成功返回
        XEventBus.observe(this, EventName.WXLoginResult) { it: Result<UserBean> ->
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    LogUtil.e("LoginCNFirstFragment微信登录成功返回,页面可见$isVisible")
                    if (!isVisible) {
                        return
                    }
                    val data = it.getOrNull()
                    data?.apply {
                        val activity = activity as LoginRegisterCNActivity
                        mLoginCNViewModel.loginSuccess(activity, this)
                    }
                }
            })
        }
    }

    /**
     * 微信登录
     */
    private fun wechatLogin() {
        val activity = activity as LoginRegisterCNActivity
        if (!activity.isAgreePolicy(mViewBinding.llLoginCnPolicyTerms.loginAgreePrivacyCheckbox)) {
            return
        }
        ThirdPartyUtil.instance.startWXLogin(activity)
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.btnLoginCnWechat -> wechatLogin()

            mViewBinding.btnLoginCnAccount -> {
                val activity = activity as LoginRegisterCNActivity
                activity.switchFragment(LoginRegisterCNActivity.Fragment_Account_Login)
            }
            mViewBinding.btnLoginCnCode -> {
                val activity = activity as LoginRegisterCNActivity
                activity.switchFragment(LoginRegisterCNActivity.Fragment_Code_Login)
            }
            mViewBinding.llLoginCnPolicyTerms.loginCnPolicy -> {
                ViewUtil.instance.goPrivacyPolicy(mActivity!!)
            }
            mViewBinding.llLoginCnPolicyTerms.loginCnTerms -> {
                ViewUtil.instance.goServiceContract(mActivity!!)
            }
        }
    }

}