package eu.crowtec.rnpixelcolor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.io.File;

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;
import fr.bamlab.rnimageresizer.ImageResizer;

import java.io.IOException;


class RNPixelColorModule extends ReactContextBaseJavaModule {
    private Context context;

    public RNPixelColorModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.context = reactContext;
    }

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

    @ReactMethod
    public void getHex(String path, ReadableMap options, final Callback callback) {
        Bitmap image;
        if (path.startsWith("data:") || path.startsWith("file:")) {
          image = ImageResizer.loadBitmapFromBase64(path);
        } else {
          Uri imageUrl = Uri.parse(path);
          File file = ImageResizer.getFileFromUri(this.context, imageUrl);
          BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
          bitmapOptions.inJustDecodeBounds = true;
          try {
            image = ImageResizer.loadBitmap(this.context, file.getPath(), bitmapOptions);
          } catch(IOException e){
            callback.invoke("Error parsing bitmap. Error: " + e.getMessage());
            return;
          }
        }

        if (image == null) {
          callback.invoke("Could not create image from given path.");
          return;
        }

        int x = options.getInt("x");
        int y = options.getInt("y");

        if (options.hasKey("width") && options.hasKey("height")) { //TODO check what this is for
          int scaledWidth = options.getInt("width");
          int scaledHeight = options.getInt("height");

          float originalWidth = image.getWidth();
          float originalHeight = image.getHeight();

          x = x * ((int)originalWidth / scaledWidth);
          y = y * ((int)originalHeight / scaledHeight);

        }

        int color = colorAtPixel(image, x, y);

        callback.invoke(colorToHexString(color));
    }

    private int colorAtPixel(Bitmap bitmap, int x, int y) {
      return bitmap.getPixel(x, y);
    }

    private String colorToHexString(int color) {
        return String.format("#%06X", (0xFFFFFF & color));
    }


}

@@ -1553,16 +1553,22 @@
 essage()
+, null
 );%0A     
@@ -1701,16 +1701,22 @@
 n path.%22
+, null
 );%0A     
@@ -1754,16 +1754,22 @@
  int x =
+ (int)
  options
@@ -1772,19 +1772,22 @@
 ions.get
-Int
+Double
 (%22x%22);%0A 
@@ -1800,16 +1800,22 @@
  int y =
+ (int)
  options
@@ -1818,19 +1818,22 @@
 ions.get
-Int
+Double
 (%22y%22);%0A%0A
@@ -1902,38 +1902,8 @@
 )) %7B
- //TODO check what this is for
 %0A   
@@ -2010,36 +2010,34 @@
 t%22);%0A%0A          
-floa
+in
 t originalWidth 
@@ -2070,12 +2070,10 @@
     
-floa
+in
 t or
@@ -2122,29 +2122,24 @@
    x = x * (
-(int)
 originalWidt
@@ -2175,21 +2175,16 @@
  = y * (
-(int)
 original
@@ -2291,16 +2291,22 @@
 .invoke(
+null, 
 colorToH

fix callback