package com.hongyegroup.cpt_auth.ui

import android.content.Intent
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.facade.annotation.Route
import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
import com.guadou.cs_router.ARouterPath
import com.guadou.cs_router.YYRouterService
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.utils.KeyboardUtils
import com.guadou.lib_baselib.utils.StatusBarUtils
import com.hongyegroup.cpt_auth.BR
import com.hongyegroup.cpt_auth.R
import com.hongyegroup.cpt_auth.bean.Book
import com.hongyegroup.cpt_auth.databinding.ActivityUserLoginBinding
import com.hongyegroup.cpt_auth.mvvm.vm.UserLoginViewModel
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * 用户登录页面
 */
@Route(path = ARouterPath.PATH_AUTH_PAGE_LOGIN)
@AndroidEntryPoint
class UserLoginActivity : YYBaseVDBActivity<UserLoginViewModel, ActivityUserLoginBinding>() {

    @Inject
    lateinit var iBook: Book

    companion object {
        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, UserLoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun init(savedInstanceState: Bundle?) {
        StatusBarUtils.immersive(this)
        setStatusBarWhiteText()

        initListener()

        iBook.sayBook()
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_user_login, BR.viewModel, mViewModel)
            .addBindingParams(BR.click, ClickProxy())
    }

    override fun startObserve() {
        mViewModel.mLoginSuccessLD.observe(this) {
            if (it != null && it) {
                gotoMainPage()
            }
        }
    }

    private fun initListener() {
        mBinding.cbPsd.setOnCheckedChangeListener { _, isChecked ->
            //设置密码隐藏与显示
            mBinding.etPassword.transformationMethod =
                if (isChecked) HideReturnsTransformationMethod.getInstance() else PasswordTransformationMethod.getInstance()
        }
    }

    private fun gotoMainPage() {
        YYRouterService.mainComponentServer?.startMainPage()
        finish()
    }

    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        var isPasswordType = MutableLiveData(true)  //当前是密码登录模式

        //选电话区号
        fun selectCountryCode(view: View) {
            KeyboardUtils.hideSoftInput(mActivity)
            mViewModel.showCountryCodePopup(view, mActivity)
        }

        //切换为密码登录模式
        fun changePasswordType() {
            KeyboardUtils.hideSoftInput(mActivity)
            isPasswordType.value = true
        }

        //切换为验证码登录模式
        fun changeVerifyCodeType() {
            KeyboardUtils.hideSoftInput(mActivity)
            isPasswordType.value = false
        }

        //获取验证码
        fun getVerityCode() {
            KeyboardUtils.hideSoftInput(mActivity)
            toast("获取验证码")
        }

        //用户协议
        fun userAgreement() {
            KeyboardUtils.hideSoftInput(mActivity)
            toast("用户协议")
        }

        //隐私协议
        fun privateAgreement() {
            KeyboardUtils.hideSoftInput(mActivity)
            toast("隐私协议")
        }

        //点击登录
        fun doLogin() {
            KeyboardUtils.hideSoftInput(mActivity)
            if (mBinding.cbAgree.isChecked) {
                val userPhone = mViewModel.mMobileCodeLiveData.value + mViewModel.mPhoneNumLD.value
                val password = if (isPasswordType.value == true) mViewModel.mPasswordLD.value else mViewModel.mPassCodeLD.value

                mViewModel.requestLogin()

            } else {
                toast("请同意用户协议")
            }
        }

        //微信登录
        fun wechatLogin() {
            KeyboardUtils.hideSoftInput(mActivity)
            toast("微信登录")
        }

        //QQ登录
        fun qqLogin() {
            KeyboardUtils.hideSoftInput(mActivity)
            toast("QQ登录")
        }

        //注册账号
        fun registAccount() {
            KeyboardUtils.hideSoftInput(mActivity)
            UserRegisterActivity.startInstance()
        }

        //忘记密码
        fun forgotPsd() {
            KeyboardUtils.hideSoftInput(mActivity)
            UserForgotActivity.startInstance()
        }
    }
}