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

import com.annimon.stream.Stream;
import com.google.gson.reflect.TypeToken;

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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.JtbComt;
import it.integry.integrywmsnative.core.utility.UtilityQuery;

@Singleton
public class CommessaRESTConsumer extends _BaseRESTConsumer {

    private final SystemRESTConsumer systemRESTConsumer;

    public CommessaRESTConsumer(SystemRESTConsumer systemRESTConsumer) {
        this.systemRESTConsumer = systemRESTConsumer;
    }


    public void getJtbComts(List<String> itemsToFind, RunnableArgs<List<JtbComt>> onComplete, RunnableArgs<Exception> onFailed) {
        var whereCondMap = Stream.of(itemsToFind)
                .map(x -> {
                    HashMap<String, Object> vars = new HashMap<>();
                    vars.put("cod_jcom", x);
                    return vars;
                })
                .toList();

        var whereCond = " WHERE " + UtilityQuery.concatFieldListInWhereCond(whereCondMap);

        Type typeOfObjectsList = new TypeToken<ArrayList<JtbComt>>() {}.getType();
        this.systemRESTConsumer.processSql("SELECT * FROM jtb_comt " + whereCond, typeOfObjectsList, onComplete, onFailed);

    }

}
