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

import android.util.Log;

import com.google.gson.Gson;
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 it.integry.integrywmsnative.core.REST.RESTBuilder;
import it.integry.integrywmsnative.core.REST.model.EsitoType;
import it.integry.integrywmsnative.core.REST.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.model.EntityBase;
import it.integry.integrywmsnative.core.model.MtbColt;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EntityRESTConsumer {

    public static <T extends EntityBase> void processEntity(T entityToSave, final ISimpleOperationCallback<T> callback, Class<T> type){

        EntityRESTConsumerService service = RESTBuilder.getService(EntityRESTConsumerService.class);
        Call<ServiceRESTResponse<JsonObject>> request = service.processEntity(entityToSave);
        request.enqueue(new Callback<ServiceRESTResponse<JsonObject>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<JsonObject>> call, Response<ServiceRESTResponse<JsonObject>> response) {
                if(response.isSuccessful()) {

                    if(response.body() != null) {
                        if(response.body().getEsito() == EsitoType.OK) {
                            Gson gson = new Gson();
                            T object = gson.fromJson(response.body().getEntity(), type);

                            callback.onSuccess(object);
                        } else {
                            Log.e("EntityRESTConsumer", response.body().getErrorMessage());
                            callback.onFailed(new Exception(response.body().getErrorMessage()));
                        }
                    } else {
                        Log.e("EntityRESTConsumer", response.message());
                        callback.onFailed(new Exception(response.message()));
                    }
                } else {
                    Log.e("EntityRESTConsumer", "Status " + response.code() + ": " + response.message());
                    callback.onFailed(new Exception("Status " + response.code() + ": " + response.message()));
                }
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<JsonObject>> call, Throwable t) {
                Log.e("EntityRESTConsumer", t.toString());
                callback.onFailed(new Exception(t));
            }
        });

    }




    public static <T extends EntityBase> void selectEntity(T entityToSave, final ISimpleOperationCallback<List<T>> callback, Class type){

        EntityRESTConsumerService service = RESTBuilder.getService(EntityRESTConsumerService.class);
        Call<ServiceRESTResponse<JsonObject>> request = service.processEntity(entityToSave);
        request.enqueue(new Callback<ServiceRESTResponse<JsonObject>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<JsonObject>> call, Response<ServiceRESTResponse<JsonObject>> response) {
                if(response.isSuccessful()) {

                    if(response.body() != null) {
                        if(response.body().getEsito() == EsitoType.OK) {
                            Gson gson = new Gson();
                            List<JsonObject> jsons = response.body().getEntityList();

                            List<T> newList = new ArrayList<T>();

                            if(jsons != null) {
                                for (int i = 0; i < jsons.size(); i ++){
                                    JsonObject jsonTmp = jsons.get(i);

                                    newList.add((T) gson.fromJson(jsonTmp, type));
                                }
                            }


                            callback.onSuccess(newList);
                        } else {
                            Log.e("EntityRESTConsumer", response.body().getErrorMessage());
                            callback.onFailed(new Exception(response.body().getErrorMessage()));
                        }
                    } else {
                        Log.e("EntityRESTConsumer", response.message());
                        callback.onFailed(new Exception(response.message()));
                    }
                } else {
                    Log.e("EntityRESTConsumer", "Status " + response.code() + ": " + response.message());
                    callback.onFailed(new Exception("Status " + response.code() + ": " + response.message()));
                }
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<JsonObject>> call, Throwable t) {
                Log.e("EntityRESTConsumer", t.toString());
                callback.onFailed(new Exception(t));
            }
        });

    }

}
