package com.lumivote.lumivote.api;

import android.util.Log;

import com.lumivote.lumivote.api.huffpost_responses.republican_primary_polls.Estimate;
import com.lumivote.lumivote.api.huffpost_responses.republican_primary_polls.RepublicanPollResponse;
import com.lumivote.lumivote.bus.BusProvider;
import com.lumivote.lumivote.bus.HuffPostRepublicanPrimaryPollsEvent;
import com.squareup.otto.Bus;

import java.util.List;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.http.GET;
import retrofit.http.Path;
import retrofit.http.Query;

/**
 * Created by alex on 8/11/15.
 */
public class HuffPostPollRESTClient {

    private static final String API_URL = "http://elections.huffingtonpost.com/pollster/api";
    private static final HuffPostPollRESTClient restClient = new HuffPostPollRESTClient();

    private Bus eventBus;

    public List<Estimate> republicans;
    private RepublicanPollResponse republicanPrimaryPolls;

    private HuffPostPollRESTClient() {

    }

    public static HuffPostPollRESTClient getInstance() {
        restClient.eventBus = BusProvider.getInstance();
        restClient.eventBus.register(restClient);
        return restClient;
    }

    public interface HuffPostService {
        @GET("/{method}")
        void fetchRepublicanPolls(
                @Path("method") String method,
                @Query("topic") String topic,
                Callback<RepublicanPollResponse> republicanPollResponseCallback);
    }

    public void fetchRepublicanPrimaryPolls() {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(API_URL)
                .build();

        HuffPostService service = restAdapter.create(HuffPostService.class);
        service.fetchRepublicanPolls("charts", "2016-president-gop-primary",
                new Callback<RepublicanPollResponse>() {
                    @Override
                    public void success(RepublicanPollResponse republicanResponse, Response response) {
                        republicans = republicanResponse.getEstimates();
                        eventBus.post(new HuffPostRepublicanPrimaryPollsEvent(
                                HuffPostRepublicanPrimaryPollsEvent.Type.COMPLETED,
                                republicans));
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        Log.e("Connection failure: ", "Republican primary polls", error.getCause());
                    }
                });
    }
}

@@ -948,67 +948,8 @@
 ans;
-%0A    private RepublicanPollResponse republicanPrimaryPolls;
 %0A%0A  
@@ -1401,32 +1401,37 @@
        Callback%3C
+List%3C
 RepublicanPollRe
@@ -1437,16 +1437,17 @@
 esponse%3E
+%3E
  republi
@@ -1835,16 +1835,21 @@
 allback%3C
+List%3C
 Republic
@@ -1863,16 +1863,17 @@
 esponse%3E
+%3E
 () %7B%0A   
@@ -1939,16 +1939,21 @@
 success(
+List%3C
 Republic
@@ -1966,16 +1966,17 @@
 Response
+%3E
  republi
@@ -2066,16 +2066,23 @@
 esponse.
+get(1).
 getEstim

Fix for retrofit callback