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.MtbInvent;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.rest.model.inventario.InsertInventarioRequestDTO;
import it.integry.integrywmsnative.core.rest.model.inventario.RetrieveInventarioArtsResponseDTO;
import it.integry.integrywmsnative.core.rest.model.inventario.RetrieveInventarioResponseDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class InventarioRESTConsumer extends _BaseRESTConsumer {


    public void loadInventario(long inventoryId, RunnableArgs<RetrieveInventarioResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
        var inventarioRESTConsumerService = RESTBuilder.getService(InventarioRESTConsumerService.class);
        inventarioRESTConsumerService.retrieve(inventoryId)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(@NonNull Call<ServiceRESTResponse<RetrieveInventarioResponseDTO>> call, @NonNull Response<ServiceRESTResponse<RetrieveInventarioResponseDTO>> response) {
                        analyzeAnswer(response, "loadInventario", onComplete, onFailed);
                    }

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


    public void loadInventarioArts(long inventoryId, RunnableArgs<RetrieveInventarioArtsResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
        var inventarioRESTConsumerService = RESTBuilder.getService(InventarioRESTConsumerService.class);
        inventarioRESTConsumerService.retrieveArts(inventoryId)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(@NonNull Call<ServiceRESTResponse<RetrieveInventarioArtsResponseDTO>> call, @NonNull Response<ServiceRESTResponse<RetrieveInventarioArtsResponseDTO>> response) {
                        analyzeAnswer(response, "loadInventarioArts", onComplete, onFailed);
                    }

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

    public List<MtbInvent> makeSynchronousRetrieveRequest() throws Exception {
        var inventarioRESTConsumerService = RESTBuilder.getService(InventarioRESTConsumerService.class, 120);

        var response = inventarioRESTConsumerService.retrieve()
                .execute();

        var data = analyzeAnswer(response, "retrieveInventario");
        if (data == null) return null;
        return data.getInventories();
    }


    public void makeInsertRequest(MtbInvent inventarioToInsert, final Runnable onComplete, final RunnableArgs<Exception> onFailed) {
        var request = new InsertInventarioRequestDTO()
                .setMtbInvent(inventarioToInsert);

        var inventarioRESTConsumerService = RESTBuilder.getService(InventarioRESTConsumerService.class, 300);
        inventarioRESTConsumerService
                .insert(inventarioToInsert.getIdInventario(), request)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<Void>> call, Response<ServiceRESTResponse<Void>> response) {
                        analyzeAnswer(response, "insertInventario", ignored -> onComplete.run(), onFailed);
                    }

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

    }

}
