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

import android.util.Log;

import java.util.List;

import it.integry.integrywmsnative.core.REST.RESTBuilder;
import it.integry.integrywmsnative.core.REST.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static br.com.zbra.androidlinq.Linq.stream;

public class PrinterRESTConsumer extends _BaseRESTConsumer {

    public enum Type {
        PRIMARY,
        SECONDARY
    }


    public static void getAvailablePrinters(final ISimpleOperationCallback<List<String>> callback) {
        PrinterRESTConsumerService printerService = RESTBuilder.getService(PrinterRESTConsumerService.class);
        printerService.getAvailablePrinters().enqueue(new Callback<ServiceRESTResponse<List<String>>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<String>>> call, Response<ServiceRESTResponse<List<String>>> response) {
                analyzeAnswer(response, "GetAvailablePrinters", callback);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<List<String>>> call, Throwable t) {

            }
        });
    }


    public static void getAvailablePrinters(String codMdep, final ISimpleOperationCallback<List<String>> callback) {

        PrinterRESTConsumerService printerService = RESTBuilder.getService(PrinterRESTConsumerService.class);
        printerService.getAvailablePrinters(codMdep).enqueue(new Callback<ServiceRESTResponse<List<String>>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<String>>> call, Response<ServiceRESTResponse<List<String>>> response) {
                analyzeAnswer(response, "GetAvailablePrinters", new ISimpleOperationCallback<List<String>>() {
                    @Override
                    public void onSuccess(List<String> value) {
                        callback.onSuccess(value != null ? stream(value).where(x -> x != null).toList() : null);
                    }

                    @Override
                    public void onFailed(Exception ex) {
                        callback.onFailed(ex);
                    }
                });
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<List<String>>> call, Throwable t) {
                Log.e("GetAvailablePrinters", t.toString());
                UtilityLogger.errorMe(new Exception(t));
                callback.onFailed(new Exception(t));
            }
        });
    }


    public static void getAvailablePrinters(String codMdep, Type printerType, final ISimpleOperationCallback<List<String>> callback) {

        PrinterRESTConsumerService printerService = RESTBuilder.getService(PrinterRESTConsumerService.class);
        printerService.getAvailablePrinters(codMdep, printerType.toString()).enqueue(new Callback<ServiceRESTResponse<List<String>>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<List<String>>> call, Response<ServiceRESTResponse<List<String>>> response) {
                analyzeAnswer(response, "GetAvailablePrinters", callback);
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<List<String>>> call, Throwable t) {
                Log.e("GetAvailablePrinters", t.toString());
                UtilityLogger.errorMe(new Exception(t));
                callback.onFailed(new Exception(t));
            }
        });
    }

    public static void printCollo(String printerName, MtbColt testataColloToPrint, int quantity, String reportName, Runnable onComplete, RunnableArgs<Exception> onFailed) {

        PrinterRESTConsumerService printerService = RESTBuilder.getService(PrinterRESTConsumerService.class);
        printerService.printCollo(
                printerName,
                testataColloToPrint.getDataColloS(),
                testataColloToPrint.getGestione(),
                testataColloToPrint.getSerCollo(),
                testataColloToPrint.getNumCollo(),
                quantity,
                reportName)

                .enqueue(new Callback<ServiceRESTResponse<Object>>() {
                    @Override
                    public void onResponse(Call<ServiceRESTResponse<Object>> call, Response<ServiceRESTResponse<Object>> response) {
                        analyzeAnswer(response, "PrintCollo", data -> {
                            onComplete.run();
                        }, onFailed);
                    }

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

    }

}
