package com.develop.auth.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.develop.auth.R
import com.develop.auth.databinding.FragmentMemberForgotBinding
import com.develop.auth.viewmodel.FragmentTag
import com.develop.auth.viewmodel.MemberViewModel
import com.develop.base.mvvm.BaseBVMFragment
import com.develop.common.ui.CommonBVMFragment
import com.drake.net.utils.scopeDialog

class MemberForgotFragment : CommonBVMFragment<FragmentMemberForgotBinding, MemberViewModel>() {


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

    override fun onPostCreateView() {
        super.onPostCreateView()
        binding.tvLogin.setOnClickListener {
            viewModel.fragmentChangeLiveData.postValue(FragmentTag.MEMBER_LOGIN)
        }

        binding.tvSendEmail.setOnClickListener {
            val email = binding.etEmailId.text.toString()
            if (email.isEmpty()) {
                showToast("Please fill in your email")
                return@setOnClickListener
            }
            showPlainDialog()
            viewModel.sendEmail(email)
        }
        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
            }
            showPlainDialog()
            viewModel.resetPassword(email, captcha, password)
        }
        viewModel.resetPasswordLiveData.observe(viewLifecycleOwner) {
            dismissPlainDialog()
            it.msg?.apply {
                showToast(this)
            }
        }
    }

    override fun createViewModel(): MemberViewModel {
        return getViewModelOfActivity(MemberViewModel::class.java)
    }
}