package it.integry.integrywmsnative.core.barcode_reader;

import android.content.Context;
import android.text.SpannableString;

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

import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;
import it.integry.plugins.barcode_base_library.exception.BarcodeAdapterNotFoundException;
import it.integry.plugins.barcode_base_library.interfaces.BarcodeReaderInterface;
import it.integry.pointmobilescannerlibrary.PointMobileBarcodeReader;

public class BarcodeManager {

    private static Context context;

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

    private static boolean mEnabled = true;


    private static Class<? extends BarcodeReaderInterface>[] registeredBarcodeReaderInterfaces = new Class[]{
            PointMobileBarcodeReader.class
    };


    public static void init(Context context) {
        BarcodeManager.context = context;

        initBarcodeReader();
    }

    private static void initBarcodeReader() {

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

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

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


            } catch (Exception e) {
                UtilityLogger.errorMe(e);
            }
        }

        if(mCurrentBarcodeInterface != null) {

            try {
                mCurrentBarcodeInterface.init();
            } catch (BarcodeAdapterNotFoundException ex) {
                UtilityExceptions.defaultException(context, ex);
            }

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



}
