package com.example.java.sample;

import android.app.Dialog;
import android.content.DialogInterface;
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 IndicatorDialogFragment extends DialogFragment {

    private  Function mFunction;
    public IndicatorDialogFragment(Function function){
        mFunction = function ;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // Create and return a dialog with custom layout for indicator
        AlertDialog alertDialog =  new MaterialAlertDialogBuilder(requireActivity())
                .setView(R.layout.fragment_indicator_dailog)
                .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;
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        super.onCancel(dialog);
        // Handle actions when dialog is cancelled
        // For example, if you want to retrieve paired devices when searching for Bluetooth printers on an Android device,
        // you can call "cancelDiscoveryPrinter()" to immediately get them.
        mFunction.getPrinterManager().cancelDiscoveryPrinter();

    }
}