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

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.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentForgetPasswordCnStartBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.RegisterCNViewModel
import com.abbidot.tracker.vm.CountDownTimerViewModel


/**
 * 国内忘记密码
 */
class ForgetPasswordCNStartFragment : BaseFragment<FragmentForgetPasswordCnStartBinding>(
    FragmentForgetPasswordCnStartBinding::inflate
) {

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

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

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

            setOnClickListenerViews(
                btnForgetPasswordCnNext,
                llForgetPasswordGetCode.btnGetCode,
                forgetPasswordReturnLoginCn
            )
        }

        val activity = activity as LoginRegisterCNActivity
        activity.apply {
            if (mType == ConstantInt.Close) {
                setTopTitle(getString(R.string.txt_find_password))
            } else {
                setTopTitle(getString(R.string.txt_change_pwd))
                mViewBinding.let {
                    val phone = MMKVUtil.getString(MMKVKey.Phone)
                    it.llForgetPasswordGetCode.etGetCodePhone.setText(phone)
                    it.llForgetPasswordGetCode.etGetCodePhone.isFocusable = false
                    it.forgetPasswordReturnLoginCn.visibility = View.GONE
                }
            }
            setTopContent("")
        }
    }

    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.llForgetPasswordGetCode.btnGetCode.apply {
                isEnabled = false
                text = "${it}s"
            }
        }
        mCountDownTimerViewModel.mCountDownEndLiveData.observe(viewLifecycleOwner) {
            mViewBinding.llForgetPasswordGetCode.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_Forget_Password_Finish)
                }
            })
        }
    }

    /**
     * 忘记密码下一步
     */
    private fun forgetPasswordNext() {
        val phone = mViewBinding.llForgetPasswordGetCode.etGetCodePhone.text.toString()
        val code = mViewBinding.llForgetPasswordGetCode.etPhoneCode.text.toString()
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            showToast(R.string.txt_not_null)
            return
        }
        val activity = activity as LoginRegisterCNActivity
        activity.mPhone = phone
        mRegisterCNViewModel.validatePhoneCode(this, phone, code, 4)
    }

    /**
     * 获取验证码
     */
    private fun getPhoneCode() {
        val phone = mViewBinding.llForgetPasswordGetCode.etGetCodePhone.text.toString()
        if (TextUtils.isEmpty(phone)) {
            showToast(R.string.txt_register_phone)
            return
        }
        mRegisterCNViewModel.sendCode(this, phone, 4)
    }

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

            mViewBinding.llForgetPasswordGetCode.btnGetCode -> getPhoneCode()
        }
    }
}