package it.integry.integrywmsnative.gest.accettazione_ordini_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.model.dto.AlreadyRegisteredUDCDTO;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers._BaseRESTConsumer;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.gest.accettazione_ordini_elenco.dto.OrdineAccettazioneInevasoDTO;
import it.integry.integrywmsnative.gest.accettazione_ordini_picking.dto.RetrieveAlreadyRegisteredULAccettazioneOrdineRequestDTO;
import it.integry.integrywmsnative.gest.accettazione_ordini_picking.dto.RetrieveAlreadyRegisteredULAccettazioneOrdineResponseDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class AccettazioneOrdiniPickingRESTConsumer extends _BaseRESTConsumer {

    private final SystemRESTConsumer mSystemRestConsumer;

    @Inject
    public AccettazioneOrdiniPickingRESTConsumer(SystemRESTConsumer systemRESTConsumer) {
        this.mSystemRestConsumer = systemRESTConsumer;
    }

    public void getBancaliGiaRegistrati(List<OrdineAccettazioneInevasoDTO> ordiniToShow, RunnableArgs<List<AlreadyRegisteredUDCDTO>> onComplete, RunnableArgs<Exception> onFailed) {
        AccettazioneOrdiniPickingRESTConsumerService service = RESTBuilder.getService(AccettazioneOrdiniPickingRESTConsumerService.class);

        var request = new RetrieveAlreadyRegisteredULAccettazioneOrdineRequestDTO()
                .setOrdini(ordiniToShow);

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

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


}
