package com.example.java.sample;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;

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

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

public class MessageDialogFragment extends DialogFragment {

    private String mMessage = "";

    // Constructor to set the message
    public  MessageDialogFragment(String message){
        mMessage = message;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // Create and return a dialog with the specified message
        AlertDialog alertDialog =  new MaterialAlertDialogBuilder(requireActivity())
                .setTitle(R.string.app_name)
                .setMessage(mMessage)
                .setPositiveButton(R.string.button_name_ok, (dialog, id) -> {
                    // Dismiss the dialog on positive button click
                    dismiss();
                })
                .create();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int dialogWidth = displayMetrics.widthPixels;
        int dialogHeight = displayMetrics.heightPixels;
        WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
        layoutParams.width = (Math.min(dialogWidth, dialogHeight));
        alertDialog.getWindow().setAttributes(layoutParams);

        return alertDialog;
    }
}
