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

import com.google.gson.JsonObject;

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.contab_doc_interni.dto.GrigliaAcquistiDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.SaveDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.DocInterniSetupDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class DocInterniRESTConsumer extends _BaseRESTConsumer {

    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 Exception(t));
            }
        });
    }

    public void fetchProducts(String codDtip, String codMgrp, RunnableArgs<GrigliaAcquistiDTO> onComplete, RunnableArgs<Exception> onFailed) {
        DocInterniRESTConsumerService service = RESTBuilder.getService(DocInterniRESTConsumerService.class);
        service.fetchProducts(codDtip,codMgrp,"L").enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<GrigliaAcquistiDTO>> call, Response<ServiceRESTResponse<GrigliaAcquistiDTO>> response) {
                analyzeAnswer(response, "fetchProducts", onComplete, onFailed);
            }

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


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

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