package com.evetensorflowyolo;

import androidx.annotation.NonNull;

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.module.annotations.ReactModule;

import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.bridge.Callback;
import android.util.Log;

import com.facebook.react.bridge.NativeModule;

import com.facebook.react.bridge.ReactContext;

import java.util.Map;
import java.util.HashMap;

//cut unnecessary imports

import android.os.Bundle;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.*;
import android.media.Image;
import android.media.ImageReader;
import android.net.Uri;

import android.view.inputmethod.InputMethodManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.concurrent.Executors;

import java.io.File;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;

import java.io.ByteArrayOutputStream;


import android.os.Build;

import android.util.Log;
import java.io.InputStream;
import java.io.IOException;

import java.io.FileOutputStream;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;

@ReactModule(name = EveTensorflowYoloModule.NAME)
public class EveTensorflowYoloModule extends ReactContextBaseJavaModule {
    public static final String NAME = "EveTensorflowYoloModule";
    public static String imgPath = "";

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

    @Override
    @NonNull
    public String getName() {
        return NAME;
    }

    // start Python module
    // Example method
    // See https://reactnative.dev/docs/native-modules-android
    @ReactMethod
    public void multiply(double a, double b, Promise promise) {
        promise.resolve(a * b);
    }

    @ReactMethod
    public void printString(String str1, String str2, Callback callBack) {
        String word = str1 + str2 + "God is working on me";
        // String word = name + Lname;
        // Log.d("ModuleAModule", word);
        callBack.invoke(word);
    }

    @ReactMethod
    public void getImageString(String imgString, Callback callBack) {
        imgPath = imgString;
        // String word = name + Lname;
        Log.v("ModuleAModule", "I stored the image");
        callBack.invoke(imgPath);
    }
    
  
    @ReactMethod
    public void setImagePath(String imgString, Callback callBack) {
        imgPath = imgString;
        String result = "Good work";
        // convert Java object to Python object
        /* Python py = Python.getInstance();
        PyObject module = py.getModule("yolo");
        // PyObject item = PyObject.fromJava(imgPath);

        // call Python module object
        // String word = module.callAttr("main", item).toString()

        try {
            // result = module.callAttr("main", item).toString();
            result = module.callAttr("main").toString();
        } catch (Exception e) {
            result = "Bad result from python";
        }
        // pass path */

        callBack.invoke(result);

    }

    @ReactMethod
    public void runImage(String imgString, Callback callBack) {
        imgPath = imgString;
        String result = "Great work";
        // convert Java object to Python object
        /* Python py = Python.getInstance();
        PyObject module = py.getModule("yolo");
        PyObject item = PyObject.fromJava(imgPath);

        // call Python module object
        // String word = module.callAttr("main", item).toString()

        try {
            result = module.callAttr("main", item).toString();
            // result = module.callAttr("main").toString();
        } catch (Exception e) {
            result = "Bad result from python";
        }
        // pass path */

        callBack.invoke(result);

    }

    

    @ReactMethod
    public void launchBday(String msg, Callback callBack) {
   
        Activity activity = getCurrentActivity();
        String result = "Hi";
        if( activity == null) {
            Log.v("Love", "No activity");
        } else{
            Log.v("Love", "We have an activity");
        }

        
        Intent bday = new Intent(activity, DetectorActivity2.class);
        if( bday == null) {
            Log.v("Love", "We have an intent");
            result = "Keep trying";
        } else{
            Log.v("Love", "No intent");
            result = "You did it!";
        } 
        activity.startActivity(bday);
        callBack.invoke(result);
    }

       

    @ReactMethod
    public void usePTL(String imgString, Callback callBack) {
        imgPath = imgString;
        String result = "Read in image";
        /*
         * convert Java object to Python object
         * Python py = Python.getInstance();
         * PyObject module = py.getModule("yolo");
         * PyObject item = PyObject.fromJava(imgPath);
         * 
         * // call Python module object
         * //String word = module.callAttr("main", item).toString()
         * 
         * try {
         * result = module.callAttr("main", item).toString();
         * //result = module.callAttr("main").toString();
         * } catch (Exception e) {
         * result = "Bad result from python";
         * }
         * // pass path
         */
        // convert image to Bitmap

        // get Tensor
        // InputStream is = this.getResources().openRawResource(drawableResourceId);
        /*
         * Bitmap bit1 = BitmapFactory.decodeFile(imgPath);
         * if (bit1 == null) {
         * Log.v("Love", "Image is null");
         * }else{
         * result = "Read in Image";
         * }
         * 
         * Log.v("Love", "Made bitmap");
         * 
         * Log.v("Love", "Creating input tensor");
         * Tensor inputTensor1 = TensorImageUtils.bitmapToFloat32Tensor(bit1,
         * TensorImageUtils.TORCHVISION_NORM_MEAN_RGB,
         * TensorImageUtils.TORCHVISION_NORM_STD_RGB,
         * MemoryFormat.CHANNELS_LAST);
         * 
         * 
         * 
         * // running the model
         * Log.v("Love", "Created input tensor");
         * result = "Created input tensor";
         * /*Log.v("Love", "Creating output tensor");
         * /*Tensor outputTensor1 =
         * module.forward(IValue.from(inputTensor1)).toTensor();
         * Log.v("Love", "Created out tensor");
         * // getting tensor content as java array of floats
         * Log.v("Love", "Creating scores array");
         * final float[] scores1 = outputTensor1.getDataAsFloatArray();
         * Log.v("Love", "Created scores array");
         * 
         * Log.v("Love", "Creating max float");
         * // searching for the index with maximum score
         * float maxScore1 = -Float.MAX_VALUE;
         * Log.v("Love", "Created max float ");
         * Log.v("Love", "Creating max score");
         * int maxScoreIdx1 = -1;
         * for (int i = 0; i < scores1.length; i++) {
         * if (scores1[I] > maxScore1) {
         * maxScore1 = scores1[i];
         * maxScoreIdx1 = i;
         * }
         * }
         * Log.v("Love", "Got max score");
         * Log.v("Love", "Creating name");
         * try {
         * String name1 = ImageNetClasses.IMAGENET_CLASSES[maxScoreIdx1];
         * result = name1;
         * } catch (Exception e) {
         * 
         * Log.v("Love", "No name");
         * }
         */
        callBack.invoke(result);

    }

}
