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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.exception.RestException;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.consumers.MagazzinoRESTConsumer;
import it.integry.integrywmsnative.core.rest.consumers._BaseRESTConsumer;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.DocInterniSetupDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.SaveDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class DocInterniRESTConsumer extends _BaseRESTConsumer {

    private final MagazzinoRESTConsumer magazzinoRESTConsumer;

    public DocInterniRESTConsumer(MagazzinoRESTConsumer magazzinoRESTConsumer) {
        this.magazzinoRESTConsumer = magazzinoRESTConsumer;
    }

    public void getDocInterniSetup(RunnableArgs<DocInterniSetupDTO> onComplete, RunnableArgs<Exception> onFailed) {
        DocInterniRESTConsumerService service = RESTBuilder.getService(DocInterniRESTConsumerService.class);
        service.getSetupDocInterni().enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<DocInterniSetupDTO>> call, Response<ServiceRESTResponse<DocInterniSetupDTO>> response) {
                analyzeAnswer(response, "getSetupDocInterni", onComplete, onFailed);
            }

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


    public void saveDoc(SaveDTO saveDTO, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        magazzinoRESTConsumer.saveTerminalinoWMS(saveDTO, onComplete, onFailed);
    }
}
