package it.integry.integrywmsnative.core.barcode_reader;

import android.content.Context;
import android.view.KeyEvent;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import it.integry.barcode_base_android_library.interfaces.BarcodeReaderInterface;
import it.integry.honeywellscannerlibrary.HoneyWellBarcodeReader;
import it.integry.keyobardemulatorscannerlibrary.KeyboardEmulatorBarcodeReader;
import it.integry.pointmobilescannerlibrary.PointMobileBarcodeReader;
import it.integry.zebrascannerlibrary.ZebraBarcodeReader;

public class BarcodeManager {

    private static BarcodeReaderInterface mCurrentBarcodeInterface;
    private static final List<BarcodeCallbackDTO> mBarcodeCallbacksStacktrace = new ArrayList<>();

    private static boolean mEnabled = true;


    private static final Class<? extends BarcodeReaderInterface>[] registeredBarcodeReaderInterfaces = new Class[]{
            PointMobileBarcodeReader.class,
            ZebraBarcodeReader.class,
            HoneyWellBarcodeReader.class,
            KeyboardEmulatorBarcodeReader.class
    };


    public static void init(Context applicationContext) throws Exception {

        initBarcodeReader(applicationContext);
    }

    private static void initBarcodeReader(Context applicationContext) throws Exception {

        for (Class<? extends BarcodeReaderInterface> readerInterface : registeredBarcodeReaderInterfaces) {

            Constructor<?> cons = Class.forName(readerInterface.getName()).getConstructors()[0];
            BarcodeReaderInterface object = (BarcodeReaderInterface) cons.newInstance(applicationContext);

            if (object.isRightAdapter()) {
                mCurrentBarcodeInterface = object;
                break;
            }


        }

        if (mCurrentBarcodeInterface != null) {

            mCurrentBarcodeInterface.init(() -> {

                mCurrentBarcodeInterface.register(data -> {
                    BarcodeCallbackDTO callback = getValidCallback();
                    if (callback != null && mEnabled) {
                        callback.getOnScanSuccessfull().run(data);
                    }
                }, ex -> {
                    BarcodeCallbackDTO callback = getValidCallback();
                    if (callback != null && mEnabled) {
                        callback.getOnScanFailed().run(ex);
                    }
                });

            });
        }
    }


    private static BarcodeCallbackDTO getValidCallback() {

        if (mBarcodeCallbacksStacktrace.size() > 0) {
            return mBarcodeCallbacksStacktrace.get(mBarcodeCallbacksStacktrace.size() - 1);
        } else {
            return null;
        }

    }

    public static boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        if (mCurrentBarcodeInterface != null)
            mCurrentBarcodeInterface.onKeyEvent(keyEvent);
        return true;
    }

    public static BarcodeReaderInterface getCurrentBarcodeInterface() {
        return mCurrentBarcodeInterface;
    }

    public static int addCallback(BarcodeCallbackDTO barcodeCallbackDTO) {

        int newID = -1;

        if (mBarcodeCallbacksStacktrace.size() > 0) {
            newID = mBarcodeCallbacksStacktrace.get(mBarcodeCallbacksStacktrace.size() - 1).getID() + 1;
        } else {
            newID = 1;
        }

        barcodeCallbackDTO.setID(newID);
        mBarcodeCallbacksStacktrace.add(barcodeCallbackDTO);

        return newID;
    }

    public static void removeCallback(int ID) {
        boolean callbackObjFound = false;

        //Rimuovo la callback con l'ID trovato e tutte quelle con >ID in modo che rimuovo tutte le call aggiunte successivamente

        for (int i = 0; i < mBarcodeCallbacksStacktrace.size(); i++) {
            if (mBarcodeCallbacksStacktrace.get(i).getID() == ID || callbackObjFound) {
                mBarcodeCallbacksStacktrace.remove(i);

                callbackObjFound = true;
            }
        }
    }


    public static void disable() {
        mEnabled = false;
    }

    public static void enable() {
        mEnabled = true;
    }

    public static boolean isEnabled() {
        return mEnabled;
    }


    public static boolean isKeyboardEmulator() {
        return mCurrentBarcodeInterface instanceof KeyboardEmulatorBarcodeReader;
    }
}
