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

import androidx.annotation.NonNull;

import java.util.List;

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.DtbDoct;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.LoadColliDTO;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.rest.model.documento.RetrieveDocumentoArtsResponseDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class DocumentRESTConsumer extends _BaseRESTConsumer {
    

    public void createDocsFromColli(List<LoadColliDTO> listColli, RunnableArgs<List<DtbDoct>> onComplete, RunnableArgs<Exception> onFailed) {
        DocumentiRESTConsumerService documentiRESTConsumerService = RESTBuilder.getService(DocumentiRESTConsumerService.class);
        documentiRESTConsumerService.createDocsFromColli(listColli).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<DtbDoct>>> call, Response<ServiceRESTResponse<List<DtbDoct>>> response) {
                analyzeAnswer(response, "createDocsFromColli", onComplete, onFailed);
            }

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

    public void createDocFromColli(LoadColliDTO loadColliDTO, RunnableArgs<DtbDoct> onComplete, RunnableArgs<Exception> onFailed) {
        DocumentiRESTConsumerService documentiRESTConsumerService = RESTBuilder.getService(DocumentiRESTConsumerService.class);
        documentiRESTConsumerService.createDocFromColli(loadColliDTO).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<DtbDoct>> call, Response<ServiceRESTResponse<DtbDoct>> response) {
                analyzeAnswer(response, "createDocFromColli", onComplete, onFailed);
            }

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




    public void loadDocumentAvailableArts(String codDtip, String codMgrp, String codAnagForn, RunnableArgs<RetrieveDocumentoArtsResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
        var inventarioRESTConsumerService = RESTBuilder.getService(DocumentiRESTConsumerService.class);
        inventarioRESTConsumerService.retrieveArts(codDtip, codMgrp, codAnagForn)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(@NonNull Call<ServiceRESTResponse<RetrieveDocumentoArtsResponseDTO>> call, @NonNull Response<ServiceRESTResponse<RetrieveDocumentoArtsResponseDTO>> response) {
                        analyzeAnswer(response, "loadDocumentoArts", onComplete, onFailed);
                    }

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