package com.app.simplitend.welcome.welcomecg.fragments;

import static com.app.simplitend.welcome.welcomecg.fragments.CgCheckEmailFragment.CG_USER_XID;
import static com.app.simplitend.welcome.welcomecg.fragments.CgCheckEmailFragment.FORGOT_EMAIL;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.databinding.CgForgotPasswordBinding;
import com.app.simplitend.welcome.welcomecg.WelcomeContracts;
import com.app.simplitend.welcome.welcomecg.mvvm.CgForgotPwdResult;
import com.app.simplitend.welcome.welcomecg.mvvm.CgWelcomeViewModel;

public class CgForgotPasswordFragment extends Fragment implements WelcomeContracts.ForgotPwdSentCodeCallback {

    protected CgForgotPasswordBinding binding;

    private CgWelcomeViewModel viewModel;

    private ProgressDialog progressDialog;

    String mEmail;

    public CgForgotPasswordFragment(){
        // required
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = CgForgotPasswordBinding.inflate(inflater, container, false);

        viewModel = new ViewModelProvider(requireActivity()).get(CgWelcomeViewModel.class);

        initViews();

        clickEvents();

        return binding.getRoot();
    }

    private void clickEvents() {

        binding.backBtn.setOnClickListener(view -> {
            requireActivity().onBackPressed();
        });

        binding.submit.setOnClickListener(v -> {

            if (!Patterns.EMAIL_ADDRESS.matcher(binding.emailAddress.getText().toString().trim()).matches()){
                binding.emailAddress.setError("Invalid email.");
                return;
            }

            AppUtil.closeKeyboard(requireActivity());

            progressDialog.setTitle("Please wait...");
            progressDialog.setMessage("while we send you an email with a code.");
            progressDialog.setCancelable(false);
            progressDialog.show();

            mEmail = binding.emailAddress.getText().toString().trim();

            viewModel.sendForgotCodeEmail(mEmail, this);

        });

    }

    private void initViews() {

        progressDialog = new ProgressDialog(requireContext());

        // showing gif
        Glide.with(binding.image)
                .asGif()
                .load(R.raw.email_sending_anim)
                .placeholder(R.drawable.forgot_pin_email_img)
                .into(binding.image);

    }

    @Override
    public void onEmailSentSuccess(CgForgotPwdResult result) {
        Bundle bundle = new Bundle();

        bundle.putString(FORGOT_EMAIL, mEmail);
        bundle.putString(CG_USER_XID, result.principal_xid);

        progressDialog.dismiss();

        Toast.makeText(requireContext(), "OTP sent successfully.", Toast.LENGTH_SHORT).show();

        Navigation.findNavController(binding.getRoot())
                .navigate(R.id.action_cgForgotPasswordFragment_to_cgCheckEmailFragment, bundle);

    }

    @Override
    public void onEmailSentFailed(Throwable t, String message) {
        progressDialog.dismiss();

        Toast.makeText(requireContext(), "" + message, Toast.LENGTH_SHORT).show();
    }
}
