package es.craftsmanship.toledo.katangapp.activities;

import es.craftsmanship.toledo.katangapp.models.BusStopResult;
import es.craftsmanship.toledo.katangapp.models.QueryResult;

import android.content.Intent;

import android.graphics.Color;

import android.os.Bundle;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.widget.Toast;

import java.util.List;

/**
 * @author Cristóbal Hermida
 * @author Manuel de la Peña
 */
public class ShowStopsActivity extends BaseGeoLocatedActivity {

    private List<BusStopResult> busStopResults;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_show_stops);

        Intent intent = getIntent();

        if (intent.hasExtra("queryResult") &&
            (intent.getSerializableExtra("queryResult") != null)) {

            QueryResult queryResult = (QueryResult) intent.getSerializableExtra("queryResult");

            busStopResults = queryResult.getResults();

            if (busStopResults.isEmpty()) {
                processEmptyResults();

                return;
            }

            recyclerView = (RecyclerView) findViewById(R.id.stops);

            swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

            initializeSwipeRefreshLayout();

            processResults(busStopResults);
        }
        else {
            processEmptyResults();
        }
    }

    private void initializeSwipeRefreshLayout() {
        swipeRefreshLayout.setColorSchemeColors(
            Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);

        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                processResults(busStopResults);
            }

        });

        final LinearLayoutManager layoutParams = new LinearLayoutManager(this);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                swipeRefreshLayout.setEnabled(
                    layoutParams.findFirstCompletelyVisibleItemPosition() == 0);
            }

        });
    }

    private void processEmptyResults() {
        Toast.makeText(
            ShowStopsActivity.this, getString(R.string.bustop_results_empty), Toast.LENGTH_SHORT
        ).show();

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);

        startActivity(intent);
    }

    private void processResults(List<BusStopResult> busStopResults) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new StopsAdapter(busStopResults));
    }

}
@@ -445,24 +445,61 @@
 get.Toast;%0A%0A
+import com.squareup.otto.Subscribe;%0A%0A
 import java.
@@ -509,16 +509,16 @@
 l.List;%0A
-
 %0A/**%0A * 
@@ -780,16 +780,444 @@
 ayout;%0A%0A
+    @Subscribe%0A    public void busStopsReceived(Error error) %7B%0A    %7D%0A%0A    @Subscribe%0A    public void busStopsReceived(QueryResult queryResult) %7B%0A        busStopResults.clear();%0A%0A        List%3CBusStopResult%3E results = queryResult.getResults();%0A%0A        busStopResults.addAll(results);%0A%0A        recyclerView.setLayoutManager(new LinearLayoutManager(this));%0A        recyclerView.setAdapter(new StopsAdapter(busStopResults));%0A    %7D%0A%0A
     @Ove
@@ -2032,37 +2032,36 @@
     
-processResults(busStop
+busStopsReceived(query
 Result
-s
 );%0A 
@@ -2506,56 +2506,8 @@
 ) %7B%0A
-                processResults(busStopResults);%0A
     
@@ -3214,24 +3214,24 @@
 ty.class);%0A%0A
+
         star
@@ -3260,219 +3260,5 @@
  %7D%0A%0A
-    private void processResults(List%3CBusStopResult%3E busStopResults) %7B%0A        recyclerView.setLayoutManager(new LinearLayoutManager(this));%0A        recyclerView.setAdapter(new StopsAdapter(busStopResults));%0A    %7D%0A%0A
 %7D

Implement the subscription, that will be used instead of the processResults