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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.consumers.interfaces.ColliCaricoRESTConsumerInterface;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.rest.model.udc.CloseUDCRequestDTO;
import it.integry.integrywmsnative.core.rest.model.udc.CloseUDCResponseDTO;
import it.integry.integrywmsnative.core.rest.model.udc.CreateUDCRequestDTO;
import it.integry.integrywmsnative.core.rest.model.udc.CreateUDCResponseDTO;
import it.integry.integrywmsnative.core.rest.model.udc.DeleteUDCRowRequestDTO;
import it.integry.integrywmsnative.core.rest.model.udc.EditUDCRowRequestDTO;
import it.integry.integrywmsnative.core.rest.model.udc.EditUDCRowResponseDTO;
import it.integry.integrywmsnative.core.rest.model.uds.InsertUDCRowRequestDTO;
import it.integry.integrywmsnative.core.rest.model.uds.InsertUDCRowResponseDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class ColliAccettazioneRESTConsumer extends _BaseRESTConsumer implements ColliCaricoRESTConsumerInterface {

    public void createUDC(CreateUDCRequestDTO createUDCRequestDTO, RunnableArgs<MtbColt> onComplete, RunnableArgs<Exception> onFailed) {
        ColliAccettazioneRESTConsumerService colliAccettazioneRESTConsumerService = RESTBuilder.getService(ColliAccettazioneRESTConsumerService.class);

        colliAccettazioneRESTConsumerService.createUDC(createUDCRequestDTO)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<CreateUDCResponseDTO>> call, Response<ServiceRESTResponse<CreateUDCResponseDTO>> response) {
                        analyzeAnswer(response, "accettazione/createUDC", data -> onComplete.run(data.getMtbColt()), onFailed);
                    }

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

    }

    @Override
    public void closeUDC(CloseUDCRequestDTO closeUDCRequestDTO, RunnableArgs<CloseUDCResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
        ColliAccettazioneRESTConsumerService colliAccettazioneRESTConsumerService = RESTBuilder.getService(ColliAccettazioneRESTConsumerService.class);

        colliAccettazioneRESTConsumerService.closeUDC(closeUDCRequestDTO)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<CloseUDCResponseDTO>> call, Response<ServiceRESTResponse<CloseUDCResponseDTO>> response) {
                        analyzeAnswer(response, "accettazione/closeUDC", onComplete, onFailed);
                    }

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

    @Override
    public void insertUDCRow(InsertUDCRowRequestDTO insertUDCRowRequestDTO, RunnableArgs<MtbColr> onComplete, RunnableArgs<Exception> onFailed) {
        ColliAccettazioneRESTConsumerService colliAccettazioneRESTConsumerService = RESTBuilder.getService(ColliAccettazioneRESTConsumerService.class);

        colliAccettazioneRESTConsumerService.insertUDCRow(insertUDCRowRequestDTO)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<InsertUDCRowResponseDTO>> call, Response<ServiceRESTResponse<InsertUDCRowResponseDTO>> response) {
                        analyzeAnswer(response, "accettazione/insertUDCRow", data -> onComplete.run(data.getSavedMtbColr()), onFailed);
                    }

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

    @Override
    public void editUDCRow(EditUDCRowRequestDTO editUDCRowRequestDTO, RunnableArgs<MtbColr> onComplete, RunnableArgs<Exception> onFailed) {
        ColliAccettazioneRESTConsumerService colliAccettazioneRESTConsumerService = RESTBuilder.getService(ColliAccettazioneRESTConsumerService.class);

        colliAccettazioneRESTConsumerService.editUDCRow(editUDCRowRequestDTO)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<EditUDCRowResponseDTO>> call, Response<ServiceRESTResponse<EditUDCRowResponseDTO>> response) {
                        analyzeAnswer(response, "accettazione/editUDCRow", data -> onComplete.run(data.getSavedMtbColr()), onFailed);
                    }

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

    @Override
    public void deleteUDCRow(DeleteUDCRowRequestDTO deleteUDCRowRequestDTO, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        ColliAccettazioneRESTConsumerService colliAccettazioneRESTConsumerService = RESTBuilder.getService(ColliAccettazioneRESTConsumerService.class);

        colliAccettazioneRESTConsumerService.deleteUDCRow(deleteUDCRowRequestDTO)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<Void>> call, Response<ServiceRESTResponse<Void>> response) {
                        analyzeAnswer(response, "accettazione/deleteUDCRow", data -> onComplete.run(), onFailed);
                    }

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

}
