package com.cloudflare.realtimekit;

import android.util.Base64;

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 java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import com.cloudflare.realtimekit.RTKLogger;

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

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

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


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

    public static native int nativeMultiply(int a, int b);

    @ReactMethod(isBlockingSynchronousMethod = true)
    public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException {
      byte[] data = new byte[byteLength];
      SecureRandom random = new SecureRandom();

      random.nextBytes(data);

      return Base64.encodeToString(data, Base64.NO_WRAP);
    }

    /**
     * Enable debug logging for RealtimeKit Android module.
     * Logs will be visible in logcat with tag "RealtimeKit".
     */
    @ReactMethod
    public void enableLogging() {
        RTKLogger.enable();
    }

    /**
     * Disable debug logging for RealtimeKit Android module.
     */
    @ReactMethod
    public void disableLogging() {
        RTKLogger.disable();
    }
}
