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

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.FragmentRegisterCnStartBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.RegisterCNViewModel
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.CountDownTimerViewModel


/**
 * 国内开始注册
 */
class RegisterCNStartFragment :
    BaseFragment<FragmentRegisterCnStartBinding>(FragmentRegisterCnStartBinding::inflate) {

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

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

    override fun initData() {
        super.initData()
        mViewBinding.apply {
            //添加下划线
            returnToLoginCn.paint.flags = Paint.UNDERLINE_TEXT_FLAG

            setOnClickListenerViews(llRegisterCnGetPhoneCode.btnGetCode, returnToLoginCn,
                    btnNextRegisterCnCode, llRegisterCnCodeAgree.loginCnTerms,
                    llRegisterCnCodeAgree.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.llRegisterCnGetPhoneCode.btnGetCode.apply {
                isEnabled = false
                text = "${it}s"
            }
        }
        mCountDownTimerViewModel.mCountDownEndLiveData.observe(viewLifecycleOwner) {
            mViewBinding.llRegisterCnGetPhoneCode.btnGetCode.apply {
                isEnabled = true
                text = getString(R.string.register_txt_get_code)
            }
        }

        mRegisterCNViewModel.mValidateCodeResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    val activity = activity as LoginRegisterCNActivity
                    activity.switchFragment(LoginRegisterCNActivity.Fragment_Register_Finish)
                }
            })
        }
    }

    /**
     * 注册下一步
     */
    private fun registerCNNext() {
        val activity = activity as LoginRegisterCNActivity
        if (!activity.isAgreePolicy(mViewBinding.llRegisterCnCodeAgree.loginAgreePrivacyCheckbox)) {
            return
        }
        val phone = mViewBinding.llRegisterCnGetPhoneCode.etGetCodePhone.text.toString()
        val code = mViewBinding.llRegisterCnGetPhoneCode.etPhoneCode.text.toString()
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            showToast(R.string.txt_not_null)
            return
        }

        activity.mPhone = phone
        mRegisterCNViewModel.validatePhoneCode(this, phone, code, 3)
    }

    /**
     * 获取验证码
     */
    private fun getPhoneCode() {
        val activity = activity as LoginRegisterCNActivity
        if (!activity.isAgreePolicy(mViewBinding.llRegisterCnCodeAgree.loginAgreePrivacyCheckbox)) {
            return
        }
        val phone = mViewBinding.llRegisterCnGetPhoneCode.etGetCodePhone.text.toString()
        if (TextUtils.isEmpty(phone)) {
            showToast(R.string.txt_register_phone)
            return
        }
        mRegisterCNViewModel.sendCode(this, phone, 3)
    }

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

            mViewBinding.llRegisterCnGetPhoneCode.btnGetCode -> getPhoneCode()

            mViewBinding.llRegisterCnCodeAgree.loginCnPolicy -> {
                ViewUtil.instance.goPrivacyPolicy(mActivity!!)
            }
            mViewBinding.llRegisterCnCodeAgree.loginCnTerms -> {
                ViewUtil.instance.goServiceContract(mActivity!!)
            }

        }
    }
}