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

import android.content.Context
import android.graphics.Paint
import android.text.TextUtils
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.AppUtils
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.bean.UserBean
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentLoginCnAccountBinding
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 LoginCNAccountFragment :
    BaseFragment<FragmentLoginCnAccountBinding>(FragmentLoginCnAccountBinding::inflate) {

    private val mLoginCNViewModel: LoginCNViewModel by viewModels()

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

    override fun initData() {
        super.initData()

        mViewBinding.apply {
            //添加下划线
            loginCnAccountSignUp.paint.flags = Paint.UNDERLINE_TEXT_FLAG
            loginCnAccountForgetPassword.paint.flags = Paint.UNDERLINE_TEXT_FLAG

            setOnClickListenerViews(
                btnLoginCnAccountCode,
                btnLoginCnWx,
                btnStartLoginCnAccount,
                loginCnAccountSignUp,
                loginCnAccountForgetPassword,
                llLoginCnAccountAgree.loginCnPolicy,
                llLoginCnAccountAgree.loginCnTerms
            )
        }
    }

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

        mLoginCNViewModel.mAccountPwdLoginResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    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.llLoginCnAccountAgree.loginAgreePrivacyCheckbox)) {
            return
        }
        ThirdPartyUtil.instance.startWXLogin(activity)
    }

    /**
     *账号密码登录
     */
    private fun accountPwdLogin() {
        val activity = activity as LoginRegisterCNActivity
        if (!activity.isAgreePolicy(mViewBinding.llLoginCnAccountAgree.loginAgreePrivacyCheckbox)) {
            return
        }
        var phone = mViewBinding.loginCnPhoneEd.text.toString()
        var pwd = mViewBinding.loginCnPasswordEd.text.toString()
        if (AppUtils.isDebug()) {
            if (TextUtils.equals("12", phone)) {
                phone = "13049437653"
                pwd = "123456"
            }
        }
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pwd)) {
            showToast(R.string.txt_not_null)
            return
        }

        mLoginCNViewModel.accountPasswordLogin(this, phone, pwd)
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.loginCnAccountSignUp -> {
                val activity = activity as LoginRegisterCNActivity
                activity.switchFragment(LoginRegisterCNActivity.Fragment_Register_Start)
            }

            mViewBinding.loginCnAccountForgetPassword -> {
                val activity = activity as LoginRegisterCNActivity
                activity.switchFragment(LoginRegisterCNActivity.Fragment_Forget_Password_Start)
            }

            mViewBinding.btnStartLoginCnAccount -> accountPwdLogin()
            mViewBinding.btnLoginCnWx -> wechatLogin()

            mViewBinding.btnLoginCnAccountCode -> {
                val activity = activity as LoginRegisterCNActivity
                activity.switchFragment(LoginRegisterCNActivity.Fragment_Code_Login)
            }

            mViewBinding.llLoginCnAccountAgree.loginCnPolicy -> {
                ViewUtil.instance.goPrivacyPolicy(mActivity!!)
            }

            mViewBinding.llLoginCnAccountAgree.loginCnTerms -> {
                ViewUtil.instance.goServiceContract(mActivity!!)
            }
        }
    }
}