package com.app.simplitend.welcome.welcomepatient.fragments.forgotpin;

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 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.apputils.EditTextErrorRemover;
import com.app.simplitend.databinding.ForgotPinFragmentBinding;
import com.app.simplitend.welcome.welcomepatient.mvvm.WelcomeContracts;
import com.app.simplitend.welcome.welcomepatient.mvvm.WelcomeViewModel;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.OTPSentResponse;

public class ForgotPinFragment extends Fragment implements WelcomeContracts.SendOTPToEmailCallback {

    protected ForgotPinFragmentBinding binding;

    private WelcomeViewModel welcomeViewModel;

    private ProgressDialog progressDialog;

    public static final String USER_ID_KEY = "user_id_key";
    public static final String EMAIL_KEY = "email_key";

    public ForgotPinFragment(){
        // required empty constructor
    }

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

        welcomeViewModel = new ViewModelProvider(requireActivity()).get(WelcomeViewModel.class);

        initViews();

        clickEvents();

        return binding.getRoot();
    }

    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);

        new EditTextErrorRemover(
                binding.emailAddress
        );
    }

    private void clickEvents() {

        binding.backBtn.setOnClickListener(v -> {
            if (getActivity() != null){
                getActivity().onBackPressed();
            }
        });

        binding.submit.setOnClickListener(v -> {
            AppUtil.closeKeyboard(requireActivity());

            if (allOkay()){
                sendOTP();
            }

        });

    }

    private void sendOTP() {
        progressDialog.setTitle("Please wait...");
        progressDialog.setMessage("A 4-digit One Time Password will be sent to your email.");
        progressDialog.setCancelable(false);
        progressDialog.show();

        welcomeViewModel.sendOTPToEmail(binding.emailAddress.getText().toString().trim(),
                this);

    }

    private boolean allOkay() {
        boolean allOkay = true;

        if (binding.emailAddress.getText().toString().trim().isEmpty()){
            allOkay = false;
            binding.emailAddress.setError("Required");
        }else if (!Patterns.EMAIL_ADDRESS.matcher(binding.emailAddress.getText().toString().trim()).matches()){
            allOkay = false;
            binding.emailAddress.setError("Invalid email");
        }

        return allOkay;
    }

    @Override
    public void onOTPSent(OTPSentResponse otpSentResponse) {
        progressDialog.dismiss();

        Bundle bundle = new Bundle();

        bundle.putString(USER_ID_KEY, otpSentResponse.principal_xid);
        bundle.putString(EMAIL_KEY, binding.emailAddress.getText().toString().trim());

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

    }

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

        AppUtil.showAlert(requireContext(),
                getString(R.string.something_went_wrong),
                message,
                getString(R.string.ok),
                ((dialog, which) -> {
                    dialog.dismiss();
                }), null, null);
    }
}
