package es.mbarrben.aroundme.android.instagram;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.blinxbox.restig.auth.DialogError;
import com.blinxbox.restig.auth.InstagramAuthDialog;
import com.blinxbox.restig.auth.InstagramAuthDialog.DialogListener;
import com.blinxbox.restinstagram.DefaultInstagramClient;
import com.blinxbox.restinstagram.InstagramClient;
import com.blinxbox.restinstagram.InstagramCollection;
import com.blinxbox.restinstagram.Parameter;
import com.blinxbox.restinstagram.types.MediaPost;

public class Instagram {
    private static final String CLIENT_ID = "5b755d7249b6432bb37d2c5ff2dfaca3";
    private static final String CLIENT_SECRET = "9efc92c0af5a423391520c0ff3a12188";
    private static final String CALLBACK_URL = "aroundme://callback";

    private Activity mActivity;
    private String mAccessToken;
    private DialogListener mExternalDialogListener;
    private InstagramAuthDialog mAuthDialog;
    private InstagramClient mInstagramClient;

    private DialogListener mAuthDialoglistener = new DialogListener() {

        @Override
        public void onError(DialogError error) {
            Log.e(getClass().getName(), "Error authenticating: " + error.getMessage());

            if (mExternalDialogListener != null) {
                mExternalDialogListener.onError(error);
            }
        }

        @Override
        public void onComplete(Bundle values) {
            mAccessToken = values.getString("access_token");
            Log.i(getClass().getName(), "access token = " + mAccessToken);

            if (mExternalDialogListener != null) {
                mExternalDialogListener.onComplete(values);
            }
        }

        @Override
        public void onCancel() {
            Log.e(getClass().getName(), "User cancelled auth process");

            if (mExternalDialogListener != null) {
                mExternalDialogListener.onCancel();
            }
        }
    };

    public Instagram(Activity activity) {
        this.mActivity = activity;
    }

    public void setAuthDialogListener(DialogListener listener) {
        this.mAuthDialoglistener = listener;
    }

    public void authorize() {
        authorize(null);
    }

    public void authorize(DialogListener listener) {
        this.mExternalDialogListener = listener;
        mAuthDialog = new InstagramAuthDialog(mActivity, mAuthDialoglistener, Instagram.CLIENT_ID,
                Instagram.CALLBACK_URL);
        mAuthDialog.setCancelable(false);
        mAuthDialog.show();
    }

    public InstagramCollection<MediaPost> fetchNearMediaCollection(double latitude, double longitude, int distanceMetres) {
        final String endPoint = "media/search";
        final Parameter lat = new Parameter("lat", latitude);
        final Parameter lng = new Parameter("lng", longitude);
        final Parameter distance = new Parameter("distance", distanceMetres);
        return getInstagramClient().fetchCollection(endPoint, MediaPost.class, lat, lng, distance);
    }

    private InstagramClient getInstagramClient() {
        if (mInstagramClient == null) {
            mInstagramClient = new DefaultInstagramClient(Instagram.CLIENT_ID, mAccessToken);
        }

        return mInstagramClient;
    }
}

@@ -831,18 +831,17 @@
 ctivity 
-mA
+a
 ctivity;
@@ -860,18 +860,17 @@
  String 
-mA
+a
 ccessTok
@@ -900,18 +900,17 @@
 istener 
-mE
+e
 xternalD
@@ -956,18 +956,17 @@
 hDialog 
-mA
+a
 uthDialo
@@ -996,18 +996,17 @@
 mClient 
-mI
+i
 nstagram
@@ -1041,18 +1041,17 @@
 istener 
-mA
+a
 uthDialo
@@ -1250,34 +1250,33 @@
             if (
-mE
+e
 xternalDialogLis
@@ -1300,34 +1300,33 @@
                 
-mE
+e
 xternalDialogLis
@@ -1450,18 +1450,17 @@
         
-mA
+a
 ccessTok
@@ -1558,18 +1558,17 @@
 n = %22 + 
-mA
+a
 ccessTok
@@ -1581,34 +1581,33 @@
             if (
-mE
+e
 xternalDialogLis
@@ -1631,34 +1631,33 @@
                 
-mE
+e
 xternalDialogLis
@@ -1847,18 +1847,17 @@
     if (
-mE
+e
 xternalD
@@ -1897,18 +1897,17 @@
         
-mE
+e
 xternalD
@@ -2019,18 +2019,17 @@
    this.
-mA
+a
 ctivity 
@@ -2125,18 +2125,17 @@
    this.
-mA
+a
 uthDialo
@@ -2295,10 +2295,9 @@
 his.
-mE
+e
 xter
@@ -2326,34 +2326,33 @@
 stener;%0A        
-mA
+a
 uthDialog = new 
@@ -2371,18 +2371,17 @@
 hDialog(
-mA
+a
 ctivity,
@@ -2381,18 +2381,17 @@
 tivity, 
-mA
+a
 uthDialo
@@ -2421,32 +2421,16 @@
 IENT_ID,
-%0A               
  Instagr
@@ -2447,34 +2447,33 @@
 K_URL);%0A        
-mA
+a
 uthDialog.setCan
@@ -2496,18 +2496,17 @@
         
-mA
+a
 uthDialo
@@ -3067,18 +3067,17 @@
     if (
-mI
+i
 nstagram
@@ -3106,18 +3106,17 @@
         
-mI
+i
 nstagram
@@ -3172,18 +3172,17 @@
 ENT_ID, 
-mA
+a
 ccessTok
@@ -3216,10 +3216,9 @@
 urn 
-mI
+i
 nsta

rename fields