package io.scrollback.neighborhoods;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;

public class ImageChooserModule extends ReactContextBaseJavaModule {

    public final int PICK_IMAGE = 3500;

    private final String CALLBACK_TYPE_SUCCESS = "success";
    private final String CALLBACK_TYPE_ERROR = "error";
    private final String CALLBACK_TYPE_CANCEL = "cancel";

    private Context mActivityContext;
    private Callback mPickerCallback;

    public ImageChooserModule(ReactApplicationContext reactContext, Context activityContext) {
        super(reactContext);

        mActivityContext = activityContext;
    }

    @Override
    public String getName() {
        return "ImageChooserModule";
    }

    private void consumeCallback(String type, WritableMap map) {
        if (mPickerCallback != null) {
            map.putString("type", type);

            mPickerCallback.invoke(map);
            mPickerCallback = null;
        }
    }

    private String getPathFromUri(Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};

        CursorLoader loader = new CursorLoader(mActivityContext, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        String result = cursor.getString(column_index);

        cursor.close();

        return result;
    }

    @ReactMethod
    public void pickImage(final Callback callback) {
        final Intent galleryIntent = new Intent(Intent.ACTION_PICK);

        galleryIntent.setType("image/*");

        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Pick an image");

        ((Activity) mActivityContext).startActivityForResult(chooserIntent, PICK_IMAGE);

        mPickerCallback = callback;
    }

    public boolean handleActivityResult(final int requestCode, final int resultCode, final Intent intent) {
        if (requestCode == PICK_IMAGE) {
            if (mPickerCallback != null) {
                WritableMap map = Arguments.createMap();

                if (resultCode == Activity.RESULT_CANCELED) {
                    consumeCallback(CALLBACK_TYPE_CANCEL, map);
                } else if (resultCode == Activity.RESULT_OK) {
                    Uri uri = intent.getData();

                    if (uri != null) {
                        BitmapFactory.Options options = new BitmapFactory.Options();

                        options.inJustDecodeBounds = true;

                        BitmapFactory.decodeFile(getPathFromUri(uri), options);

                        map.putInt("height", options.outHeight);
                        map.putInt("width", options.outWidth);
                        map.putString("uri", uri.toString());

                        consumeCallback(CALLBACK_TYPE_SUCCESS, map);
                    } else {
                        consumeCallback(CALLBACK_TYPE_ERROR, map);
                    }
                }
            }

            return true;
        }

        return false;
    }
}

@@ -602,16 +602,17 @@
 leMap;%0A%0A
+%0A
 public c
@@ -1518,24 +1518,133 @@
 ntentUri) %7B%0A
+        if (!contentUri.getScheme().equals(%22content%22)) %7B%0A            return contentUri.getPath();%0A        %7D%0A%0A
         Stri
@@ -1652,16 +1652,22 @@
 g%5B%5D proj
+ection
  = %7BMedi
@@ -1775,16 +1775,22 @@
 ri, proj
+ection
 , null, 
@@ -2081,24 +2081,693 @@
 ult;%0A    %7D%0A%0A
+    private String getNameFromUri(Uri contentUri) %7B%0A        if (!contentUri.getScheme().equals(%22content%22)) %7B%0A            return contentUri.getLastPathSegment();%0A        %7D%0A%0A        String%5B%5D projection = %7BMediaStore.MediaColumns.DISPLAY_NAME%7D;%0A%0A        Cursor metaCursor = mActivityContext.getContentResolver().query(contentUri, projection, null, null, null);%0A%0A        if (metaCursor != null) %7B%0A            try %7B%0A                if (metaCursor.moveToFirst()) %7B%0A                    return metaCursor.getString(0);%0A                %7D%0A            %7D finally %7B%0A                metaCursor.close();%0A            %7D%0A        %7D%0A%0A        return contentUri.getLastPathSegment();%0A    %7D%0A%0A
     @ReactMe
@@ -4105,16 +4105,84 @@
 ring());
+%0A                        map.putString(%22name%22, getNameFromUri(uri));
 %0A%0A      

Support file:// URIs and returns file names