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

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
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.FragmentForgotPasswordV2ThereBinding
import com.abbidot.tracker.deprecated.ui.activity.vm.ChangePasswordViewModel
import com.abbidot.tracker.ui.activity.account.AddEmailValidActivity
import com.abbidot.tracker.ui.activity.account.ResetPasswordSuccessActivity
import com.abbidot.tracker.util.ViewUtil


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

    private val mChangePasswordViewModel: ChangePasswordViewModel by viewModels()

    private var isShowPassword = false
    private var isShowConfirmPassword = false
    private lateinit var mForgotPasswordV2Activity: AddEmailValidActivity

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

    override fun initData() {
        super.initData()
        mForgotPasswordV2Activity = activity as AddEmailValidActivity
        mViewBinding.apply {

            ilResetPasswordPwd.let {
                it.tvInputNameTitle.setText(R.string.login_txt_password)
                it.etInputContent.setHint(R.string.login_txt_password)
                ViewUtil.instance.setShowAndHidePassword(
                    it.etInputContent, it.ivInputRepresentImage
                )
                it.etInputContent.addTextChangedListener { str ->
                    val surePassword = ilResetPasswordPwdSure.etInputContent.text.toString()
                    it.rlEtInputLayout.background = ContextCompat.getDrawable(
                        mContext!!, R.drawable.shape8_gray_border_white_bg
                    )
                    if (!TextUtils.isEmpty(surePassword)) {
                        tvResetPasswordPwdErrorTip.setTextColor(
                            ContextCompat.getColor(
                                mContext!!, R.color.blue_color1
                            )
                        )
                    }
                    str?.apply {
                        if (length < 6 || surePassword.length < 6) {
                            tvResetPasswordPwdErrorTip.setCompoundDrawablesWithIntrinsicBounds(
                                0, 0, 0, 0
                            )
                        } else {
                            tvResetPasswordPwdErrorTip.setCompoundDrawablesWithIntrinsicBounds(
                                R.drawable.icon_input_check_svg, 0, 0, 0
                            )
                        }
                    }
                }
            }

            ilResetPasswordPwdSure.let {
                it.tvInputNameTitle.setText(R.string.txt_confirm_password)
                it.etInputContent.setHint(R.string.login_txt_password)
                ViewUtil.instance.setShowAndHidePassword(
                    it.etInputContent, it.ivInputRepresentImage
                )
                it.etInputContent.addTextChangedListener { str ->
                    val password = ilResetPasswordPwd.etInputContent.text.toString()
                    it.rlEtInputLayout.background = ContextCompat.getDrawable(
                        mContext!!, R.drawable.shape8_gray_border_white_bg
                    )
                    if (!TextUtils.isEmpty(password)) {
                        tvResetPasswordPwdErrorTip.setTextColor(
                            ContextCompat.getColor(
                                mContext!!, R.color.blue_color1
                            )
                        )
                    }
                    str?.apply {
                        if (length < 6 || password.length < 6) {
                            tvResetPasswordPwdErrorTip.setCompoundDrawablesWithIntrinsicBounds(
                                0, 0, 0, 0
                            )
                        } else {
                            tvResetPasswordPwdErrorTip.setCompoundDrawablesWithIntrinsicBounds(
                                R.drawable.icon_input_check_svg, 0, 0, 0
                            )
                        }
                    }
                }
            }

            setOnClickListenerViews(
                btnResetNowPwd,
                ilResetPasswordPwd.ivInputRepresentImage,
                ilResetPasswordPwdSure.ivInputRepresentImage
            )
        }
    }

    override fun liveDataObserve() {
        mChangePasswordViewModel.mResetPasswordLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        mForgotPasswordV2Activity.startActivityFinish(
                            Intent(
                                mContext, ResetPasswordSuccessActivity::class.java
                            )
                        )
                    }
                }
            })
        }
    }

    private fun resetPwd() {
        mViewBinding.apply {
            val password = ilResetPasswordPwd.etInputContent.text.toString()
            val surePassword = ilResetPasswordPwdSure.etInputContent.text.toString()
            var isSure = true

            if (TextUtils.isEmpty(password) || password.length < 6) {
                isSure = false
                ViewUtil.instance.setInputViewErrorShow(
                    mContext!!, ilResetPasswordPwd.rlEtInputLayout, tvResetPasswordPwdErrorTip
                )
            }
            if (TextUtils.isEmpty(surePassword) || surePassword.length < 6) {
                isSure = false
                ViewUtil.instance.setInputViewErrorShow(
                    mContext!!, ilResetPasswordPwdSure.rlEtInputLayout, tvResetPasswordPwdErrorTip
                )
            }

            if (!isSure) return

            if (password != surePassword) {
                showToast(R.string.txt_password_no_same, gravity = Gravity.CENTER)
                return
            }

            val pwd = Utils.get32Md5Value(password)
            mChangePasswordViewModel.resetPassword(
                mForgotPasswordV2Activity, mForgotPasswordV2Activity.mUserEmail, pwd
            )
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnResetNowPwd -> resetPwd()

                ilResetPasswordPwd.ivInputRepresentImage -> {
                    isShowPassword = !isShowPassword
                    ViewUtil.instance.showAndHidePasswordClick(
                        ilResetPasswordPwd.etInputContent,
                        ilResetPasswordPwd.ivInputRepresentImage,
                        isShowPassword
                    )
                }

                ilResetPasswordPwdSure.ivInputRepresentImage -> {
                    isShowConfirmPassword = !isShowConfirmPassword
                    ViewUtil.instance.showAndHidePasswordClick(
                        ilResetPasswordPwdSure.etInputContent,
                        ilResetPasswordPwdSure.ivInputRepresentImage,
                        isShowConfirmPassword
                    )
                }
            }
        }
    }
}