package it.integry.integrywmsnative.core.expansion;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

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

import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityProgress;

public class BaseDialogFragment extends DialogFragment {

    private boolean mBarcodeListener = false;
    private Dialog mCurrentProgress;

    public BaseDialogFragment() {
        super();
    }


    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commit();
        } catch (IllegalStateException e) {
            Log.d("ABSDIALOGFRAG", "Exception", e);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (BarcodeManager.isEnabled() && BarcodeManager.isKeyboardEmulator()) {
            getDialog().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;
    }




    public void onLoadingStarted() {
        BarcodeManager.disable();
        this.openProgress();
    }

    public void onLoadingEnded() {
        this.closeProgress();
        BarcodeManager.enable();
    }

    protected void openProgress() {

//        new Thread(() -> {
        requireActivity().runOnUiThread(() -> {
            if (this.mCurrentProgress == null) {
                this.mCurrentProgress = UtilityProgress.createDefaultProgressDialog(getActivity());
            }
        });
//        }).start();
    }

    protected void closeProgress() {
//        new Thread(() -> {
        requireActivity().runOnUiThread(() -> {
            if (mCurrentProgress != null) {
                mCurrentProgress.dismiss();
                mCurrentProgress = null;
            }
        });
//        }).start();
    }


    public void onError(Exception ex) {
        requireActivity().runOnUiThread(() -> {
            this.closeProgress();
            UtilityExceptions.defaultException(requireActivity(), ex);
        });
    }
}
