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

import javax.inject.Singleton;

import it.integry.barcode_base_android_library.model.BarcodeScanDTO;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.Ean128Model;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class BarcodeRESTConsumer extends _BaseRESTConsumer {

    public void decodeEan128(BarcodeScanDTO barcodeObj, RunnableArgs<Ean128Model> onComplete, RunnableArgs<Exception> onFailed) {

        String ean128 = barcodeObj.getStringValue().replaceAll("" + ((char) 29), "|");

        BarcodeRESTConsumerService barcodeRESTConsumerService = RESTBuilder.getService(BarcodeRESTConsumerService.class);
        barcodeRESTConsumerService.decodeEan128(ean128).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<Ean128Model>> call, Response<ServiceRESTResponse<Ean128Model>> response) {
                analyzeAnswer(response, "DecodeEan128", onComplete, onFailed);
            }

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