package it.integry.integrywmsnative.gest.accettazione_picking.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.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import it.integry.integrywmsnative.core.utility.UtilityQuery;
import it.integry.integrywmsnative.gest.accettazione.dto.OrdineAccettazioneInevasoDTO;

@Singleton
public class AccettazionePickingRESTConsumer {

    private final SystemRESTConsumer mSystemRestConsumer;

    @Inject
    public AccettazionePickingRESTConsumer(SystemRESTConsumer systemRESTConsumer) {
        this.mSystemRestConsumer = systemRESTConsumer;
    }

    public void getBancaliGiaRegistrati(List<OrdineAccettazioneInevasoDTO> ordiniToShow, RunnableArgs<ArrayList<RecoverMtbColt>> onComplete, RunnableArgs<Exception> onFailed) {
        List<HashMap<String, Object>> whereCondListMap = new ArrayList<>();

        Stream
                .of(ordiniToShow)
                .distinctBy(x -> x.getData() + " " + x.getGestione() + " " + x.getNumero())
                .forEach(x -> {
                    try {
                        HashMap<String, Object> whereCondMap = new HashMap<>();
                        whereCondMap.put("mtb_colr.data_ord", UtilityDate.recognizeDate(x.getData()));
                        whereCondMap.put("mtb_colr.gestione", x.getGestione().equalsIgnoreCase("P") ? "L" : x.getGestione());
                        whereCondMap.put("mtb_colr.num_ord", x.getNumero());

                        whereCondListMap.add(whereCondMap);
                    } catch (Exception ex) {
                        UtilityLogger.error(ex);
                    }
                });


        String sql = "SELECT DISTINCT CAST(CASE WHEN MAX(ISNULL(mtb_colr_scar.num_collo, 0)) = 0 THEN 1 ELSE 0 END AS BIT) AS flagCanBeRecovered, " +
                "                mtb_colt.gestione," +
                "                mtb_colt.data_collo," +
                "                mtb_colt.num_collo," +
                "                segno," +
                "                peso_kg," +
                "                lunghezza_cm," +
                "                larghezza_cm," +
                "                altezza_cm," +
                "                cod_anag," +
                "                cod_dtip," +
                "                mtb_colt.data_doc," +
                "                mtb_colt.ser_doc," +
                "                mtb_colt.num_doc," +
                "                rif_ord," +
                "                mtb_colt.ser_collo," +
                "                cod_tcol," +
                "                mtb_colt.data_ord," +
                "                mtb_colt.num_ord," +
                "                cod_vdes," +
                "                cod_mdep," +
                "                cod_vlis," +
                "                preparato_da," +
                "                ora_iniz_prep," +
                "                ora_fine_prep," +
                "                filtro_ordini," +
                "                annotazioni," +
                "                posizione," +
                "                cod_dtip_provv," +
                "                data_doc_provv," +
                "                ser_doc_provv," +
                "                num_doc_provv," +
                "                cod_jfas," +
                "                data_vers," +
                "                mtb_colt.peso_netto_kg " +
                "FROM mtb_colt" +
                "   INNER JOIN  mtb_colr ON " +
                " mtb_colt.gestione = mtb_colr.gestione AND " +
                " mtb_colt.ser_collo = mtb_colr.ser_collo AND " +
                " mtb_colt.data_collo = mtb_colr.data_collo AND " +
                " mtb_colt.num_collo = mtb_colr.num_collo " +
                " LEFT OUTER JOIN mtb_colr mtb_colr_scar ON mtb_colr_scar.num_collo_rif = mtb_colr.num_collo AND " +
                "                                             mtb_colr_scar.gestione_rif = mtb_colr.gestione AND " +
                "                                             mtb_colr_scar.ser_collo_rif = mtb_colr.ser_collo AND " +
                "                                             mtb_colr_scar.data_collo_rif = mtb_colr.data_collo " +
                " WHERE (" + UtilityQuery.concatFieldListInWhereCond(whereCondListMap) + ") " +
                " AND mtb_colt.data_doc IS NULL " +
                " GROUP BY mtb_colt.gestione," +
                "                mtb_colt.data_collo," +
                "                mtb_colt.num_collo," +
                "                segno," +
                "                peso_kg," +
                "                lunghezza_cm," +
                "                larghezza_cm," +
                "                altezza_cm," +
                "                cod_anag," +
                "                cod_dtip," +
                "                mtb_colt.data_doc," +
                "                mtb_colt.ser_doc," +
                "                mtb_colt.num_doc," +
                "                rif_ord," +
                "                mtb_colt.ser_collo," +
                "                cod_tcol," +
                "                mtb_colt.data_ord," +
                "                mtb_colt.num_ord," +
                "                cod_vdes," +
                "                cod_mdep," +
                "                cod_vlis," +
                "                preparato_da," +
                "                ora_iniz_prep," +
                "                ora_fine_prep," +
                "                filtro_ordini," +
                "                annotazioni," +
                "                posizione," +
                "                cod_dtip_provv," +
                "                data_doc_provv," +
                "                ser_doc_provv," +
                "                num_doc_provv," +
                "                cod_jfas," +
                "                data_vers," +
                "                mtb_colt.peso_netto_kg";


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

    }


}
