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

import android.app.ProgressDialog;
import android.content.Context;
import androidx.databinding.ObservableArrayList;

import com.google.gson.reflect.TypeToken;

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

import it.integry.integrywmsnative.core.REST.consumers.ISimpleOperationCallback;
import it.integry.integrywmsnative.core.REST.consumers.SystemRESTConsumer;
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.utility.UtilityDB;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityProgress;
import it.integry.integrywmsnative.gest.rettifica_giacenze.dto.FornitoreDTO;

public class RettificaGiacenzeHelper {

    private Context mContext;

    public RettificaGiacenzeHelper(Context context){
        mContext = context;
    }


    public void loadListaFornitori(RunnableArgs<ArrayList<FornitoreDTO>> onComplete) {
        final ProgressDialog progress = UtilityProgress.createDefaultProgressDialog(mContext);
        //progress.show();

        String sql = "SELECT cod_alis as 'codAlis', descrizione FROM atb_list WHERE flag_attivo = 'S'";

        Type typeOfObjectsList = new TypeToken<ArrayList<FornitoreDTO>>() {}.getType();
        SystemRESTConsumer.processSql(sql, typeOfObjectsList, new ISimpleOperationCallback<ArrayList<FornitoreDTO>>() {
            @Override
            public void onSuccess(ArrayList<FornitoreDTO> value) {
                progress.dismiss();

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

            @Override
            public void onFailed(Exception ex) {
                UtilityExceptions.defaultException(mContext, ex, progress);
            }
        });
    }

    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();
        SystemRESTConsumer.processSql(sql, typeOfObjectsList, new ISimpleOperationCallback<ArrayList<MtbAartWithFornitore>>() {
            @Override
            public void onSuccess(ArrayList<MtbAartWithFornitore> value) {
                if(onComplete != null) onComplete.run(value);
            }

            @Override
            public void onFailed(Exception ex) {
                if(onFailed != null) onFailed.run(ex);
            }
        });
    }


    public void searchArtInt(String codMartOrBarcodeOrDescr, RunnableArgs<ArrayList<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {
        String sql = "SELECT DISTINCT mtb_aart.* " +
                "FROM mtb_aart, mvw_barcode " +
                "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 + "%') AND mtb_aart.cod_mart = mvw_barcode.cod_mart";

        Type typeOfObjectsList = new TypeToken<ArrayList<MtbAart>>() {}.getType();
        SystemRESTConsumer.processSql(sql, typeOfObjectsList, new ISimpleOperationCallback<ArrayList<MtbAart>>() {
            @Override
            public void onSuccess(ArrayList<MtbAart> value) {
                if(onComplete != null) onComplete.run(value);
            }

            @Override
            public void onFailed(Exception ex) {
                if(onFailed != null) onFailed.run(ex);
            }
        });
    }



}
