/*
 * Copyright (c) 2015-present, Horcrux.
 * All rights reserved.
 *
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 */


package com.horcrux.svg;

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.ReadableMap;

class SvgViewModule extends ReactContextBaseJavaModule {
    SvgViewModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    static public void toDataURL(final int tag, final ReadableMap options, final Callback successCallback, final int attempt) {
        SvgView svg = SvgViewManager.getSvgViewByTag(tag);

        if (svg == null) {
            SvgViewManager.runWhenViewIsAvailable(tag, new Runnable() {
                @Override
                public void run() {
                    SvgView svg = SvgViewManager.getSvgViewByTag(tag);
                    if (svg == null) { // Should never happen
                        return;
                    }
                    svg.setToDataUrlTask(new Runnable() {
                        @Override
                        public void run() {
                            toDataURL(tag, options, successCallback, attempt + 1);
                        }
                    });
                }
            });
        } else if (!svg.isRendered()) {
            svg.setToDataUrlTask(new Runnable() {
                @Override
                public void run() {
                    toDataURL(tag, options, successCallback, attempt + 1);
                }
            });
        } else {
            if (options != null) {
                successCallback.invoke(
                    svg.toDataURL(
                        options.getInt("width"),
                        options.getInt("height")
                    )
                );
            } else {
                successCallback.invoke(svg.toDataURL());
            }
        }
    }

    @ReactMethod
    public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
        toDataURL(tag, options, successCallback, 0);
    }
}

@@ -480,16 +480,63 @@
 ableMap;
+%0Aimport com.facebook.react.bridge.UiThreadUtil;
 %0A%0Aclass 
@@ -893,24 +893,179 @@
  attempt) %7B%0A
+        UiThreadUtil.runOnUiThread(%0A                new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                
         SvgV
@@ -1112,16 +1112,32 @@
 (tag);%0A%0A
+                
         
@@ -1147,32 +1147,48 @@
 (svg == null) %7B%0A
+                
             SvgV
@@ -1251,32 +1251,48 @@
                 
+           
+     
 @Override%0A      
@@ -1277,32 +1277,48 @@
       @Override%0A
+                
                 
@@ -1349,32 +1349,48 @@
                 
+    
+            
 SvgView svg = Sv
@@ -1436,32 +1436,48 @@
                 
+             
+   
 if (svg == null)
@@ -1522,24 +1522,40 @@
             
+                
 return;%0A    
@@ -1562,34 +1562,64 @@
                 
+      
-%7D%0A
+          %7D%0A              
                 
@@ -1614,32 +1614,34 @@
                 
+  
 svg.setToDataUrl
@@ -1678,32 +1678,48 @@
                 
+                
 @Override%0A      
@@ -1728,32 +1728,48 @@
                 
+       
+         
 public void run(
@@ -1792,32 +1792,48 @@
                 
+               
+ 
 toDataURL(tag, o
@@ -1899,68 +1899,148 @@
     
-%7D%0A                    %7D);%0A                %7D%0A            %7D);%0A
+                %7D%0A                                    %7D);%0A                                %7D%0A                            %7D);%0A                
     
@@ -2079,32 +2079,48 @@
 ) %7B%0A            
+      
+          
 svg.setToDataUrl
@@ -2153,34 +2153,61 @@
             
+  
-@Override%0A
+              @Override%0A           
             
@@ -2202,32 +2202,37 @@
                 
+     
 public void run(
@@ -2227,32 +2227,48 @@
 ic void run() %7B%0A
+                
                 
@@ -2334,32 +2334,48 @@
                 
+   
+             
 %7D%0A            %7D)
@@ -2364,36 +2364,68 @@
   %7D%0A            
-%7D);%0A
+                %7D);%0A                
         %7D else %7B
@@ -2433,24 +2433,40 @@
             
+     
+           
 if (options 
@@ -2484,32 +2484,48 @@
                 
+              
+  
 successCallback.
@@ -2532,16 +2532,36 @@
 invoke(%0A
+                    
         
@@ -2603,32 +2603,56 @@
                 
+  
+                      
 options.getInt(%22
@@ -2676,32 +2676,56 @@
                 
+   
+                     
 options.getInt(%22
@@ -2753,18 +2753,47 @@
         
+    
-)%0A
+                )%0A         
         
@@ -2800,19 +2800,31 @@
         
-);%0A
+       );%0A     
         
@@ -2819,32 +2819,43 @@
                 
+           
 %7D else %7B%0A       
@@ -2855,32 +2855,48 @@
                 
+ 
+               
 successCallback.
@@ -2936,19 +2936,102 @@
     
+          
-%7D%0A  
       %7D
+%0A                        %7D%0A                    %7D%0A                %7D%0A        );
 %0A   

Fix SVG.toDataURL exception #948