// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#include "WPIUtilJNI.hpp"

#include <jni.h>

#include "org_wpilib_util_WPIUtilJNI.h"
#include "wpi/util/RawFrame.hpp"
#include "wpi/util/RuntimeCheck.h"
#include "wpi/util/Synchronization.hpp"
#include "wpi/util/jni_util.hpp"
#include "wpi/util/print.hpp"
#include "wpi/util/timestamp.hpp"

using namespace wpi::util::java;

static bool mockTimeEnabled = false;
static uint64_t mockNow = 0;

static JException illegalArgEx;
static JException indexOobEx;
static JException interruptedEx;
static JException ioEx;
static JException nullPointerEx;
static JException msvcRuntimeEx;

static const JExceptionInit exceptions[] = {
    {"java/lang/IllegalArgumentException", &illegalArgEx},
    {"java/lang/IndexOutOfBoundsException", &indexOobEx},
    {"java/lang/InterruptedException", &interruptedEx},
    {"java/io/IOException", &ioEx},
    {"java/lang/NullPointerException", &nullPointerEx},
    {"org/wpilib/util/runtime/MsvcRuntimeException", &msvcRuntimeEx}};

void wpi::util::ThrowIllegalArgumentException(JNIEnv* env,
                                              std::string_view msg) {
  illegalArgEx.Throw(env, msg);
}

void wpi::util::ThrowIndexOobException(JNIEnv* env, std::string_view msg) {
  indexOobEx.Throw(env, msg);
}

void wpi::util::ThrowIOException(JNIEnv* env, std::string_view msg) {
  ioEx.Throw(env, msg);
}

void wpi::util::ThrowNullPointerException(JNIEnv* env, std::string_view msg) {
  nullPointerEx.Throw(env, msg);
}

extern "C" {

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
  JNIEnv* env;
  if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
    return JNI_ERR;
  }

  for (auto& c : exceptions) {
    *c.cls = JException(env, c.name);
    if (!*c.cls) {
      return JNI_ERR;
    }
  }

  return JNI_VERSION_1_6;
}

JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
  JNIEnv* env;
  if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
    return;
  }

  for (auto& c : exceptions) {
    c.cls->free(env);
  }
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    checkMsvcRuntime
 * Signature: ()V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_checkMsvcRuntime
  (JNIEnv* env, jclass)
{
  uint32_t foundMajor;
  uint32_t foundMinor;
  uint32_t expectedMajor;
  uint32_t expectedMinor;
  WPI_String runtimePath;

  if (!WPI_IsRuntimeValid(&foundMajor, &foundMinor, &expectedMajor,
                          &expectedMinor, &runtimePath)) {
    static jmethodID ctor =
        env->GetMethodID(msvcRuntimeEx, "<init>", "(IIIILjava/lang/String;)V");
    jstring jmsvcruntime =
        MakeJString(env, wpi::util::to_string_view(&runtimePath));
    jobject exception =
        env->NewObject(msvcRuntimeEx, ctor, foundMajor, foundMinor,
                       expectedMajor, expectedMinor, jmsvcruntime);
    WPI_FreeString(&runtimePath);
    env->Throw(static_cast<jthrowable>(exception));
  }
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    writeStderr
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_writeStderr
  (JNIEnv* env, jclass, jstring str)
{
  wpi::util::print(stderr, "{}", JStringRef{env, str}.str());
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    enableMockTime
 * Signature: ()V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_enableMockTime
  (JNIEnv*, jclass)
{
#ifdef __FIRST_SYSTEMCORE__
  wpi::util::print(stderr,
                   "WPIUtil: Mocking time is not available on systemcore\n");
#else
  mockTimeEnabled = true;
  wpi::util::SetNowImpl([] { return mockNow; });
#endif
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    disableMockTime
 * Signature: ()V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_disableMockTime
  (JNIEnv*, jclass)
{
  mockTimeEnabled = false;
  wpi::util::SetNowImpl(nullptr);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    setMockTime
 * Signature: (J)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_setMockTime
  (JNIEnv*, jclass, jlong time)
{
  mockNow = time;
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    now
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL
Java_org_wpilib_util_WPIUtilJNI_now
  (JNIEnv*, jclass)
{
  if (mockTimeEnabled) {
    return mockNow;
  } else {
    return wpi::util::Now();
  }
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    getSystemTime
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL
Java_org_wpilib_util_WPIUtilJNI_getSystemTime
  (JNIEnv*, jclass)
{
  return wpi::util::GetSystemTime();
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    makeEvent
 * Signature: (ZZ)I
 */
JNIEXPORT jint JNICALL
Java_org_wpilib_util_WPIUtilJNI_makeEvent
  (JNIEnv*, jclass, jboolean manualReset, jboolean initialState)
{
  return wpi::util::MakeEvent(manualReset, initialState);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    destroyEvent
 * Signature: (I)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_destroyEvent
  (JNIEnv*, jclass, jint eventHandle)
{
  wpi::util::DestroyEvent(eventHandle);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    setEvent
 * Signature: (I)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_setEvent
  (JNIEnv*, jclass, jint eventHandle)
{
  wpi::util::SetEvent(eventHandle);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    resetEvent
 * Signature: (I)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_resetEvent
  (JNIEnv*, jclass, jint eventHandle)
{
  wpi::util::ResetEvent(eventHandle);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    makeSemaphore
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL
Java_org_wpilib_util_WPIUtilJNI_makeSemaphore
  (JNIEnv*, jclass, jint initialCount, jint maximumCount)
{
  return wpi::util::MakeSemaphore(initialCount, maximumCount);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    destroySemaphore
 * Signature: (I)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_destroySemaphore
  (JNIEnv*, jclass, jint semHandle)
{
  wpi::util::DestroySemaphore(semHandle);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    releaseSemaphore
 * Signature: (II)Z
 */
JNIEXPORT jboolean JNICALL
Java_org_wpilib_util_WPIUtilJNI_releaseSemaphore
  (JNIEnv*, jclass, jint semHandle, jint releaseCount)
{
  return wpi::util::ReleaseSemaphore(semHandle, releaseCount);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    waitForObject
 * Signature: (I)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_waitForObject
  (JNIEnv* env, jclass, jint handle)
{
  if (!wpi::util::WaitForObject(handle)) {
    interruptedEx.Throw(env, "WaitForObject interrupted");
  }
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    waitForObjectTimeout
 * Signature: (ID)Z
 */
JNIEXPORT jboolean JNICALL
Java_org_wpilib_util_WPIUtilJNI_waitForObjectTimeout
  (JNIEnv* env, jclass, jint handle, jdouble timeout)
{
  bool timedOut;
  if (!wpi::util::WaitForObject(handle, timeout, &timedOut) && !timedOut) {
    interruptedEx.Throw(env, "WaitForObject interrupted");
    return false;
  }
  return timedOut;
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    waitForObjects
 * Signature: ([I)[I
 */
JNIEXPORT jintArray JNICALL
Java_org_wpilib_util_WPIUtilJNI_waitForObjects
  (JNIEnv* env, jclass, jintArray handles)
{
  JSpan<const jint> handlesArr{env, handles};
  wpi::util::SmallVector<WPI_Handle, 8> signaledBuf;
  signaledBuf.resize(handlesArr.size());
  std::span<const WPI_Handle> handlesArr2{
      reinterpret_cast<const WPI_Handle*>(handlesArr.data()),
      handlesArr.size()};

  auto signaled = wpi::util::WaitForObjects(handlesArr2, signaledBuf);
  if (signaled.empty()) {
    interruptedEx.Throw(env, "WaitForObjects interrupted");
    return nullptr;
  }
  return MakeJIntArray(env, signaled);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    waitForObjectsTimeout
 * Signature: ([ID)[I
 */
JNIEXPORT jintArray JNICALL
Java_org_wpilib_util_WPIUtilJNI_waitForObjectsTimeout
  (JNIEnv* env, jclass, jintArray handles, jdouble timeout)
{
  JSpan<const jint> handlesArr{env, handles};
  wpi::util::SmallVector<WPI_Handle, 8> signaledBuf;
  signaledBuf.resize(handlesArr.size());
  std::span<const WPI_Handle> handlesArr2{
      reinterpret_cast<const WPI_Handle*>(handlesArr.data()),
      handlesArr.size()};

  bool timedOut;
  auto signaled =
      wpi::util::WaitForObjects(handlesArr2, signaledBuf, timeout, &timedOut);
  if (signaled.empty() && !timedOut) {
    interruptedEx.Throw(env, "WaitForObjects interrupted");
    return nullptr;
  }
  return MakeJIntArray(env, signaled);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    allocateRawFrame
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL
Java_org_wpilib_util_WPIUtilJNI_allocateRawFrame
  (JNIEnv*, jclass)
{
  return reinterpret_cast<jlong>(new wpi::util::RawFrame);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    freeRawFrame
 * Signature: (J)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_freeRawFrame
  (JNIEnv*, jclass, jlong frame)
{
  delete reinterpret_cast<wpi::util::RawFrame*>(frame);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    getRawFrameDataPtr
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL
Java_org_wpilib_util_WPIUtilJNI_getRawFrameDataPtr
  (JNIEnv* env, jclass, jlong frame)
{
  auto* f = reinterpret_cast<wpi::util::RawFrame*>(frame);
  if (!f) {
    wpi::util::ThrowNullPointerException(env, "frame is null");
    return 0;
  }
  return reinterpret_cast<jlong>(f->data);
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    setRawFrameData
 * Signature: (JLjava/lang/Object;IIIII)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_setRawFrameData
  (JNIEnv* env, jclass, jlong frame, jobject data, jint size, jint width,
   jint height, jint stride, jint pixelFormat)
{
  auto* f = reinterpret_cast<wpi::util::RawFrame*>(frame);
  if (!f) {
    wpi::util::ThrowNullPointerException(env, "frame is null");
    return;
  }
  auto buf = env->GetDirectBufferAddress(data);
  if (!buf) {
    wpi::util::ThrowNullPointerException(env, "data is null");
    return;
  }
  // there's no way to free a passed-in direct byte buffer
  f->SetData(buf, size, env->GetDirectBufferCapacity(data), nullptr,
             [](void*, void*, size_t) {});
  f->width = width;
  f->height = height;
  f->stride = stride;
  f->pixelFormat = pixelFormat;
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    setRawFrameTime
 * Signature: (JJI)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_setRawFrameTime
  (JNIEnv* env, jclass, jlong frame, jlong time, jint timeSource)
{
  auto* f = reinterpret_cast<wpi::util::RawFrame*>(frame);
  if (!f) {
    wpi::util::ThrowNullPointerException(env, "frame is null");
    return;
  }
  f->timestamp = time;
  f->timestampSrc = timeSource;
}

/*
 * Class:     org_wpilib_util_WPIUtilJNI
 * Method:    setRawFrameInfo
 * Signature: (JIIIII)V
 */
JNIEXPORT void JNICALL
Java_org_wpilib_util_WPIUtilJNI_setRawFrameInfo
  (JNIEnv* env, jclass, jlong frame, jint size, jint width, jint height,
   jint stride, jint pixelFormat)
{
  auto* f = reinterpret_cast<wpi::util::RawFrame*>(frame);
  if (!f) {
    wpi::util::ThrowNullPointerException(env, "frame is null");
    return;
  }
  f->width = width;
  f->height = height;
  f->stride = stride;
  f->pixelFormat = pixelFormat;
}
}  // extern "C"
