package com.develop.auth.ui

import android.text.InputFilter
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.activityViewModels
import com.develop.auth.R
import com.develop.auth.databinding.FragmentMemberForgotBinding
import com.develop.food.base.ui.ViewBindingFragment
import java.util.regex.Matcher
import java.util.regex.Pattern

class MemberForgotFragment: ViewBindingFragment<FragmentMemberForgotBinding>() {

    private val viewModel by activityViewModels<MemberViewModel>()

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentMemberForgotBinding {
        return FragmentMemberForgotBinding.inflate(inflater, container, false)
    }

    override fun onPostCreateView() {
        binding.tvLogin.setOnClickListener {
            parentFragmentManager
                .beginTransaction()
                .replace(R.id.fl_container, MemberLoginFragment())
                .commitAllowingStateLoss()
        }
        binding.tvSendEmail.setOnClickListener {
            val email = binding.etEmailId.text.toString()
            if (email.isEmpty()) {
                showToast(getString(R.string.please_fill_in_your_email))
                return@setOnClickListener
            }
            showDialog()
            viewModel.sendEmail(email).observe(viewLifecycleOwner) {
                dismissDialog()
                if (it) {
                    showToast(getString(R.string.email_send))
                }
            }
        }
        binding.tvResetPassword.setOnClickListener {
            val email = binding.etEmailId.text.toString()
            val captcha = binding.etCaptcha.text.toString()
            val password = binding.etPassword.text.toString()
            if (email.isEmpty()) {
                showToast(getString(R.string.please_fill_in_your_email))
                return@setOnClickListener
            }
            if (captcha.isEmpty()) {
                showToast(getString(R.string.captcha_can_not_be_empty))
                return@setOnClickListener
            }
            if (password.isEmpty()) {
                showToast(getString(R.string.please_fill_in_the_password))
                return@setOnClickListener
            }
            showDialog()
            viewModel.resetPassword(email, captcha, password).observe(viewLifecycleOwner) {
                dismissDialog()
                if (it) {
                    showToast(getString(R.string.reset_password_success))
                    parentFragmentManager
                        .beginTransaction()
                        .replace(R.id.fl_container, MemberLoginFragment())
                        .commitAllowingStateLoss()
                }
            }
        }
        setEditTextInhibitInputSpeChat(binding.etPassword)
    }


    private fun setEditTextInhibitInputSpeChat(editText: EditText) {
        val filter =
            InputFilter { source, start, end, dest, dstart, dend ->
                val speChat = "[-©®™℅¡¿¥£°^¢€%&-+()~`|•√π÷×¶∆`~!@#$%^=|':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\"'_\\\\]"
                val pattern: Pattern = Pattern.compile(speChat)
                val matcher: Matcher = pattern.matcher(source.toString())
                if (matcher.find()) "" else null
            }
        editText.filters = arrayOf(filter)
    }

}