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

import android.util.Log;

import it.integry.integrywmsnative.core.REST.RESTBuilder;
import it.integry.integrywmsnative.core.REST.model.GestSetupDTO;
import it.integry.integrywmsnative.core.REST.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import it.integry.integrywmsnative.generated.callback.Runnable;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GestSetupRESTConsumer extends _BaseRESTConsumer {

    public static void getValue(String gestName, String sectionName, String keySection, RunnableArgs<GestSetupDTO> onComplete, RunnableArgs<Exception> onFailed) {
        GestSetupRESTConsumerService service = RESTBuilder.getService(GestSetupRESTConsumerService.class);
        service.getGestSetupValue(gestName, sectionName, keySection).enqueue(new Callback<ServiceRESTResponse<GestSetupDTO>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<GestSetupDTO>> call, Response<ServiceRESTResponse<GestSetupDTO>> response) {
                analyzeAnswer(response, "GestSetup", onComplete, onFailed);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<GestSetupDTO>> call, Throwable t) {
                Log.e("PrintCollo", t.toString());
                UtilityLogger.errorMe(new Exception(t));
                if(onFailed != null) onFailed.run(new Exception(t));
            }
        });
    }

    public static void getBooleanValue(String gestName, String sectionName, String keySection, RunnableArgs<Boolean> onComplete, RunnableArgs<Exception> onFailed) {
        getValue(gestName, sectionName, keySection, value -> {
            if(value != null){
                onComplete.run("S".equalsIgnoreCase(value.value));
            } else onComplete.run(false);
        }, ex -> {
            if(onFailed != null) onFailed.run(ex);
        });
    }

    public static void getValue(String gestName, String sectionName, String keySection, String codMdep, RunnableArgs<GestSetupDTO> onComplete, RunnableArgs<Exception> onFailed) {
        GestSetupRESTConsumerService service = RESTBuilder.getService(GestSetupRESTConsumerService.class);
        service.getGestSetupValue(gestName, sectionName, keySection, codMdep).enqueue(new Callback<ServiceRESTResponse<GestSetupDTO>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<GestSetupDTO>> call, Response<ServiceRESTResponse<GestSetupDTO>> response) {
                analyzeAnswer(response, "GestSetup", onComplete, onFailed);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<GestSetupDTO>> call, Throwable t) {
                Log.e("PrintCollo", t.toString());
                UtilityLogger.errorMe(new Exception(t));
                if(onFailed != null) onFailed.run(new Exception(t));
            }
        });
    }

    public static void getBooleanValue(String gestName, String sectionName, String keySection, String codMdep, RunnableArgs<Boolean> onComplete, RunnableArgs<Exception> onFailed) {
        getValue(gestName, sectionName, keySection, codMdep, value -> {
            if(value != null){
                onComplete.run("S".equalsIgnoreCase(value.value));
            } else onComplete.run(false);
        }, ex -> {
            if(onFailed != null) onFailed.run(ex);
        });
    }


}
