package it.integry.integrywmsnative.core.expansion;

import android.content.DialogInterface;
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 javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.view.dialogs.DialogProgressView;

public class BaseDialogFragment extends DialogFragment implements DialogInterface.OnShowListener {

    @Inject
    public DialogProgressView mCurrentProgress;
    private boolean progressOpened;

    private boolean mBarcodeListener = false;

    private boolean cancelable = true;


    @Override
    public boolean isCancelable() {
        return cancelable;
    }


    @Override
    public void setCancelable(boolean cancelable) {
        super.setCancelable(cancelable);
        this.cancelable = cancelable;
    }

    @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);
        this.initialize();
    }

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

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

    private void openProgress() {
        if (!progressOpened && !this.mCurrentProgress.isAdded() && !this.mCurrentProgress.isInLayout()) {
            this.progressOpened = true;
            requireActivity().runOnUiThread(() -> {
                this.mCurrentProgress.show(requireActivity().getSupportFragmentManager(), "tag");
            });
        }
    }

    private void closeProgress() {
        if (progressOpened) {
            this.progressOpened = false;
            requireActivity().runOnUiThread(() -> {
                mCurrentProgress.dismiss();
            });
        }
    }


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

    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;
    }


    @Override
    public void onShow(DialogInterface dialogInterface) {
        this.initialize();
    }

    private void initialize() {
        MainApplication.appComponent
                .baseDialogFragmentComponent()
                .create()
                .inject(this);

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


}
