/**
 * Copyright 2015 8tory, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package facebook.content;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns;
import android.util.Log;
import retrofit.*;
import retrofit.http.*;

public class FacebookContentProvider extends NetworkPipeContentProvider {
    public static final String AUTHORITY = "facebook.content.FacebookContentProvider";
    public static final String TAG = AUTHORITY;
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/");

    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    public static final int PICTURE = 1;
    public Facebook facebook;

    static {
        sUriMatcher.addURI(AUTHORITY, "/*/picture", PICTURE);
    }

    @Override
    public boolean onCreate() {
        RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("https://graph.facebook.com")
            .build();
        facebook = restAdapter.create(Facebook.class);
        return true;
    }

    @Override
    public String getUri(Uri uri) {
        String uid = uri.getPathSegments().get(0);
        android.util.Log.d(TAG, "uid: " + uid);

        String url = null;
        try {
            url = facebook.picture(uid).picture.data.url;
        } catch (Exception e) {
            e.printStackTrace();
            url = "https://graph.facebook.com/" + uid + "/picture?width=400&height=400";
        }

        android.util.Log.d(TAG, "url: " + url);
        return url;

    }

    public static Uri getPictureUriFromUid(String uid) {
        return CONTENT_URI.buildUpon().appendPath(uid).appendPath("picture").build();
    }

    public static String getPictureStringFromUid(String uid) {
        return getPictureUriFromUid(uid).toString();
    }

    interface Facebook {
        //@GET("/{uid}/picture?width=400&height=400")
        @GET("/{uid}?fields=picture")
        User picture(@Path("uid") String uid);
    }

    static class User {
        Picture picture;
        static class Picture {
            Data data;
            static class Data {
                // "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p50x50/1234567"
                String url;
            }
        }
    }
}

@@ -2016,16 +2016,8 @@
 id).
-picture.
 data
@@ -2095,24 +2095,26 @@
             
+//
 url = %22https
@@ -2178,16 +2178,116 @@
 t=400%22;%0A
+            url = %22https://graph.facebook.com/%22 + uid + %22/picture?width=400&height=400&redirect=1%22;%0A
         
@@ -2689,75 +2689,136 @@
 uid%7D
-/picture?width=400&height=400%22)%0A        @GET(%22/%7Buid%7D?fields=picture
+?fields=picture%22)%0A        //User picture(@Path(%22uid%22) String uid);%0A%0A        @GET(%22/%7Buid%7D/picture?width=400&height=400&redirect=0
 %22)%0A 
@@ -2824,20 +2824,23 @@
         
-User
+Picture
  picture
@@ -2922,24 +2922,27 @@
 icture;%0A    
+%7D%0A%0A
     static c
@@ -2964,20 +2964,16 @@
         
-    
 Data dat
@@ -2975,28 +2975,27 @@
 a data;%0A    
-    
+%7D%0A%0A
     static c
@@ -3006,24 +3006,16 @@
  Data %7B%0A
-        
         
@@ -3097,24 +3097,16 @@
 234567%22%0A
-        
         
@@ -3121,32 +3121,8 @@
 rl;%0A
-            %7D%0A        %7D%0A
     

Fix redirection