package com.abbidot.tracker.ui.fragment.account.cn

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
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.FragmentAccountCnPhoneBinding
import com.abbidot.tracker.ui.activity.account.AddEmailValidActivity
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.cn.RegisterCNViewModel

/**
 * A simple [Fragment] subclass.
 * Use the [AccountCnPhoneFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class AccountCnPhoneFragment :
    BaseFragment<FragmentAccountCnPhoneBinding>(FragmentAccountCnPhoneBinding::inflate) {

    private val mRegisterCNViewModel: RegisterCNViewModel by viewModels()

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

    override fun initData() {
        super.initData()
        mViewBinding.apply {
            ilAccountCnPhoneEditLayout.let {
                it.tvInputNameTitle.setText(R.string.login_txt_phone)
                it.etInputContent.setHint(R.string.login_txt_phone)
                val filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
                it.etInputContent.filters = filters
                it.etInputContent.inputType = InputType.TYPE_CLASS_NUMBER
                ViewUtil.instance.setInputViewErrorHideClick(
                    mContext!!, it.rlEtInputLayout, tvAccountCnPhoneErrorTip, it.etInputContent
                )
            }
            setOnClickListenerViews(btnAccountCnPhoneContinue)
        }
    }

    override fun liveDataObserve() {
        mRegisterCNViewModel.mSendCodeResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    showToast(R.string.txt_code_success)
                    getHostActivity(AddEmailValidActivity::class.java)?.switchFragment(1)
                    setButtonEnabled(mViewBinding.btnAccountCnPhoneContinue, ConstantInt.Type1)
                }

                override fun onRequestError(exceptionCode: String?) {
                    setButtonEnabled(mViewBinding.btnAccountCnPhoneContinue, ConstantInt.Type1)
                }
            })
        }
    }

    private fun registerCNPhoneContinue() {
        mViewBinding.apply {
            if (!ilAccountCnPhonePolicyTerms.cbLoginAgreePrivacyCheckbox.isChecked) {
                val tips = getString(R.string.register_txt_agree1) + getString(
                    R.string.register_txt_policy
                ) + getString(R.string.register_txt_agree2) + getString(
                    R.string.register_txt_terms
                )
                showToast(tips)
                return
            }
            val phone = ilAccountCnPhoneEditLayout.etInputContent.text.toString()
            if (TextUtils.isEmpty(phone) || phone.length < 11) {
                ViewUtil.instance.setInputViewErrorShow(
                    mContext!!, ilAccountCnPhoneEditLayout.rlEtInputLayout, tvAccountCnPhoneErrorTip
                )
                return
            }
            getHostActivity(AddEmailValidActivity::class.java)?.setToEmail(phone)
            setButtonEnabled(btnAccountCnPhoneContinue, ConstantInt.Type0)
            mRegisterCNViewModel.sendCode(this@AccountCnPhoneFragment, phone, 1)
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnAccountCnPhoneContinue -> registerCNPhoneContinue()
            }
        }
    }
}