package it.integry.integrywmsnative.core.settings;

import android.content.Context;

import com.google.firebase.perf.metrics.Trace;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Singleton;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.CommonConst;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.dto.InternalCodAnagsDTO;
import it.integry.integrywmsnative.core.rest.consumers.AziendaRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers.GestSetupRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers.PosizioniRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.rest.model.AvailableCodMdepsDTO;
import it.integry.integrywmsnative.core.utility.UtilityFirebase;
import it.integry.integrywmsnative.core.utility.UtilityLogger;

@Singleton
public class SettingsManager {

    private static String TAG = "SETTINGS";

    private static SettingsModel settingsModelIstance;
    private static DBSettingsModel dbSettingsModelIstance;

    private static boolean firstStart = false;

    private static Context mContext;
    private static SystemRESTConsumer mSystemRESTConsumer;
    private static AziendaRESTConsumer mAziendaRESTConsumer;
    private static GestSetupRESTConsumer mGestSetupRESTConsumer;
    private static PosizioniRESTConsumer mPosizioniRESTConsumer;

    public SettingsManager(Context context,
                           SystemRESTConsumer systemRESTConsumer,
                           AziendaRESTConsumer aziendaRESTConsumer,
                           GestSetupRESTConsumer gestSetupRESTConsumer,
                           PosizioniRESTConsumer posizioniRESTConsumer) {
        mContext = context;
        mSystemRESTConsumer = systemRESTConsumer;
        mAziendaRESTConsumer = aziendaRESTConsumer;
        mGestSetupRESTConsumer = gestSetupRESTConsumer;
        mPosizioniRESTConsumer = posizioniRESTConsumer;
    }

    public void init() {
        settingsModelIstance = (SettingsModel) Stash.getObject(TAG, SettingsModel.class);

        if (settingsModelIstance == null) {
            settingsModelIstance = new SettingsModel();

            settingsModelIstance.setServer(new SettingsModel.Server());
            firstStart = true;
        }

        dbSettingsModelIstance = new DBSettingsModel();

    }

    public SettingsModel getSettings() {
        return settingsModelIstance;
    }


    public static SettingsModel i() {
        return settingsModelIstance;
    }

    public static DBSettingsModel iDB() {
        return dbSettingsModelIstance;
    }

    public static boolean isFirstStart() {
        return firstStart;
    }

    public static boolean isInstanceAvailable() {
        return i() != null;
    }

    public static void update() {
        Stash.put(TAG, settingsModelIstance);
    }


    public static void loadDBVariables(RunnableArgs<String> onProgress, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        dbSettingsModelIstance = new DBSettingsModel();

        Trace perfTrace = UtilityFirebase.getNewPerformanceTrace("db_load_vars");
        perfTrace.start();

        Runnable tmpOnComplete = () -> {
            perfTrace.stop();
            onComplete.run();
        };

        RunnableArgs<Exception> tmpOnFailed = ex -> {
            UtilityLogger.error(ex);
            perfTrace.putAttribute("failed", "true");
            if (!(ex instanceof SocketTimeoutException)) onFailed.run(ex);
            else
                onFailed.run(new Exception("Errore durante il caricamento dei dati. Riavviare l'applicazione!"));
        };


        onProgress.run("depositi");
        loadAvailableCodMdeps(() -> {

            onProgress.run("posizioni");
            loadAvailablePosizioni(() -> {

                onProgress.run("impostazioni");
                loadGestSetupValues(() -> {

                    onProgress.run("dati azienda");
                    loadDatiAzienda(tmpOnComplete, tmpOnFailed);
                }, tmpOnFailed);
            }, tmpOnFailed);
        }, tmpOnFailed);
    }

    private static void loadDatiAzienda(Runnable onComplete, RunnableArgs<Exception> onFailed) {

        mAziendaRESTConsumer.retrieveAzienda(datiAzienda -> {
            SettingsManager.iDB().setDatiAzienda(datiAzienda);

            if (!SettingsManager.iDB().isFlagUseCodAnagAziendale()) {
                onComplete.run();
                return;
            }

            String internalCodAnagsQuery = "SELECT ga.cod_anag,\n" +
                    "       CAST(CASE WHEN vc.cod_anag IS NOT NULL THEN 1 ELSE 0 END AS BIT) AS is_cliente,\n" +
                    "       CAST(CASE WHEN af.cod_anag IS NOT NULL THEN 1 ELSE 0 END AS BIT) AS is_fornitore\n" +
                    "FROM azienda\n" +
                    "INNER JOIN gtb_anag ga on azienda.part_iva = ga.part_iva\n" +
                    "LEFT OUTER JOIN vtb_clie vc on ga.cod_anag = vc.cod_anag AND vc.flag_stato = 'A'\n" +
                    "LEFT OUTER JOIN atb_forn af on ga.cod_anag = af.cod_anag AND af.flag_stato = 'A'";

            Type type = new TypeToken<List<InternalCodAnagsDTO>>() {
            }.getType();
            mSystemRESTConsumer.<List<InternalCodAnagsDTO>>processSql(internalCodAnagsQuery, type, internalCodAnagsList -> {
                SettingsManager.iDB().setInternalCodAnags(internalCodAnagsList);

                if (onComplete != null) onComplete.run();
            }, onFailed);

        }, onFailed);
    }

    private static void loadAvailableCodMdeps(Runnable onComplete, RunnableArgs<Exception> onFailed) {
        mSystemRESTConsumer.getAvailableCodMdeps(availableCodMdeps -> {
            dbSettingsModelIstance.setAvailableCodMdep(availableCodMdeps);

            if (availableCodMdeps == null || availableCodMdeps.size() == 0) {
                onFailed.run(new Exception(mContext.getText(R.string.no_codmdep_available).toString()));
                return;
            }

            boolean codMdepExistsAnymore = false;

            if (settingsModelIstance.getUserSession().getDepo() != null) {
                for (AvailableCodMdepsDTO availableCodMdepDTO : availableCodMdeps) {
                    //Controllo se il codMdep salvato esiste ancora
                    if (availableCodMdepDTO.getCodMdep().equalsIgnoreCase(settingsModelIstance.getUserSession().getDepo().getCodMdep())) {
                        codMdepExistsAnymore = true;
                        break;
                    }
                }
            }

            if (!codMdepExistsAnymore) {
                settingsModelIstance.getUserSession().setDepo(availableCodMdeps.get(0));
            }

            if (onComplete != null) onComplete.run();
        }, ex -> {
            //BOH
            if (onFailed != null) onFailed.run(ex);
        });
    }


    private static void loadAvailablePosizioni(Runnable onComplete, RunnableArgs<Exception> onFailed) {
        mPosizioniRESTConsumer.getAvailablePosizioni(availablePosizioni -> {
                    dbSettingsModelIstance.setAvailablePosizioni(availablePosizioni);

                    if (onComplete != null) onComplete.run();
                }, ex -> {
                    if (onFailed != null) onFailed.run(ex);
                }
        );
    }


    private static void loadGestSetupValues(Runnable onComplete, RunnableArgs<Exception> onFailed) {

        List<StbGestSetupReader> stbGestSetupReaderList = new ArrayList<>();

        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("ENABLE_CHECK_PARTITA_MAG_PICKING_V")
                .setSetter(dbSettingsModelIstance::setEnableCheckPartitaMagCheckPickingV));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("FLAG_MULTI_CLIENTE_ORD_VENDITA")
                .setSetter(dbSettingsModelIstance::setFlagMultiClienteOrdV));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("FLAG_USE_COD_ANAG_AZIENDALE")
                .setSetter(dbSettingsModelIstance::setFlagUseCodAnagAziendale));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("PICKING_LIBERO")
                .setKeySection("FLAG_ASK_CLIENTE")
                .setSetter(dbSettingsModelIstance::setFlagAskClienteInPickingLibero));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("PICKING_LIBERO")
                .setKeySection("FLAG_ALLOW_EMPTY_CLIENTE")
                .setSetter(dbSettingsModelIstance::setFlagPickLiberoAllowEmptyCliente));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_CAN_ADD_EXTRA_ITEMS")
                .setSetter(dbSettingsModelIstance::setFlagCanAddExtraItemSpedizione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE")
                .setKeySection("FLAG_AUTO_OPEN_NEW_UL")
                .setSetter(dbSettingsModelIstance::setFlagCanAutoOpenNewULAccettazione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_CAN_ADD_EXTRA_QUANTITY")
                .setSetter(dbSettingsModelIstance::setFlagCanAddExtraQuantitySpedizione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("ENABLE_CHECK_DEPOSITO")
                .setSetter(dbSettingsModelIstance::setFlagEnableCheckDepositoSpedizione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("FLAG_FORCE_ALL_TO_COLLI")
                .setSetter(dbSettingsModelIstance::setFlagForceAllToColli));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("FLAG_ASK_PESO_COLLO")
                .setSetter(dbSettingsModelIstance::setFlagAskPesoColloSpedizione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("PRODUZIONE")
                .setKeySection("FLAG_VERSAMENTO_DIRETTO")
                .setSetter(dbSettingsModelIstance::setFlagVersamentoDirettoProduzione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(String.class)
                .setGestName("PICKING")
                .setSection("PRODUZIONE")
                .setKeySection("FILTER_FORNTIORE_PROD")
                .setSetter(dbSettingsModelIstance::setFilterFornitoreProd));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("PRODUZIONE")
                .setKeySection("FLAG_GROUP_BY_GRP_MERC")
                .setSetter(dbSettingsModelIstance::setGroupPoductionByCommodityGroup)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("ENABLE_MANUAL_PICK")
                .setSetter(dbSettingsModelIstance::setFlagSpedizioneEnableManualPick));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("ENABLE_FAKE_GIACENZA")
                .setSetter(dbSettingsModelIstance::setFlagSpedizioneEnableFakeGiacenza));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_CAN_SELECT_MULTIPLE_ORDERS")
                .setSetter(dbSettingsModelIstance::setFlagSpedizioneCanSelectMultipleOrders));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_USE_QTA_ORD")
                .setSetter(dbSettingsModelIstance::setFlagSpedizioneUseQtaOrd));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Integer.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("ON_NUM_CNF_INPUT_CHANGED")
                .setSetter(data -> {
                    if (data != null) dbSettingsModelIstance.setOnNumCnfInputChanged(data);
                }));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Integer.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("ON_QTA_TOT_INPUT_CHANGED")
                .setSetter(data -> {
                    if (data != null) dbSettingsModelIstance.setOnQtaTotInputChanged(data);
                }));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(String.class)
                .setGestName("PVM")
                .setSection("DOC_INTERNI")
                .setKeySection("NOTE_PERDITA")
                .setSetter(data -> {
                    if (data != null)
                        dbSettingsModelIstance.setNotePerditaDocInterni(Arrays.asList(data.split("\\|")));
                }));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PVM")
                .setSection("ORDINI_A")
                .setKeySection("ORDINA_NUOVI_ARTICOLI")
                .setSetter(dbSettingsModelIstance::setFlagOrdinaNuoviArticoliInGriglia));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PVM")
                .setSection("ORDINI_A")
                .setKeySection("ORDINA_ARTICOLI_ON_SCAN")
                .setSetter(dbSettingsModelIstance::setFlagOrdinaArticoliOnScan));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PVM")
                .setSection("ORDINI_A")
                .setKeySection("FLAG_CONSENTI_ORDINE_DA_GRIGLIA")
                .setSetter(dbSettingsModelIstance::setFlagConsentiFuoriPianoLogistico));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PVM")
                .setSection("ORDINI_A")
                .setKeySection("FLAG_CONSENTI_ORDINE_SENZA_GRIGLIA")
                .setSetter(dbSettingsModelIstance::setFlagConsentiOrdineSenzaGriglia));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE")
                .setKeySection("FLAG_USE_QTA_ORD")
                .setSetter(dbSettingsModelIstance::setFlagAccettazioneUseQtaOrd));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("PICKING_LIBERO")
                .setKeySection("FLAG_ALLOW_EMPTY_DEST")
                .setSetter(dbSettingsModelIstance::setFlagPickLiberoAllowEmptyDest));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_PRINT_PACKING_LIST_ON_CLOSE")
                .setSetter(dbSettingsModelIstance::setFlagPrintPackingListOnOrderClose));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_PRINT_ETICHETTE_ON_CLOSE")
                .setSetter(dbSettingsModelIstance::setFlagPrintEtichetteOnOrderClose));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_PRINT_ETICHETTE_ON_LU_CLOSE")
                .setSetter(dbSettingsModelIstance::setFlagPrintEtichetteOnLUClose)
                .setDefaultValue(true));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_ASK_INFO_AGGIUNTIVE")
                .setSetter(dbSettingsModelIstance::setFlagAskInfoAggiuntiveSpedizione));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(String.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("COD_DTIP_ORD_TRASF")
                .setSetter(dbSettingsModelIstance::setCodDtipOrdTrasfV));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_NOTIFICA_STATO_PARTITA")
                .setSetter(dbSettingsModelIstance::setNotifyLotStatus));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_SHOW_COD_FORN")
                .setSetter(dbSettingsModelIstance::setShowCodFornSpedizione)
                .setDefaultValue(true));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_GROUP_BY_GRP_MERC")
                .setSetter(dbSettingsModelIstance::setGroupShippingByCommodityGroup)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("INVENTARIO")
                .setKeySection("FLAG_CAN_CREATE_INVENTARIO")
                .setSetter(dbSettingsModelIstance::setFlagCanCreateInventario)
                .setDefaultValue(true));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("INVENTARIO")
                .setKeySection("FLAG_CAN_ADD_UNKNOWN_ITEMS")
                .setSetter(dbSettingsModelIstance::setFlagCanAddUnknownItemsInventario)
                .setDefaultValue(true));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("INVENTARIO")
                .setKeySection("FLAG_SHOULD_ASK_TO_CREATE_OR_UPDATE_ROW")
                .setSetter(dbSettingsModelIstance::setFlagShouldAskToCreateOrUpdateRowInventario)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("INVENTARIO")
                .setKeySection("ENABLE_OFFLINE_CACHE")
                .setSetter(dbSettingsModelIstance::setFlagIsInventarioCacheEnabled)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(String.class)
                .setGestName("PICKING")
                .setSection("SETUP")
                .setKeySection("COMMESSA_MAGAZZINO")
                .setSetter(dbSettingsModelIstance::setCommessaMagazzino)
                .setDefaultValue(CommonConst.Config.COMMESSA_MAG));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("GENERA_DOC")
                .setSetter(dbSettingsModelIstance::setFlagGeneraDocumentoSpedizione)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_ASK_PRINT_UL")
                .setSetter(dbSettingsModelIstance::setFlagAskPrintUlAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_ASK_TIPO_COLLO")
                .setSetter(dbSettingsModelIstance::setFlagAskTipoColloAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_ASK_VERSAMENTO_AUTOMATICO")
                .setSetter(dbSettingsModelIstance::setFlagAskVersamentoAutomaticoAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_CAN_ADD_UNKNOWN_ITEMS")
                .setSetter(dbSettingsModelIstance::setFlagCanAddUnknownItemsAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_CAN_ADD_UNKNOWN_BARCODES")
                .setSetter(dbSettingsModelIstance::setFlagCanAddUnknownBarcodesAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_ENABLE_FAST_PICK")
                .setSetter(dbSettingsModelIstance::setFlagEnableFastPickAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_DISABLE_MANDATORY_TRACCIABILITA")
                .setSetter(dbSettingsModelIstance::setFlagDisableMandatoryTracciabilitaAccettazioneBolla)
                .setDefaultValue(false));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("ACCETTAZIONE_BOLLA")
                .setKeySection("FLAG_ALLOW_UL_RECOVER")
                .setSetter(dbSettingsModelIstance::setFlagAllowULRecoverAccettazioneBolla)
                .setDefaultValue(true));
        stbGestSetupReaderList.add(new StbGestSetupReader<>(Boolean.class)
                .setGestName("PICKING")
                .setSection("SPEDIZIONE")
                .setKeySection("FLAG_OVERFLOW_QUANTITY_WARNING")
                .setSetter(dbSettingsModelIstance::setFlagOverflowQuantityWarning)
                .setDefaultValue(false));

        String codMdep = SettingsManager.i().getUserSession().getDepo().getCodMdep();


        mGestSetupRESTConsumer.getValues(codMdep, stbGestSetupReaderList, list -> {

            for (var stbGestSetupReader : list) {
                var value = stbGestSetupReader.getValue();
                var clazz = stbGestSetupReader.getClazz();

                Object finalValue = null;
                if (value != null) {
                    if (clazz == String.class) {
                        finalValue = clazz.cast(value);
                    } else if (clazz == Boolean.class) {
                        finalValue = clazz.cast("S".equalsIgnoreCase(value));
                    } else if (clazz == Integer.class && value != null) {
                        finalValue = clazz.cast(Integer.parseInt(value));
                    }
                } else if (stbGestSetupReader.getDefaultValue() == null) {
                    if (clazz == Boolean.class) {
                        finalValue = clazz.cast(Boolean.FALSE);
                    } else if (clazz == Integer.class) {
                        finalValue = clazz.cast(0);
                    }
                } else finalValue = stbGestSetupReader.getDefaultValue();

                stbGestSetupReader.getSetter().run(finalValue);
            }

            if (onComplete != null) onComplete.run();
        }, onFailed);

    }

}
