/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.core.js.images;

import im.actor.runtime.js.fs.JsBlob;

public class JsImageResize {
    public static native void resize(JsBlob file, JsResizeListener resizeListener)/*-{
        console.log('resizing...')
        var img = document.createElement("img");
        img.style.cssText = "image-orientation: from-image;"

        var reader = new FileReader();
        reader.onload = function(e) {
            console.log('loaded...')
            img.src = e.target.result
            var canvas = document.createElement("canvas");

            var MAX_WIDTH = 90;
            var MAX_HEIGHT = 90;
            var width = img.width;
            var height = img.height;

            if (width > height) {
                if (width > MAX_WIDTH) {
                    height *= MAX_WIDTH / width;
                    width = MAX_WIDTH;
                }
            } else {
                if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;
                    height = MAX_HEIGHT;
                }
            }
            canvas.width = width;
            canvas.height = height;

            var ctx = canvas.getContext("2d");

            ctx.fillStyle = 'white';
            ctx.fillRect(0, 0, width, height);

            ctx.drawImage(img, 0, 0, width, height);

            var compressedImage = canvas.toDataURL("image/jpeg", 0.55);

            console.log('completed... ' + compressedImage)
            resizeListener.@im.actor.core.js.images.JsResizeListener::onResized(*)(compressedImage,
                  width, height, img.width, img.height);
        }
        reader.readAsDataURL(file);
    }-*/;
}

@@ -242,16 +242,102 @@
 er)/*-%7B%0A
+        var reader = new FileReader();%0A%0A        reader.onload = function(event) %7B%0A    
         
@@ -353,22 +353,24 @@
 og('
-resizing
+loaded
 ...')%0A
+    
     
@@ -387,39 +387,25 @@
 g = 
-document.createElement(%22img%22);%0A
+new Image();%0A    
     
@@ -460,16 +460,17 @@
 -image;%22
+;
 %0A%0A      
@@ -475,53 +475,15 @@
     
-var reader = new FileReader();%0A        reader
+    img
 .onl
@@ -498,20 +498,28 @@
 nction(e
-) %7B%0A
+vent) %7B%0A    
         
@@ -539,58 +539,27 @@
 og('
-loaded
+resizing
 ...')
+;
 %0A    
-        img.src = e.target.result%0A
     
@@ -609,25 +609,28 @@
 (%22canvas%22);%0A
-%0A
+    
             
@@ -653,32 +653,36 @@
 90;%0A            
+  
+  
 var MAX_HEIGHT =
@@ -694,24 +694,28 @@
             
+    
 var width = 
@@ -718,19 +718,28 @@
 h = 
-img
+event.target
 .width;%0A
     
@@ -730,24 +730,28 @@
 rget.width;%0A
+    
             
@@ -763,19 +763,28 @@
 eight = 
-img
+event.target
 .height;
@@ -789,32 +789,36 @@
 t;%0A%0A            
+  
+  
 if (width %3E heig
@@ -819,24 +819,28 @@
 %3E height) %7B%0A
+    
             
@@ -884,24 +884,28 @@
             
+ 
+   
 height *= MA
@@ -913,32 +913,36 @@
 _WIDTH / width;%0A
+    
                 
@@ -972,34 +972,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D el
@@ -1023,24 +1023,28 @@
             
+    
 if (height %3E
@@ -1054,24 +1054,28 @@
 X_HEIGHT) %7B%0A
+    
             
@@ -1128,24 +1128,28 @@
             
+    
 height = MAX
@@ -1165,34 +1165,42 @@
                 
+   
+ 
 %7D%0A
+    
             %7D%0A  
@@ -1205,24 +1205,28 @@
             
+   
+ 
 canvas.width
@@ -1227,32 +1227,36 @@
 .width = width;%0A
+    
             canv
@@ -1284,24 +1284,28 @@
             
+  
+  
 var ctx = ca
@@ -1332,32 +1332,36 @@
 );%0A%0A            
+    
 ctx.fillStyle = 
@@ -1373,32 +1373,36 @@
 e';%0A            
+  
+  
 ctx.fillRect(0, 
@@ -1412,33 +1412,36 @@
 width, height);%0A
-%0A
+    
             ctx.
@@ -1450,19 +1450,28 @@
 awImage(
-img
+event.target
 , 0, 0, 
@@ -1495,24 +1495,28 @@
             
+  
+  
 var compress
@@ -1568,32 +1568,36 @@
 );%0A%0A            
+    
 console.log('com
@@ -1609,30 +1609,16 @@
 d...
- ' + compressedImage)%0A
+');%0A    
     
@@ -1727,24 +1727,26 @@
             
+  
 width, heigh
@@ -1752,22 +1752,40 @@
 ht, 
-img.width, img
+event.target.width, event.target
 .hei
@@ -1790,33 +1790,94 @@
 eight);%0A        
+  
-%7D
+  %7D;%0A%0A            img.src = event.target.result;%0A        %7D;%0A
 %0A        reader.

Update JsImageResize.java