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

import android.util.Log;

import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.StbGestSetup;
import it.integry.integrywmsnative.core.rest.RESTBuilder;
import it.integry.integrywmsnative.core.rest.model.GestSetupDTO;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.settings.StbGestSetupReader;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Singleton
public class GestSetupRESTConsumer extends _BaseRESTConsumer {

    public void getValue(String gestName, String sectionName, String keySection, RunnableArgs<GestSetupDTO> onComplete, RunnableArgs<Exception> onFailed) {
        GestSetupRESTConsumerService service = RESTBuilder.getService(GestSetupRESTConsumerService.class);

        service.getGestSetupValue(gestName, sectionName, keySection)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<GestSetupDTO>> call, Response<ServiceRESTResponse<GestSetupDTO>> response) {
                        analyzeAnswer(response, "GestSetup", onComplete, onFailed);
                    }

                    @Override
                    public void onFailure(Call<ServiceRESTResponse<GestSetupDTO>> call, Throwable t) {
                        Log.e("GestSetup", t.toString());
                        // UtilityLogger.errorMe(new Exception(t));
                        if (onFailed != null) onFailed.run(new Exception(t));
                    }
                });
    }

    public void getBooleanValue(String gestName, String sectionName, String keySection, RunnableArgs<Boolean> onComplete, RunnableArgs<Exception> onFailed) {
        getValue(gestName, sectionName, keySection, value -> {
            if (value != null) {
                onComplete.run("S".equalsIgnoreCase(value.value));
            } else onComplete.run(false);
        }, ex -> {
            if (onFailed != null) onFailed.run(ex);
        });
    }

    public void getValue(String gestName, String sectionName, String keySection, String codMdep, RunnableArgs<GestSetupDTO> onComplete, RunnableArgs<Exception> onFailed) {
        GestSetupRESTConsumerService service = RESTBuilder.getService(GestSetupRESTConsumerService.class);
        service.getGestSetupValue(gestName, sectionName, keySection, codMdep).enqueue(new Callback<ServiceRESTResponse<GestSetupDTO>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<GestSetupDTO>> call, Response<ServiceRESTResponse<GestSetupDTO>> response) {
                analyzeAnswer(response, "GestSetup", onComplete, onFailed);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<GestSetupDTO>> call, Throwable t) {
                Log.e("PrintCollo", t.toString());
                UtilityLogger.error(new Exception(t));
                if (onFailed != null) onFailed.run(new Exception(t));
            }
        });
    }

    public void getBooleanValue(String gestName, String sectionName, String keySection, String codMdep, RunnableArgs<Boolean> onComplete, RunnableArgs<Exception> onFailed) {
        getValue(gestName, sectionName, keySection, codMdep, value -> {
            if (value != null) {
                onComplete.run("S".equalsIgnoreCase(value.value));
            } else onComplete.run(false);
        }, ex -> {
            if (onFailed != null) onFailed.run(ex);
        });
    }


    public void getValues(String codMdep, List<StbGestSetupReader> stbGestSetupList, RunnableArgs<List<StbGestSetupReader>> onComplete, RunnableArgs<Exception> onFailed) {
        var stbGestSetups = stbGestSetupList.stream().map(x -> (StbGestSetup) x).collect(Collectors.toList());

        GestSetupRESTConsumerService service = RESTBuilder.getService(GestSetupRESTConsumerService.class);
        service.getGestSetupValues(codMdep, stbGestSetups).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<StbGestSetup>>> call, Response<ServiceRESTResponse<List<StbGestSetup>>> response) {
                analyzeAnswer(response, "GestSetup", data -> {

                    for (StbGestSetup stbGestSetup : data) {
                        stbGestSetupList.stream().filter(x -> stbGestSetup.getGestName().equalsIgnoreCase(x.getGestName()) &&
                                        stbGestSetup.getSection().equalsIgnoreCase(x.getSection()) &&
                                        stbGestSetup.getKeySection().equalsIgnoreCase(x.getKeySection()))
                                .findFirst()
                                .ifPresent(x -> x.setValue(stbGestSetup.getValue()));
                    }

                    onComplete.run(stbGestSetupList);
                }, onFailed);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<List<StbGestSetup>>> call, Throwable t) {
                UtilityLogger.error(new Exception(t));
                if (onFailed != null) onFailed.run(new Exception(t));
            }
        });
    }


}
