package it.integry.integrywmsnative.gest.ultimi_arrivi_fornitore.rest;

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

import org.apache.commons.lang3.StringUtils;

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.GtbAnag;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.rest.model.DocumentoResoDTO;
import it.integry.integrywmsnative.core.utility.UtilityDB;

@Singleton
public class UltimiArriviFornitoreRESTConsumer {

    private final SystemRESTConsumer systemRESTConsumer;

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

    public void getAvailableCodAnagFornitori(RunnableArgs<List<GtbAnag>> onComplete, RunnableArgs<Exception> onFailed) {
        String sql = "SELECT DISTINCT gtb_anag.cod_anag, gtb_anag.rag_soc " +
                "FROM gtb_anag " +
                "INNER JOIN atb_forn ON atb_forn.cod_anag = gtb_anag.cod_anag " +
                "WHERE flag_stato = 'A'";

        Type typeOfObjectsList = new TypeToken<ArrayList<GtbAnag>>() {
        }.getType();
        this.systemRESTConsumer.<ArrayList<GtbAnag>>processSql(sql, typeOfObjectsList, value -> {
            if (onComplete != null) onComplete.run(value);
        }, ex -> {
            if (onFailed != null) onFailed.run(ex);
        });
    }

    public void getUltimeConsegneFornitori(String codMdep, String codAnag, List<MtbAart> mtbAarts, int limitConsegnePerCli, int limitDays, RunnableArgs<ArrayList<DocumentoResoDTO>> onComplete, RunnableArgs<Exception> onFailed) {
        String codMarts = null;

        if (mtbAarts != null && mtbAarts.size() > 0) {
            codMarts = StringUtils.join(Stream.of(mtbAarts)
                    .map(MtbAart::getCodMart)
                    .toList(), "|");
        }

        String sql = "SELECT consegne.*, " +
                "       rag_soc " +
                "FROM WMS_GetUltimeConsegneFornitori(" +
                UtilityDB.valueToString(limitConsegnePerCli >= 0 ? limitConsegnePerCli : null) + ", " +
                UtilityDB.valueToString(codAnag) + ", " +
                UtilityDB.valueToString(codMarts) + ", " +
                UtilityDB.valueToString(codMdep) + ", " +
                UtilityDB.valueToString(limitDays) + ") consegne " +
                "LEFT OUTER JOIN gtb_anag ON consegne.cod_anag = gtb_anag.cod_anag " +
                "ORDER BY rag_soc, " +
                "         counter_consegna ";


        Type typeOfObjectsList = new TypeToken<ArrayList<DocumentoResoDTO>>() {
        }.getType();
        this.systemRESTConsumer.<ArrayList<DocumentoResoDTO>>processSql(sql, typeOfObjectsList, value -> {
            if (onComplete != null) onComplete.run(value);
        }, ex -> {
            if (onFailed != null) onFailed.run(ex);
        });
    }


}
