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

import com.google.gson.reflect.TypeToken;

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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.model.MtbAartWithFornitore;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers._BaseRESTConsumer;
import it.integry.integrywmsnative.core.utility.UtilityDB;
import it.integry.integrywmsnative.gest.rettifica_giacenze.dto.FornitoreDTO;

@Singleton
public class RettificaGiacenzeRESTConsumer extends _BaseRESTConsumer {

    private final SystemRESTConsumer mSystemRestConsumer;

    public RettificaGiacenzeRESTConsumer(SystemRESTConsumer systemRestConsumer) {
        mSystemRestConsumer = systemRestConsumer;
    }

    public void loadListaFornitori(RunnableArgs<ArrayList<FornitoreDTO>> onComplete, RunnableArgs<Exception> onFailed) {
        String sql = "SELECT cod_alis as 'codAlis', descrizione FROM atb_list WHERE flag_attivo = 'S'";

        Type typeOfObjectsList = new TypeToken<ArrayList<FornitoreDTO>>() {
        }.getType();

        mSystemRestConsumer.<ArrayList<FornitoreDTO>>processSql(sql, typeOfObjectsList, value -> {
            if (onComplete != null) onComplete.run(value);
        }, onFailed);
    }


    public void searchArtForn(String codAlis, String codArtForOrDescr, RunnableArgs<ArrayList<MtbAartWithFornitore>> onComplete, RunnableArgs<Exception> onFailed) {
        String sql = "SELECT mtb_aart.*, mtb_lisa.cod_art_for from mtb_lisa, mtb_aart, mtb_part " +
                " WHERE mtb_lisa.cod_alis = " + UtilityDB.valueToString(codAlis) +
                " AND mtb_lisa.cod_mart = mtb_aart.cod_mart " +
                " AND mtb_lisa.cod_mart = mtb_part.cod_mart " +
                " AND mtb_part.cod_mdep = '01' " +
                " AND (mtb_aart.descrizione_estesa LIKE '%" + codArtForOrDescr + "%' OR cod_art_for LIKE '%" + codArtForOrDescr + "%') " +
                " ORDER BY ABS(mtb_part.qta_esistente) DESC, mtb_aart.descrizione_estesa";


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


    public void searchArtInt(String codMartOrBarcodeOrDescr, RunnableArgs<ArrayList<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {
        String sql = "SELECT DISTINCT mtb_aart.* " +
                "FROM mtb_aart " +
                "LEFT OUTER JOIN mvw_barcode on mtb_aart.cod_mart = mvw_barcode.cod_mart " +
                "WHERE CASE" +
                "       WHEN LEN(mvw_barcode.cod_barre) > 13 THEN mvw_barcode.cod_barre " +
                "       ELSE Replicate('0', 13 - len(mvw_barcode.cod_barre))+ mvw_barcode.cod_barre " +
                "END  LIKE '%' +  CASE " +
                "                   WHEN LEN('" + codMartOrBarcodeOrDescr + "') > 13 THEN '" + codMartOrBarcodeOrDescr + "' " +
                "                   ELSE Replicate('0', 13 - len('" + codMartOrBarcodeOrDescr + "')) + '" + codMartOrBarcodeOrDescr + "'" +
                "                 END " +
                "OR mtb_aart.cod_mart = '" + codMartOrBarcodeOrDescr + "' " +
                "OR mtb_aart.descrizione_estesa LIKE '%" + codMartOrBarcodeOrDescr + "%'";

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

}
