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

import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.google.gson.reflect.TypeToken;

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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.model.MtbGrup;
import it.integry.integrywmsnative.core.model.dto.StatoArticoloDTO;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.utility.UtilityQuery;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class ArticoloRESTConsumer extends _BaseRESTConsumer {

    private final SystemRESTConsumer systemRESTConsumer;

    public ArticoloRESTConsumer(SystemRESTConsumer systemRESTConsumer) {
        this.systemRESTConsumer = systemRESTConsumer;
    }

    public void getByBarcodeProd(String barcodeProd, RunnableArgs<List<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {
        ArticoloRESTConsumerService articoloRESTConsumerService = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumerService.getByBarcodeProd(barcodeProd).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<MtbAart>> call, Response<ServiceRESTResponse<MtbAart>> response) {
                analyzeAnswer(response, "getByBarcodeProd", (m) -> {
                    List<MtbAart> aartList = response.body().getEntityList();
                    aartList = aartList != null ? Stream.of(aartList).filter(x -> x.getFlagStato().equalsIgnoreCase("A")).toList() : null;
                    onComplete.run(aartList);
                }, onFailed);
            }

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


    public void getByCodMarts(List<String> codMartToFind, RunnableArgs<List<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {
        getByCodMartsStatic(codMartToFind, onComplete, onFailed);
    }

    public static void getByCodMartsStatic(List<String> codMartToFind, RunnableArgs<List<MtbAart>> onComplete, RunnableArgs<Exception> onFailed) {

        String joinedCods = Stream.of(codMartToFind)
                .collect(Collectors.joining(","));


        ArticoloRESTConsumerService articoloRESTConsumer = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumer.getByCodMart(joinedCods).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<MtbAart>> call, Response<ServiceRESTResponse<MtbAart>> response) {
                analyzeAnswer(response, "getByCodMart", (m) -> {
                    onComplete.run(response.body().getEntityList());
                }, onFailed);
            }

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

    }

    public void getStatoPartita(String codMart, String partitaMag, RunnableArgs<List<StatoArticoloDTO>> onComplete, RunnableArgs<Exception> onFailed) {

        ArticoloRESTConsumerService articoloRESTConsumer = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumer.getStatoPartita(codMart, partitaMag).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<StatoArticoloDTO>>> call, Response<ServiceRESTResponse<List<StatoArticoloDTO>>> response) {
                analyzeAnswer(response, "getStatoPartita", onComplete, onFailed);
            }

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

    }


    public void getByCodMart(String codMartToFind, RunnableArgs<MtbAart> onComplete, RunnableArgs<Exception> onFailed) {

        ArticoloRESTConsumerService articoloRESTConsumer = RESTBuilder.getService(ArticoloRESTConsumerService.class);
        articoloRESTConsumer.getByCodMart(codMartToFind).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<MtbAart>> call, Response<ServiceRESTResponse<MtbAart>> response) {
                analyzeAnswer(response, "getByCodMart", (m) -> {
                    if (response.body().getEntityList() != null && !response.body().getEntityList().isEmpty()) {
                        onComplete.run(response.body().getEntityList().get(0));
                    } else onComplete.run(null);

                }, onFailed);
            }

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


    public void getArtsGroups(List<String> groupsToFind, RunnableArgs<List<MtbGrup>> onComplete, RunnableArgs<Exception> onFailed) {
        var whereCondMap = Stream.of(groupsToFind)
                .map(x -> {
                    HashMap<String, Object> vars = new HashMap<>();
                    vars.put("cod_mgrp", x);
                    return vars;
                })
                .toList();

        var whereCond = whereCondMap.isEmpty() ? "" : " WHERE " + UtilityQuery.concatFieldListInWhereCond(whereCondMap);

        Type typeOfObjectsList = new TypeToken<ArrayList<MtbGrup>>() {
        }.getType();
        this.systemRESTConsumer.processSql("SELECT * FROM mtb_grup " + whereCond, typeOfObjectsList, onComplete, onFailed);

    }

}
