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

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.Inject;
import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.DtbDocr;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.rest.consumers.ArticoloRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.rest.model.DocumentoResoDTO;
import it.integry.integrywmsnative.core.utility.UtilityQuery;

@Singleton
public class PickingResiRESTConsumer {


    private final SystemRESTConsumer mSystemRestConsumer;
    private final ArticoloRESTConsumer mArticoloRESTConsumer;

    @Inject
    public PickingResiRESTConsumer(SystemRESTConsumer systemRESTConsumer, ArticoloRESTConsumer articoloRESTConsumer) {
        this.mSystemRestConsumer = systemRESTConsumer;
        this.mArticoloRESTConsumer = articoloRESTConsumer;
    }

    public void loadDocRows(List<DocumentoResoDTO> documents, RunnableArgs<ArrayList<WithdrawableDtbDocr>> onComplete, RunnableArgs<Exception> onFailed) {
        List<HashMap<String, Object>> filterCond = new ArrayList<>();

        for(int i = 0; i < documents.size(); i++) {
            HashMap<String, Object> filter = new HashMap<>();

            filter.put("data_doc", documents.get(i).getDataDocD());
            filter.put("num_doc", documents.get(i).getNumDoc());
            filter.put("ser_doc", documents.get(i).getSerDoc());
            filter.put("cod_anag", documents.get(i).getCodAnag());
            filter.put("cod_dtip", documents.get(i).getCodDtip());

            filterCond.add(filter);
        }

        String sql = "SELECT * FROM dvw_situazione_qta_docs " +
                "WHERE " +
                UtilityQuery.concatFieldListInWhereCond(filterCond) +
                "ORDER BY cod_mart";


        Type typeOfObjectsList = new TypeToken<ArrayList<WithdrawableDtbDocr>>() {}.getType();
        this.mSystemRestConsumer.<ArrayList<WithdrawableDtbDocr>>processSql(sql, typeOfObjectsList, values -> {

                if(values != null && values.size() > 0){
                    List<String> codMarts = Stream.of(values)
                            .map(DtbDocr::getCodMart)
                            .withoutNulls()
                            .distinct()
                            .toList();

                    mArticoloRESTConsumer.getByCodMarts(codMarts, arts -> {

                        if(arts != null && arts.size() > 0) {
                            for (DtbDocr value : values) {

                                MtbAart foundMtbAart = null;

                                List<MtbAart> mtbAartStream = Stream.of(arts)
                                        .filter(x -> x.getCodMart().equalsIgnoreCase(value.getCodMart())).toList();

                                if(mtbAartStream != null && mtbAartStream.size() > 0){
                                    foundMtbAart = mtbAartStream.get(0);
                                }

                                value.setMtbAart(foundMtbAart);
                            }


                            if(onComplete != null) onComplete.run(values);
                        }

                    }, onFailed);

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

}
