package it.integry.integrywmsnative.view.dialogs.yes_no;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogYesNoBinding;
import it.integry.integrywmsnative.view.dialogs.DialogConsts;

public class DialogYesNoView extends DialogFragment {

    private final String title;
    private final String description;
    private final RunnableArgs<DialogConsts.Results> onDialogDismiss;

    public DialogYesNoView(final String title, final String description, RunnableArgs<DialogConsts.Results> onDialogDismiss) {
        super();

        this.title = title;
        this.description = description;
        this.onDialogDismiss = onDialogDismiss;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        var binding = DialogYesNoBinding.inflate(LayoutInflater.from(requireContext()));
        binding.executePendingBindings();

        if(UtilityString.isNullOrEmpty(title)) {
            binding.titleText.setVisibility(View.GONE);
            binding.descriptionText.setTextAppearance(requireContext(), R.style.TextAppearance_Material3_BodyLarge);
        } else {
            binding.titleText.setText(title);
            binding.titleText.setVisibility(View.VISIBLE);
            binding.descriptionText.setTextAppearance(requireContext(), R.style.TextAppearance_Material3_BodyMedium);
        }

        binding.descriptionText.setText(description);


        var alertDialog = new MaterialAlertDialogBuilder(requireContext())
                .setView(binding.getRoot())
                .setCancelable(false)
                .setPositiveButton("Ok", (dialog, which) -> {
                    if (onDialogDismiss != null) onDialogDismiss.run(DialogConsts.Results.YES);
                })
                .setNegativeButton("Annulla", (dialog, which) -> {
                    if (onDialogDismiss != null) onDialogDismiss.run(DialogConsts.Results.NO);
                })
                .create();

        alertDialog.setCanceledOnTouchOutside(false);
        return alertDialog;
    }
}
