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

import com.annimon.stream.Stream;
import com.google.gson.JsonObject;

import java.util.List;

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.gest.prod_linee_produzione.dto.ProdLineStatusDTO;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class ProductionLinesRESTConsumer extends _BaseRESTConsumer {

    public void avviaLineaDiProduzione(String codJfas, Integer hrNum, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        ProductionLinesRESTConsumerService restService = RESTBuilder.getService(ProductionLinesRESTConsumerService.class);
        Call<ServiceRESTResponse<JsonObject>> callable = restService.avviaLineaDiProduzione(codJfas, hrNum);
        callable.enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<JsonObject>> call, Response<ServiceRESTResponse<JsonObject>> response) {
                analyzeAnswer(response, "avviaLineaDiProduzione", data -> {
                    onComplete.run();
                }, onFailed);
            }

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

    public void arrestaLineaDiProduzione(String codJfas, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        ProductionLinesRESTConsumerService restService = RESTBuilder.getService(ProductionLinesRESTConsumerService.class);
        Call<ServiceRESTResponse<JsonObject>> callable = restService.arrestaLineaDiProduzione(codJfas);
        callable.enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<JsonObject>> call, Response<ServiceRESTResponse<JsonObject>> response) {
                analyzeAnswer(response, "arrestaLineaDiProduzione", data -> {
                    onComplete.run();
                }, onFailed);
            }

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

    public void avviaProduzioneArticoloSuLinea(String codJfas, String codMart, String partitaMag, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        ProductionLinesRESTConsumerService restService = RESTBuilder.getService(ProductionLinesRESTConsumerService.class, 300);
        Call<ServiceRESTResponse<JsonObject>> callable = restService.avviaProduzioneArticoloSuLinea(codJfas, codMart, partitaMag);
        callable.enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<JsonObject>> call, Response<ServiceRESTResponse<JsonObject>> response) {
                analyzeAnswer(response, "avviaProduzioneArticoloSuLinea", data -> {
                    onComplete.run();
                }, onFailed);
            }

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

    public void getStatoLinee(String codMdep, RunnableArgs<List<ProdLineStatusDTO>> onComplete, RunnableArgs<Exception> onFailed) {
        ProductionLinesRESTConsumerService restService = RESTBuilder.getService(ProductionLinesRESTConsumerService.class);
        Call<ServiceRESTResponse<List<ProdLineStatusDTO>>> callable = restService.getStatoLinee(codMdep);
        callable.enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<ProdLineStatusDTO>>> call, Response<ServiceRESTResponse<List<ProdLineStatusDTO>>> response) {
                analyzeAnswer(response, "getStatoLinee", onComplete::run, onFailed);
            }

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


    public void getLineDetails(ProdLineStatusDTO lineaProd, RunnableArgs<ProdLineStatusDTO> onComplete, RunnableArgs<Exception> onFailed) {
        ProductionLinesRESTConsumerService restService = RESTBuilder.getService(ProductionLinesRESTConsumerService.class);
        Call<ServiceRESTResponse<List<ProdLineStatusDTO>>> callable = restService.getStatoLinee(lineaProd.getCodMdepLav());
        callable.enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<ProdLineStatusDTO>>> call, Response<ServiceRESTResponse<List<ProdLineStatusDTO>>> response) {
                analyzeAnswer(response, "getLineDetails", (lines) -> {
                    ProdLineStatusDTO newLine = Stream.of(lines).filter(dto -> dto.getCodJfas().equalsIgnoreCase(lineaProd.getCodJfas())).findFirstOrElse(null);
                    onComplete.run(newLine);
                }, onFailed);
            }

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

  
}
