package com.reactnativenavigation.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.reactnativenavigation.NavigationApplication;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class ImageLoader {

	public interface ImageLoadingListener {
		void onComplete(@NonNull Drawable drawable);

		void onError(Throwable error);
	}

	public void loadIcon(final Context context, final String uri, final ImageLoadingListener listener) {
        try {
            StrictMode.ThreadPolicy threadPolicy = adjustThreadPolicyDebug();
            
            InputStream is = openStream(context, uri);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
            listener.onComplete(drawable);

            restoreThreadPolicyDebug(threadPolicy);
        } catch (IOException e) {
            listener.onError(e);
        }
    }

    private StrictMode.ThreadPolicy adjustThreadPolicyDebug() {
        StrictMode.ThreadPolicy threadPolicy = null;
        if (NavigationApplication.instance.isDebug()) {
            threadPolicy = StrictMode.getThreadPolicy();
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        }
        return threadPolicy;
    }

    private void restoreThreadPolicyDebug(@Nullable StrictMode.ThreadPolicy threadPolicy) {
        if (NavigationApplication.instance.isDebug() && threadPolicy != null) {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    private static InputStream openStream(Context context, String uri) throws IOException {
        return uri.contains("http") ? remoteUrl(uri) : localFile(context, uri);
    }

    private static InputStream remoteUrl(String uri) throws IOException {
        return new URL(uri).openStream();
    }

    private static InputStream localFile(Context context, String uri) throws FileNotFoundException {
        return context.getContentResolver().openInputStream(Uri.parse(uri));
    }
}

@@ -372,16 +372,132 @@
 lable;%0A%0A
+import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;%0Aimport com.reactnativenavigation.BuildConfig;%0A
 import c
@@ -689,17 +689,20 @@
 ader %7B%0A%0A
-%09
+    
 public i
@@ -733,18 +733,24 @@
 tener %7B%0A
-%09%09
+        
 void onC
@@ -791,10 +791,16 @@
 );%0A%0A
-%09%09
+        
 void
@@ -830,13 +830,74 @@
 r);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    private static final String FILE_SCHEME = %22file%22;%0A%0A    
 publ
@@ -1007,24 +1007,657 @@
   try %7B%0A    
+        Drawable drawable = getDrawable(context, uri);%0A            listener.onComplete(drawable);%0A        %7D catch (IOException e) %7B%0A            listener.onError(e);%0A        %7D%0A    %7D%0A%0A    @NonNull%0A    private Drawable getDrawable(Context context, String source) throws IOException %7B%0A        if (BuildConfig.DEBUG) %7B%0A            return readJsDevImage(context, source);%0A        %7D else if (isLocalFile(Uri.parse(source))) %7B%0A            return loadFile(source);%0A        %7D else %7B%0A            return loadResource(source);%0A        %7D%0A    %7D%0A%0A    @NonNull%0A    private Drawable readJsDevImage(Context context, String source) throws IOException %7B%0A
         Stri
@@ -1722,25 +1722,8 @@
 ();%0A
-            %0A    
     
@@ -1759,34 +1759,33 @@
 am(context, 
-uri);%0A    
+source);%0A
         Bitm
@@ -1836,39 +1836,70 @@
         
-    Drawable drawable =
+restoreThreadPolicyDebug(threadPolicy);%0A        return
  new Bit
@@ -1951,176 +1951,489 @@
     
-   
+%7D%0A%0A
     
- listener.onComplete(drawable);%0A%0A            restoreThreadPolicyDebug(threadPolicy
+private boolean isLocalFile(Uri uri) %7B%0A        return FILE_SCHEME.equals(uri.getScheme());%0A    %7D%0A%0A    private Drawable loadFile(String uri) %7B%0A        Bitmap bitmap = BitmapFactory.decodeFile(uri);%0A        return new BitmapDrawable(NavigationApplication.instance.getResources(), bitmap
 );%0A    
+%7D%0A%0A
     
-%7D catch (IOException e) %7B%0A            listener.onError(e);%0A        %7D
+private static Drawable loadResource(String iconSource) %7B%0A        return ResourceDrawableIdHelper.getInstance().getResourceDrawable(NavigationApplication.instance, iconSource);
 %0A   

Support reading image resources on android