package it.integry.integrywmsnative.core.rest.consumers;

import com.google.gson.reflect.TypeToken;

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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbTCol;
import it.integry.integrywmsnative.core.utility.UtilityDB;

@Singleton
public class MagazzinoRESTConsumer extends _BaseRESTConsumer {



    public void getTipoCollo(String codTcol, RunnableArgs<MtbTCol> onComplete, RunnableArgs<Exception> onFailed) {
        String baseSql = "SELECT * " +
                "FROM mtb_tcol " +
                "WHERE cod_tcol = " + UtilityDB.valueToString(codTcol);

        Type typeOfObjectsList = new TypeToken<ArrayList<MtbTCol>>() {}.getType();
        SystemRESTConsumer.processSqlStatic(baseSql, typeOfObjectsList, new ISimpleOperationCallback<List<MtbTCol>>() {
            @Override
            public void onSuccess(List<MtbTCol> values) {
                if(onComplete != null) {
                    MtbTCol value = values != null && values.size() > 0 ? values.get(0) : null;
                    onComplete.run(value);
                }
            }

            @Override
            public void onFailed(Exception ex) {
                if(onFailed != null) onFailed.run(ex);
            }
        });
    }

    public void getTipiCollo(RunnableArgs<List<MtbTCol>> onComplete, RunnableArgs<Exception> onFailed) {
        getTipiColloStatic(onComplete, onFailed);
    }

    public static void getTipiColloStatic(RunnableArgs<List<MtbTCol>> onComplete, RunnableArgs<Exception> onFailed) {
        String baseSql = "SELECT * " +
                "FROM mtb_tcol ";

        Type typeOfObjectsList = new TypeToken<ArrayList<MtbTCol>>() {}.getType();
        SystemRESTConsumer.processSqlStatic(baseSql, typeOfObjectsList, new ISimpleOperationCallback<List<MtbTCol>>() {
            @Override
            public void onSuccess(List<MtbTCol> values) {
                if(onComplete != null) {
                    onComplete.run(values);
                }
            }

            @Override
            public void onFailed(Exception ex) {
                if(onFailed != null) onFailed.run(ex);
            }
        });
    }
}
