package com.bourke.glimmr.fragments.photoset;

import android.content.Context;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.util.Log;

import com.bourke.glimmr.common.Constants;
import com.bourke.glimmr.event.Events.IPhotoListReadyListener;
import com.bourke.glimmr.fragments.base.PhotoGridFragment;
import com.bourke.glimmr.tasks.LoadPhotosetTask;

import com.googlecode.flickrjandroid.people.User;
import com.googlecode.flickrjandroid.photosets.Photoset;
import com.googlecode.flickrjandroid.photos.Photo;

import java.util.List;

public class PhotosetGridFragment extends PhotoGridFragment
        implements IPhotoListReadyListener {

    private static final String TAG = "Glimmr/PhotosetGridFragment";

    private Photoset mPhotoset;
    private LoadPhotosetTask mTask;

    public static PhotosetGridFragment newInstance(Photoset photoset) {
        PhotosetGridFragment newFragment = new PhotosetGridFragment();
        newFragment.mPhotoset = photoset;
        return newFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mShowDetailsOverlay = false;
    }

    /**
     * Once the parent binds the adapter it will trigger cacheInBackground
     * for us as it will be empty when first bound.  So we don't need to
     * override startTask().
     */
    @Override
    protected boolean cacheInBackground() {
        startTask(mPage++);
        return mMorePages;
    }

    private void startTask(int page) {
        super.startTask();
        mTask = new LoadPhotosetTask(this, this, mPhotoset, page);
        mTask.execute(mOAuth);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPhotoset != null) {
            SharedPreferences sp = mActivity.getSharedPreferences(
                    Constants.PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(Constants.PHOTOSET_FRAGMENT_SET_ID,
                    mPhotoset.getId());
            editor.commit();
        }
        if (mTask != null) {
            mTask.cancel(true);
        }
    }

    @Override
    public void onPhotosReady(List<Photo> photos) {
        User owner = mPhotoset.getOwner();
        if (owner != null) {
            for (Photo p : photos) {
                p.setOwner(owner);
                p.setUrl(String.format("%s%s%s%s", "http://flickr.com/photos/",
                            owner.getId(), "/", p.getId()));
            }
        }
        if (photos != null && photos.isEmpty()) {
            mMorePages = false;
        }
        super.onPhotosReady(photos);
    }

    @Override
    protected void refresh() {
        super.refresh();
        mTask = new LoadPhotosetTask(this, this, mPhotoset, mPage);
        mTask.execute(mOAuth);
    }

    @Override
    public String getNewestPhotoId() {
        SharedPreferences prefs = mActivity.getSharedPreferences(Constants
                .PREFS_NAME, Context.MODE_PRIVATE);
        String newestId = prefs.getString(
                Constants.NEWEST_PHOTOSET_PHOTO_ID, null);
        return newestId;
    }

    @Override
    public void storeNewestPhotoId(Photo photo) {
        SharedPreferences prefs = mActivity.getSharedPreferences(Constants
                .PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(Constants.NEWEST_PHOTOSET_PHOTO_ID, photo.getId());
        editor.commit();
        if (Constants.DEBUG)
            Log.d(getLogTag(), "Updated most recent photoset photo id to " +
                photo.getId());
    }

    @Override
    protected String getLogTag() {
        return TAG;
    }
}

@@ -2240,32 +2240,69 @@
 hoto%3E photos) %7B%0A
+        if (mPhotoset != null) %7B%0A    
         User own
@@ -2332,24 +2332,28 @@
 ();%0A        
+    
 if (owner !=
@@ -2353,32 +2353,36 @@
 wner != null) %7B%0A
+    
             for 
@@ -2414,24 +2414,28 @@
             
+    
 p.setOwner(o
@@ -2441,16 +2441,20 @@
 owner);%0A
+    
         
@@ -2495,16 +2495,48 @@
 %25s%25s%25s%22,
+%0A                               
  %22http:/
@@ -2557,16 +2557,20 @@
 otos/%22,%0A
+    
         
@@ -2634,26 +2634,33 @@
         
+ 
-%7D%0A
+   %7D%0A   
         
 %7D%0A      
@@ -2643,34 +2643,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         if (phot
@@ -2720,16 +2720,20 @@
         
+    
 mMorePag
@@ -2740,24 +2740,38 @@
 es = false;%0A
+            %7D%0A
         %7D%0A  

Handle null photolist in PhotosetGridFragment