package epson.print;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.FragmentActivity;

public class ThreeButtonDialog extends AppCompatDialogFragment {
    private static final String PARAM_LAYOUT_RESOURCE_ID = "layout_resource_id";

    public interface DialogCallback {
        void callback(int i);
    }

    @NonNull
    public static ThreeButtonDialog newInstance(int i) {
        ThreeButtonDialog threeButtonDialog = new ThreeButtonDialog();
        Bundle bundle = new Bundle();
        bundle.putInt(PARAM_LAYOUT_RESOURCE_ID, i);
        threeButtonDialog.setArguments(bundle);
        return threeButtonDialog;
    }

    public void onCreate(@Nullable Bundle bundle) {
        super.onCreate(bundle);
        setStyle(STYLE_NO_TITLE, 0);
    }

    public Dialog onCreateDialog(Bundle bundle) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        Bundle arguments = getArguments();
        int i = arguments != null ? arguments.getInt(PARAM_LAYOUT_RESOURCE_ID) : 0;
        if (i == 0) {
            i = R.layout.dialog_three_button;
        }
        View inflate = layoutInflater.inflate(i, (ViewGroup) null);
        builder.setView(inflate);
        Button button = (Button) inflate.findViewById(R.id.button1);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ThreeButtonDialog.this.execCallbackAndDismiss(1);
                }
            });
        }
        Button button2 = (Button) inflate.findViewById(R.id.button2);
        if (button2 != null) {
            button2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ThreeButtonDialog.this.execCallbackAndDismiss(2);
                }
            });
        }
        Button button3 = (Button) inflate.findViewById(R.id.button3);
        if (button3 != null) {
            button3.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ThreeButtonDialog.this.execCallbackAndDismiss(3);
                }
            });
        }
        AlertDialog create = builder.create();
        create.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return i == 84;
            }
        });
        setCancelable(false);
        return create;
    }

    private void execCallbackAndDismiss(int i) {
        DialogCallback dialogCallback = getDialogCallback();
        if (dialogCallback != null) {
            dialogCallback.callback(i);
            dismiss();
        }
    }

    private DialogCallback getDialogCallback() {
        FragmentActivity activity = getActivity();
        if (activity == null) {
            return null;
        }
        try {
            return (DialogCallback) activity;
        } catch (ClassCastException unused) {
            throw new ClassCastException(activity.toString() + " must implement ThreeButtonDialog.DialogCallback");
        }
    }
}
