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

import android.content.Context
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.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentForgotPasswordV2OneBinding
import com.abbidot.tracker.ui.activity.account.AddEmailValidActivity
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.UserProfileViewModel


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

    private val mRegisterViewModel: UserProfileViewModel by viewModels()

    //当前Fragment所在的下标
    private var mCurrentFragmentPotion = 0

    private lateinit var mForgotPasswordV2Activity: AddEmailValidActivity

    private var mEmailCodeType = 0
    private var mTitle = ""

    companion object {
        @JvmStatic
        fun newInstance(
            context: Context, title: String, emailCodeType: Int, cFragmentPotion: Int = 0
        ) = ForgotPasswordV2OneFragment().apply {
            mContext = context
            mCurrentFragmentPotion = cFragmentPotion
            mEmailCodeType = emailCodeType
            mTitle = title
        }
    }

    override fun initData() {
        super.initData()
        mForgotPasswordV2Activity = activity as AddEmailValidActivity
        mViewBinding.apply {
            tvChangePwdTitle.text = mTitle
            ilChangePwdEmail.let {
                it.tvInputNameTitle.setText(R.string.txt_email_address)
                it.etInputContent.setHint(R.string.login_txt_email)
                it.etInputContent.setText(mForgotPasswordV2Activity.mUserEmail)
                it.etInputContent.inputType = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
                ViewUtil.instance.setInputViewErrorHideClick(
                    mContext!!, it.rlEtInputLayout, tvEmailErrorTipChangePwd, it.etInputContent
                )
            }

            setOnClickListenerViews(btnForgotPasswordV2OneContinue)
        }
    }

    override fun liveDataObserve() {
        mRegisterViewModel.mGetEmailCodeLiveData.observe(viewLifecycleOwner) {
            mViewBinding.btnForgotPasswordV2OneContinue.isEnabled = true
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        //跳转下一个页面
                        mForgotPasswordV2Activity.switchFragment(mCurrentFragmentPotion + 1)
                    }
                }
            })
        }
    }

    private fun continueClick() {
        mViewBinding.apply {
            val email = ilChangePwdEmail.etInputContent.text.toString()
            if (TextUtils.isEmpty(email) || !Utils.isValidEmail(email)) {
                ViewUtil.instance.setInputViewErrorShow(
                    mContext!!, ilChangePwdEmail.rlEtInputLayout, tvEmailErrorTipChangePwd
                )
                return
            }

            mForgotPasswordV2Activity.setToEmail(email)

            btnForgotPasswordV2OneContinue.isEnabled = false
            mRegisterViewModel.getEmailCode(mForgotPasswordV2Activity, email, mEmailCodeType)
        }
    }

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