package org.gdg.frisbee.android.fragment;

import com.android.volley.Response;

import java.util.ArrayList;

import org.gdg.frisbee.android.api.ApiRequest;
import org.gdg.frisbee.android.api.model.Event;
import org.gdg.frisbee.android.api.model.SimpleEvent;
import org.gdg.frisbee.android.api.model.TaggedEvent;
import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.cache.ModelCache;
import org.gdg.frisbee.android.utils.Utils;
import org.joda.time.DateTime;

import org.gdg.frisbee.android.R;

import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;

public class DevFestEventFragment extends EventFragment {
    private static final String CACHE_KEY = "devfest2013";

    @Override
    void fetchEvents() {
        mStart = new DateTime(2013, 9, 13, 0, 0);
        mEnd = new DateTime(2013, 11, 10, 0, 0);

        setIsLoading(true);

        Response.Listener<ArrayList<TaggedEvent>> listener = new Response.Listener<ArrayList<TaggedEvent>>() {
            @Override
            public void onResponse(final ArrayList<TaggedEvent> events) {
                mEvents.addAll(events);

                App.getInstance().getModelCache().putAsync(CACHE_KEY, mEvents, DateTime.now().plusHours(2), new ModelCache.CachePutListener() {
                    @Override
                    public void onPutIntoCache() {
                        mAdapter.addAll(mEvents);
                        setIsLoading(false);
                    }
                });
            }
        };

        ApiRequest fetchEvents = mClient.getTaggedEventList(mStart, mEnd, "devfest", listener, mErrorListener);

        if (Utils.isOnline(getActivity())) {
            fetchEvents.execute();
        } else {
            App.getInstance().getModelCache().getAsync(CACHE_KEY, false, new ModelCache.CacheListener() {
                @Override
                public void onGet(Object item) {
                    ArrayList<TaggedEvent> events = (ArrayList<TaggedEvent>) item;

                    mAdapter.addAll(events);
                    setIsLoading(false);
                    Crouton.makeText(getActivity(), getString(R.string.cached_content), Style.INFO).show();
                }

                @Override
                public void onNotFound(String key) {
                    setIsLoading(false);
                    Crouton.makeText(getActivity(), getString(R.string.offline_alert), Style.ALERT).show();
                }
            });
        }
    }

}

@@ -100,16 +100,75 @@
 rayList;
+%0Aimport java.util.Collections;%0Aimport java.util.Comparator;
 %0A%0Aimport
@@ -453,16 +453,70 @@
 lCache;%0A
+import org.gdg.frisbee.android.utils.EventComparator;%0A
 import o
@@ -847,16 +847,105 @@
 st2013%22;
+%0A    private Comparator%3C? super TaggedEvent%3E mLocationComparator = new EventComparator();
 %0A%0A    @O
@@ -1231,32 +1231,32 @@
       @Override%0A
-
             publ
@@ -1309,24 +1309,87 @@
 %3E events) %7B%0A
+                Collections.sort(events, mLocationComparator);%0A
             

sort events by distance