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.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentLoginCnCodeBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.LoginCNViewModel
import com.abbidot.tracker.deprecated.ui.fragment.vm.RegisterCNViewModel
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.CountDownTimerViewModel


/**
 * 国内验证码登录
 */
class LoginCNCodeFragment :
    BaseFragment<FragmentLoginCnCodeBinding>(FragmentLoginCnCodeBinding::inflate) {

    private val mRegisterCNViewModel: RegisterCNViewModel by viewModels()
    private val mCountDownTimerViewModel: CountDownTimerViewModel by viewModels()
    private val mLoginCNViewModel: LoginCNViewModel by viewModels()

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

    override fun initData() {
        super.initData()

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

            setOnClickListenerViews(
                loginCnCodeUserPassword,
                btnStartLoginCnCode,
                llLoginCnCodeSendCode.btnGetCode,
                llLoginCnCodeAgree.loginCnTerms,
                llLoginCnCodeAgree.loginCnPolicy
            )
        }
    }

    override fun liveDataObserve() {
        mRegisterCNViewModel.mSendCodeResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    mCountDownTimerViewModel.startCountDown(60)
                    showToast(R.string.txt_code_success)
                }
            })
        }

        //倒计时
        mCountDownTimerViewModel.mCountDowningLiveData.observe(viewLifecycleOwner) {
            mViewBinding.llLoginCnCodeSendCode.btnGetCode.apply {
                isEnabled = false
                text = "${it}s"
            }
        }
        mCountDownTimerViewModel.mCountDownEndLiveData.observe(viewLifecycleOwner) {
            mViewBinding.llLoginCnCodeSendCode.btnGetCode.apply {
                isEnabled = true
                text = getString(R.string.register_txt_get_code)
            }
        }

        mLoginCNViewModel.mPhoneLoginResultLiveData.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 codeLogin() {
        val activity = activity as LoginRegisterCNActivity
        if (!activity.isAgreePolicy(mViewBinding.llLoginCnCodeAgree.loginAgreePrivacyCheckbox)) {
            return
        }

        val phone = mViewBinding.llLoginCnCodeSendCode.etGetCodePhone.text.toString()
        val code = mViewBinding.llLoginCnCodeSendCode.etPhoneCode.text.toString()
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            showToast(R.string.txt_not_null)
            return
        }

        mLoginCNViewModel.phoneLogin(this, phone, code)
    }

    /**
     * 获取验证码
     */
    private fun getPhoneCode() {
        val activity = activity as LoginRegisterCNActivity
        if (!activity.isAgreePolicy(mViewBinding.llLoginCnCodeAgree.loginAgreePrivacyCheckbox)) {
            return
        }

        val phone = mViewBinding.llLoginCnCodeSendCode.etGetCodePhone.text.toString()
        if (TextUtils.isEmpty(phone)) {
            showToast(R.string.txt_register_phone)
            return
        }
        mRegisterCNViewModel.sendCode(this, phone, 1)
    }

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

            mViewBinding.btnStartLoginCnCode -> codeLogin()
            mViewBinding.llLoginCnCodeSendCode.btnGetCode -> getPhoneCode()
            mViewBinding.llLoginCnCodeAgree.loginCnPolicy -> {
                ViewUtil.instance.goPrivacyPolicy(mActivity!!)
            }

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