package com.develop.auth.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
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("Please fill in your email")
                return@setOnClickListener
            }
            showDialog()
            viewModel.sendEmail(email).observe(viewLifecycleOwner) {
                dismissDialog()
                if (it) {
                    showToast("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("Please fill in your email")
                return@setOnClickListener
            }
            if (captcha.isEmpty()) {
                showToast("Please fill in the captcha")
                return@setOnClickListener
            }
            if (password.isEmpty()) {
                showToast("Please fill in the password")
                return@setOnClickListener
            }
            showDialog()
            viewModel.resetPassword(email, captcha, password).observe(viewLifecycleOwner) {
                dismissDialog()
                if (it) {
                    showToast("Reset password success!")
                }
            }
        }
    }

}