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

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

import java.util.List;

import javax.inject.Singleton;

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 retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class ArticoloRESTConsumer extends _BaseRESTConsumer {


    public 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 void getByCodMarts(List<String> codMartToFind, RunnableArgs<List<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {
        getByCodMartsStatic(codMartToFind, onComplete, onFailed);
    }

    public static void getByCodMartsStatic(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));
            }
        });

    }


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

        ArticoloRESTConsumerService articoloRESTConsumer = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumer.getByCodMart(codMartToFind).enqueue(new Callback<ServiceRESTResponse<MtbAart>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<MtbAart>> call, Response<ServiceRESTResponse<MtbAart>> response) {
                analyzeAnswer(response, "getByCodMart", (m) -> {
                    if(response.body().getEntityList() != null && !response.body().getEntityList().isEmpty()) {
                        onComplete.run(response.body().getEntityList().get(0));
                    } else onComplete.run(null);

                }, onFailed);
            }

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

}
