export const LoginFragment = (configuration) => {
  return `
    package ${configuration.packageName}.ui.fragments

    import android.os.Bundle
    import android.view.View
    import androidx.core.widget.doAfterTextChanged
    import androidx.fragment.app.Fragment
    import androidx.navigation.fragment.findNavController
    import ${configuration.name}.R
    import ${configuration.packageName}.data.local.AppPreferences
    import ${configuration.name}.databinding.FragmentLoginBinding
    import ${configuration.packageName}.extensions.disableSpaces
    import ${configuration.packageName}.extensions.hideSoftKeyboard
    import ${configuration.packageName}.extensions.setOnClickWithDebounce
    import ${configuration.packageName}.extensions.snackBar
    import ${configuration.packageName}.extensions.viewBinding
    import ${configuration.packageName}.network.Status
    import ${configuration.packageName}.viewmodels.BaasViewModel
    import org.koin.android.ext.android.inject
    import androidx.appcompat.app.AppCompatActivity
    import org.koin.androidx.viewmodel.ext.android.viewModel
    
    class LoginFragment : Fragment(R.layout.fragment_login) {
    
        private val binding by viewBinding(FragmentLoginBinding::bind)
        private val baasViewModel: BaasViewModel by viewModel()
        private val pref by inject<AppPreferences>()
        private var email = ""
        private var password = ""
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            binding.btnLogin.setOnClickWithDebounce {
                if (email.isNotEmpty() && password.isNotEmpty()) login(email, password)
                else snackBar(getString(R.string.invalid_email_password))
            }
    
            binding.btnSignup.setOnClickWithDebounce {
                findNavController().navigate(R.id.action_loginFragment_to_signUpFragment)
            }

            binding.btnForgetPassword.setOnClickWithDebounce {
                findNavController().navigate(R.id.action_loginFragment_to_forgetPasswordFragment)
            }
    
            binding.edTxtUsername.doAfterTextChanged { email = it.toString() }
            binding.edTxtPass.doAfterTextChanged { password = it.toString() }
            binding.edTxtUsername.disableSpaces()
            binding.edTxtPass.disableSpaces()
        }
    
        private fun login(email: String, password: String, role: String = "user") {
            requireActivity().hideSoftKeyboard()
            baasViewModel.loginLambda(email, password, role, true).observe(viewLifecycleOwner) {
                when (it.status) {
                    Status.ERROR -> snackBar(it.message ?: getString(R.string.something_went_wrong))
                    Status.LOADING -> {
                        snackBar("Logging in...")
                    }
    
                    Status.SUCCESS -> {
                        val data = it.data
                        pref.saveUser(
                            data?.user_id?.toInt(),
                            data?.token,
                            data?.role,
                            email,
                            data?.photo,
                            data?.first_name,
                            data?.last_name,
                        )
                        findNavController().navigate(R.id.action_loginFragment_to_homeFragment)
                    }
                }
            }
        }

          override fun onResume() {
            super.onResume()
            (activity as AppCompatActivity?)?.supportActionBar?.hide()
        }

        override fun onStop() {
            super.onStop()
            (activity as AppCompatActivity?)?.supportActionBar?.show()
        }

    
    }
    `;
};
