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

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.consumers._BaseRESTConsumer;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.gest.accettazione_bolla_elenco.rest.dto.TestataBollaAccettazioneDTO;
import it.integry.integrywmsnative.core.model.dto.AlreadyRegisteredUDCDTO;
import it.integry.integrywmsnative.gest.accettazione_bolla_picking.dto.RetrieveAlreadyRegisteredULAccettazioneBollaRequestDTO;
import it.integry.integrywmsnative.gest.accettazione_bolla_picking.dto.RetrieveAlreadyRegisteredULAccettazioneBollaResponseDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class AccettazioneBollaPickingRESTConsumer extends _BaseRESTConsumer {

    @Inject
    public AccettazioneBollaPickingRESTConsumer() {
    }


    public void retrieveAlreadyRegisteredUDC(List<TestataBollaAccettazioneDTO> bolle, RunnableArgs<List<AlreadyRegisteredUDCDTO>> onComplete, RunnableArgs<Exception> onFailed) {
        AccettazioneBollaPickingRESTConsumerService service = RESTBuilder.getService(AccettazioneBollaPickingRESTConsumerService.class);

        var request = new RetrieveAlreadyRegisteredULAccettazioneBollaRequestDTO()
                .setBolle(bolle);

        service.retrieveAlreadyRegisteredUDC(request)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<RetrieveAlreadyRegisteredULAccettazioneBollaResponseDTO>> call, Response<ServiceRESTResponse<RetrieveAlreadyRegisteredULAccettazioneBollaResponseDTO>> response) {
                        analyzeAnswer(response, "retrieveAlreadyRegisteredUDC",
                                data -> onComplete.run(data.getUdcList() == null ? new ArrayList<>() : data.getUdcList()),
                                onFailed);
                    }

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