package com.develop.auth.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.develop.auth.R
import com.develop.auth.databinding.FragmentMemberForgotBinding
import com.develop.food.base.ui.ViewBindingFragment

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()
                }
            }
        }
    }

}