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

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.GtbAnag;
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 UltimeConsegneClienteRESTConsumer {

    private final SystemRESTConsumer systemRESTConsumer;

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

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

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

    public void getUltimeConsegneClienti(String codMdep, String codAnag, String codMart, Integer limitConsegnePerCli, int limitDays, RunnableArgs<ArrayList<DocumentoResoDTO>> onComplete, RunnableArgs<Exception> onFailed) {

        String sql = "SELECT consegne.*, " +
                "       rag_soc," +
                "       dtb_doct.gestione  " +
                "FROM WMS_GetUltimeConsegneClienti(" +
                UtilityDB.valueToString(limitConsegnePerCli >= 0 ? limitConsegnePerCli : null) + ", " +
                UtilityDB.valueToString(codAnag) + ", " +
                UtilityDB.valueToString(codMart) + ", " +
                UtilityDB.valueToString(codMdep) + ", " +
                UtilityDB.valueToString(limitDays) + ") consegne " +
                "INNER JOIN dtb_doct ON consegne.num_doc = dtb_doct.num_doc AND consegne.data_doc = dtb_doct.data_doc AND consegne.cod_anag = dtb_doct.cod_anag AND consegne.cod_dtip = dtb_doct.cod_dtip AND consegne.ser_doc = dtb_doct.ser_doc " +
                "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();
        systemRESTConsumer.<ArrayList<DocumentoResoDTO>>processSql(sql, typeOfObjectsList, value -> {
            if (onComplete != null) onComplete.run(value);
        }, ex -> {
            if (onFailed != null) onFailed.run(ex);
        });
    }

}
