/**
 * Copyright 2009 Joe LaPenna
 */

package com.joelapenna.foursquared;

import com.joelapenna.foursquare.types.Checkin;
import com.joelapenna.foursquare.types.Group;
import com.joelapenna.foursquare.types.Mayor;
import com.joelapenna.foursquare.types.User;
import com.joelapenna.foursquare.types.Venue;
import com.joelapenna.foursquared.app.LoadableListActivity;
import com.joelapenna.foursquared.util.Comparators;
import com.joelapenna.foursquared.widget.CheckinListAdapter;
import com.joelapenna.foursquared.widget.MayorListAdapter;
import com.joelapenna.foursquared.widget.SeparatedListAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import java.util.Collections;
import java.util.Observable;
import java.util.Observer;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class VenueCheckinsActivity extends LoadableListActivity {
    public static final String TAG = "VenueCheckinsActivity";
    public static final boolean DEBUG = FoursquaredSettings.DEBUG;

    private Observer mParentDataObserver = new ParentDataObserver();
    private SeparatedListAdapter mListAdapter;

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

        mListAdapter = new SeparatedListAdapter(this);
        getListView().setAdapter(mListAdapter);
        getListView().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object item = parent.getAdapter().getItem(position);
                if (item instanceof Checkin) {
                    Checkin checkin = (Checkin)item;
                    startItemActivity(checkin.getUser());
                } else if (item instanceof Mayor) {
                    Mayor mayor = (Mayor)item;
                    startItemActivity(mayor.getUser());
                }
            }
        });

        VenueActivity parent = (VenueActivity)getParent();

        if (parent.venueObservable.getVenue() != null) {
            mParentDataObserver.update(parent.venueObservable, parent.venueObservable.getVenue());
        } else {
            ((VenueActivity)getParent()).venueObservable.addObserver(mParentDataObserver);
        }
    }

    @Override
    public int getNoSearchResultsStringId() {
        return R.string.no_checkins_be_the_first;
    }

    private void putCheckinsInAdapter(Group<Checkin> checkins) {
        if (DEBUG) Log.d(TAG, "Setting checkins.");
        CheckinListAdapter adapter = new CheckinListAdapter(this, //
                ((Foursquared)getApplication()).getRemoteResourceManager());
        adapter.setGroup(checkins);
        mListAdapter.addSection("Recent Checkins", adapter);
    }

    private void putMayorInAdapter(final Mayor mayor) {
        if (DEBUG) Log.d(TAG, "Setting mayor.");
        Group<Mayor> mayors = new Group<Mayor>();
        mayors.add(mayor);
        MayorListAdapter adapter = new MayorListAdapter(this, //
                ((Foursquared)getApplication()).getRemoteResourceManager());
        adapter.setGroup(mayors);
        mListAdapter.addSection("Mayor", adapter);
    }

    private void startItemActivity(User user) {
        if (DEBUG) Log.d(TAG, "firing venue activity for venue");
        Intent intent = new Intent(VenueCheckinsActivity.this, UserActivity.class);
        intent.putExtra(UserActivity.EXTRA_USER, user.getId());
        startActivity(intent);
    }

    private final class ParentDataObserver implements Observer {
        private boolean observed = false;

        @SuppressWarnings("unchecked")
        @Override
        public void update(Observable observable, Object data) {
            if (DEBUG) Log.d(TAG, "Received update from: " + observable.toString());
            VenueActivity parent = (VenueActivity)getParent();
            Venue venue = parent.venueObservable.getVenue();

            // For each checkin, lets get the user because that is what we're concerned about here.
            Group<Checkin> checkins = venue.getCheckins();

            if (!observed && venue != null && checkins != null) {
                observed = true;

                boolean hasMayor = venue.getStats() != null && venue.getStats().getMayor() != null;
                if (hasMayor) {
                    if (DEBUG) Log.d(TAG, "Found mayor, pushing to adapter.");
                    putMayorInAdapter(venue.getStats().getMayor());
                }

                boolean hasCheckins = venue.getCheckins().size() > 0;
                if (hasCheckins) {
                    if (DEBUG) Log.d(TAG, "Found checkins, pushing to adapter.");
                    Collections.sort(checkins, Comparators.getCheckinRecencyComparator());
                    putCheckinsInAdapter(checkins);
                }

                if (hasMayor || hasCheckins) {
                    mListAdapter.notifyDataSetInvalidated();
                } else {
                    setEmptyView();
                }
            }
        }
    }
}

@@ -3692,50 +3692,8 @@
 er %7B
-%0A        private boolean observed = false;
 %0A%0A  
@@ -4026,272 +4026,8 @@
 );%0A%0A
-            // For each checkin, lets get the user because that is what we're concerned about here.%0A            Group%3CCheckin%3E checkins = venue.getCheckins();%0A%0A            if (!observed && venue != null && checkins != null) %7B%0A                observed = true;%0A%0A    
     
@@ -4122,36 +4122,32 @@
 ll;%0A            
-    
 if (hasMayor) %7B%0A
@@ -4154,36 +4154,32 @@
                 
-   
- 
 if (DEBUG) Log.d
@@ -4213,36 +4213,32 @@
  to adapter.%22);%0A
-    
                 
@@ -4293,35 +4293,86 @@
             
-    
 %7D%0A%0A    
+        Group%3CCheckin%3E checkins = venue.getCheckins();%0A
             
@@ -4384,32 +4384,63 @@
 an hasCheckins =
+ venue.getCheckins() != null &&
  venue.getChecki
@@ -4456,20 +4456,16 @@
 () %3E 0;%0A
-    
         
@@ -4495,36 +4495,32 @@
                 
-   
- 
 if (DEBUG) Log.d
@@ -4557,36 +4557,32 @@
  to adapter.%22);%0A
-    
                 
@@ -4668,20 +4668,16 @@
         
-  
-  
 putCheck
@@ -4704,36 +4704,32 @@
 s);%0A            
-    
 %7D%0A%0A             
@@ -4719,36 +4719,32 @@
  %7D%0A%0A            
- 
-   
 if (hasMayor %7C%7C 
@@ -4750,36 +4750,32 @@
  hasCheckins) %7B%0A
-    
                 
@@ -4823,28 +4823,24 @@
             
-    
 %7D else %7B%0A   
@@ -4856,25 +4856,54 @@
     
-    setEmptyView(
+if (DEBUG) Log.d(TAG, %22No data. Setting empty%22
 );%0A 
@@ -4909,33 +4909,47 @@
                 
-%7D
+setEmptyView();
 %0A            %7D%0A 

Fix endless loading bug in VenueCheckinsActivity.