package it.integry.integrywmsnative.core.expansion;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.pointmobilescannerlibrary.PointMobileBarcodeReader;

public class BaseDialog extends Dialog {

    private boolean mBarcodeListener = false;

    public BaseDialog(@NonNull Context context) {
        super(context);
        if (BarcodeManager.isEnabled() && BarcodeManager.isKeyboardEmulator()) {
            setOnKeyListener((dialog, keyCode, event) -> {
                if (mBarcodeListener && (event.getAction() == KeyEvent.ACTION_DOWN || event.getAction() == KeyEvent.ACTION_MULTIPLE) && !isControlKey(event)) {
                    return BarcodeManager.onKeyDown(event.getKeyCode(), event);
                }
                return false;
            });
        }
    }

    private boolean isControlKey(KeyEvent keyEvent) {
        int keyCode = keyEvent.getKeyCode();
        return (
                keyCode == KeyEvent.KEYCODE_BACK ||
                        keyCode == KeyEvent.KEYCODE_SHIFT_LEFT ||
                        keyCode == KeyEvent.KEYCODE_DEL
        );
    }

    public void setBarcodeListener(boolean listen) {
        this.mBarcodeListener = listen;
    }
}
