package io.liaoyuan.reactnative.multipleimagepicker;

import android.util.Log;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.finalteam.rxgalleryfinal.RxGalleryFinal;
import cn.finalteam.rxgalleryfinal.imageloader.ImageLoaderType;
import cn.finalteam.rxgalleryfinal.rxbus.RxBusResultSubscriber;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageMultipleResultEvent;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageRadioResultEvent;
import cn.finalteam.rxgalleryfinal.bean.MediaBean;

/**
 * Created by tshen on 16/8/10.
 */
public class MultipleImagePicker extends ReactContextBaseJavaModule {

    private Map<String, MediaBean> assetsFromPath = new HashMap<String, MediaBean>();

    public MultipleImagePicker(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    private List<MediaBean> getMediaBeanListFromPathList(ReadableArray selectedPaths) {
        List<MediaBean> result = new ArrayList<MediaBean>();
        for (int i = 0; i < selectedPaths.size(); i++) {
            String currentPath = selectedPaths.getString(i);
            if (currentPath != null && this.assetsFromPath.containsKey(currentPath)) {
                MediaBean bean = this.assetsFromPath.get(currentPath);
                if (bean != null) {
                    result.add(bean);
                }
            }
        }
        return result;
    }

    @ReactMethod
    public void launchImageGallery(ReadableMap options, final Promise promise) {
        boolean single;
        int maxImagesCount;

        single = options.hasKey("single") && options.getBoolean("single");

        if (options.hasKey("maxImagesCount")) {
            maxImagesCount = options.getInt("maxImagesCount");
        } else {
            maxImagesCount = 9;
        }

        RxGalleryFinal rxGalleryFinal = RxGalleryFinal.with(getCurrentActivity()).image();

        if (single) {
            rxGalleryFinal.radio().imageLoader(ImageLoaderType.PICASSO).subscribe(new RxBusResultSubscriber<ImageRadioResultEvent>() {
                @Override
                protected void onEvent(ImageRadioResultEvent imageRadioResultEvent) throws Exception {
                    promise.resolve(imageRadioResultEvent.getResult().getOriginalPath());
                }
            }).openGallery();
        } else {
            if (options.hasKey("selectedPaths")) {
                ReadableArray selectedPaths = options.getArray("selectedPaths");
                if (selectedPaths != null) {
                    rxGalleryFinal.selected(this.getMediaBeanListFromPathList(selectedPaths));
                }
            }
            rxGalleryFinal.multiple().maxSize(maxImagesCount).imageLoader(ImageLoaderType.PICASSO).subscribe(new RxBusResultSubscriber<ImageMultipleResultEvent>() {
                @Override
                protected void onEvent(ImageMultipleResultEvent imageMultipleResultEvent) throws Exception {
                    List<MediaBean> list = imageMultipleResultEvent.getResult();
                    List<String> paths = new ArrayList<String>();
                    for (MediaBean bean : list) {
                        String path = bean.getOriginalPath();
                        Log.d("123123", path);
                        paths.add(path);
                        MultipleImagePicker.this.assetsFromPath.put(path, bean);
                    }
                    promise.resolve(paths);
                }
            }).openGallery();
        }
    }
}

@@ -73,16 +73,60 @@
 l.Log;%0A%0A
+import com.facebook.react.bridge.Arguments;%0A
 import c
@@ -413,24 +413,72 @@
 ReadableMap;
+%0Aimport com.facebook.react.bridge.WritableArray;
 %0A%0Aimport jav
@@ -525,35 +525,8 @@
 ap;%0A
-import java.util.Iterator;%0A
 impo
@@ -3546,50 +3546,51 @@
     
-List%3CString%3E paths = new ArrayList%3CString%3E
+WritableArray paths = Arguments.createArray
 ();%0A
@@ -3676,16 +3676,28 @@
 g path =
+ %22file://%22 +
  bean.ge
@@ -3741,64 +3741,24 @@
     
-Log.d(%22123123%22, path);%0A                        paths.add
+paths.pushString
 (pat

Fix Android return type.