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

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.DtbOrdt;
import it.integry.integrywmsnative.core.model.MtbTCol;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.SaveDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class MagazzinoRESTConsumer extends _BaseRESTConsumer {

    private final SystemRESTConsumer mSystemRESTConsumer;

    public MagazzinoRESTConsumer(SystemRESTConsumer mSystemRESTConsumer) {
        this.mSystemRESTConsumer = mSystemRESTConsumer;
    }


    public void getTipiCollo(RunnableArgs<List<MtbTCol>> onComplete, RunnableArgs<Exception> onFailed) {
        String baseSql = "SELECT * " +
                "FROM mtb_tcol ";

        Type typeOfObjectsList = new TypeToken<ArrayList<MtbTCol>>() {
        }.getType();

        mSystemRESTConsumer.<ArrayList<MtbTCol>>processSql(baseSql, typeOfObjectsList, values -> {
            if (onComplete != null) {
                onComplete.run(values);
            }
        }, ex -> {
            if (onFailed != null) onFailed.run(ex);
        });
    }


    public void saveOrdine(SaveDTO saveDTO, RunnableArgs<DtbOrdt> onComplete, RunnableArgs<Exception> onFailed) {
        MagazzinoRESTConsumerService service = RESTBuilder.getService(MagazzinoRESTConsumerService.class, 240);
        service.saveOrdine(saveDTO)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<List<ServiceRESTResponse<DtbOrdt>>> call, Response<List<ServiceRESTResponse<DtbOrdt>>> response) {
                        if (response.body() != null && response.body().size() > 0) {
                            analyzeListOfAnswers(response, "saveOrdine", dtoList -> {
                                if (dtoList.size() > 0) {
                                    onComplete.run(dtoList.get(0));
                                } else {
                                    onFailed.run(new Exception("Nessun ordine generato"));
                                }
                            }, onFailed);
                        } else {
                            onFailed.run(new Exception("Nessun ordine generato"));
                        }
                    }

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

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

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