package it.integry.integrywmsnative.core.class_router;

import android.content.Context;
import android.util.Pair;

import java.util.ArrayList;
import java.util.List;

public class ClassRouter {

    public enum PATH {
        FILTRO_ORDINI_VENDITA,
        ORDINI_VENDITA,
        CUSTOM_CONFIGURATION,
        BARCODE_CUSTOMIZATION
    }

    private static Context context;

    private static List<Pair<PATH, Object>> mRouteClasses = new ArrayList<>();

    public static void init() {
//        ClassRouter.context = context;
    }

    private static boolean checkIClassExists(PATH path) {
        boolean methodAlreadyDeclared = false;

        for(int i = 0; i < mRouteClasses.size() && !methodAlreadyDeclared; i++) {
            if(mRouteClasses.get(i).first == path) methodAlreadyDeclared = true;
        }

        return methodAlreadyDeclared;
    }


    private static int getClassIndex(PATH path) {
        for(int i = 0; i < mRouteClasses.size(); i++) {
            if(mRouteClasses.get(i).first == path) return i;
        }

        return -1;
    }

    public static void registerPath(PATH path, Class clazz) {
        if(checkIClassExists(path)) {
            //throw new MethodPathAlreadyDeclaredException(path);
        }

        try {
            mRouteClasses.add(new Pair<>(path, clazz.newInstance()));
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
    }

    public static void deregisterPath(PATH path) {
        if(checkIClassExists(path)) {
            mRouteClasses.remove(getClassIndex(path));
        }
    }


    public static <T> T getInstance(PATH path) {
        try {

            if (!checkIClassExists(path)) {
//                throw new MethodPathNotRegisteredException(path);
            }

            Object instance = mRouteClasses.get(getClassIndex(path)).second;

            return (T)instance;

        } catch (Exception ex) {
//             UtilityExceptions.defaultException(null, ex, true);
        }

        return null;
    }



}
