package es.craftsmanship.toledo.katangapp.interactors;

import es.craftsmanship.toledo.katangapp.models.QueryResult;
import es.craftsmanship.toledo.katangapp.services.StopsService;
import es.craftsmanship.toledo.katangapp.utils.AndroidBus;

import java.io.IOException;

import retrofit2.Response;
import retrofit2.Retrofit;

import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * @author Javier Gamarra
 */
public class StopsInteractor implements Runnable {

    private static final String BACKEND_ENDPOINT = "https://secret-depths-4660.herokuapp.com";
    private final String radio;
    private final Double latitude;
    private final Double longitude;

    public StopsInteractor(String radio, Double latitude, Double longitude) {
        this.radio = radio;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    @Override
    public void run() {
        try {
            Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BACKEND_ENDPOINT)
                .addConverterFactory(JacksonConverterFactory.create())
                .build();

            StopsService service = retrofit.create(StopsService.class);

            Response<QueryResult> response = service.listStops(
                latitude, longitude, radio).execute();

            if (response.isSuccess()) {
                AndroidBus.getInstance().post(response.body());
            }
            else {
                AndroidBus.getInstance().post(new Error(response.message()));
            }
        }
        catch (IOException e) {
            AndroidBus.getInstance().post(e);
        }
    }

}
@@ -1284,24 +1284,83 @@
 execute();%0A%0A
+            Object event = new Error(response.message());%0A%0A
             
@@ -1407,38 +1407,16 @@
     
-AndroidBus.getInstance().post(
+event = 
 resp
@@ -1426,17 +1426,16 @@
 e.body()
-)
 ;%0A      
@@ -1446,31 +1446,9 @@
   %7D%0A
-            else %7B%0A    
+%0A
     
@@ -1489,53 +1489,15 @@
 ost(
-new Error(response.message()));%0A            %7D
+event);
 %0A   

simplify logic