package it.integry.integrywmsnative.core.REST.consumers;

import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;

import java.util.List;

import it.integry.integrywmsnative.core.REST.RESTBuilder;
import it.integry.integrywmsnative.core.REST.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.utility.UtilityDB;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ArticoloRESTConsumer extends _BaseRESTConsumer {

    public static void getByBarcodeProd(String barcodeProd, RunnableArgs<List<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {
        ArticoloRESTConsumerService articoloRESTConsumerService = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumerService.getByBarcodeProd(barcodeProd).enqueue(new Callback<ServiceRESTResponse<MtbAart>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<MtbAart>> call, Response<ServiceRESTResponse<MtbAart>> response) {
                analyzeAnswer(response, "getByBarcodeProd", (m) -> {
                    onComplete.run(response.body().getEntityList());
                }, onFailed);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<MtbAart>> call, Throwable t) {
                onFailed.run(new Exception(t));
            }
        });
    }


    public static void getByCodMart(List<String> codMartToFind, RunnableArgs<List<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {

        String joinedCods = Stream.of(codMartToFind)
                .collect(Collectors.joining(","));


        ArticoloRESTConsumerService articoloRESTConsumer = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumer.getByCodMart(joinedCods).enqueue(new Callback<ServiceRESTResponse<MtbAart>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<MtbAart>> call, Response<ServiceRESTResponse<MtbAart>> response) {
                analyzeAnswer(response, "getByCodMart", (m) -> {
                    onComplete.run(response.body().getEntityList());
                }, onFailed);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<MtbAart>> call, Throwable t) {
                onFailed.run(new Exception(t));
            }
        });

    }

}
