#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <dlfcn.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <android/log.h>
#include <sys/stat.h>
#include <jni.h>

#define LOG_TAG "CameraHookZygisk"
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

#define HOOK_LIB_PATH "/data/local/camera_magic/libcamera_hook.so"

static void* g_hook_handle = nullptr;
static bool g_hook_loaded = false;
static bool g_in_zygote = false;

static bool is_camera_process() {
    char cmdline[256] = {0};
    int fd = open("/proc/self/cmdline", O_RDONLY);
    if (fd < 0) return false;
    ssize_t n = read(fd, cmdline, sizeof(cmdline) - 1);
    close(fd);
    if (n <= 0) return false;
    return (strstr(cmdline, "cameraserver") != nullptr ||
            strstr(cmdline, "camera.provider") != nullptr ||
            strstr(cmdline, "android.hardware.camera") != nullptr);
}

static void load_hook() {
    if (g_hook_loaded) return;
    g_hook_loaded = true;
    struct stat st;
    if (stat(HOOK_LIB_PATH, &st) != 0) {
        ALOGE("Hook library not found: %s", HOOK_LIB_PATH);
        return;
    }
    g_hook_handle = dlopen(HOOK_LIB_PATH, RTLD_NOW | RTLD_LOCAL);
    if (!g_hook_handle) {
        ALOGE("dlopen(%s) failed: %s", HOOK_LIB_PATH, dlerror());
        return;
    }
    ALOGI("Hook library loaded in PID %d", getpid());
}

static void post_fork_child() {
    if (is_camera_process()) {
        ALOGI("Camera process detected (PID %d), loading hook", getpid());
        load_hook();
    }
}

__attribute__((constructor))
static void zygisk_init() {
    char cmdline[256] = {0};
    int fd = open("/proc/self/cmdline", O_RDONLY);
    if (fd >= 0) {
        read(fd, cmdline, sizeof(cmdline) - 1);
        close(fd);
    }
    if (strstr(cmdline, "zygote") || strstr(cmdline, "zygote64")) {
        g_in_zygote = true;
        ALOGI("Loaded in Zygote (PID %d), registering atfork", getpid());
        pthread_atfork(nullptr, nullptr, post_fork_child);
    } else if (is_camera_process()) {
        ALOGI("Loaded directly in camera process (PID %d)", getpid());
        load_hook();
    }
}

extern "C" {

__attribute__((visibility("default")))
void zygisk_module_entry(void* api, JNIEnv* env) {
    (void)api;
    (void)env;
}

__attribute__((visibility("default")))
void pre_app_specialize(void* module, void* args) {
    (void)module;
    (void)args;
}

__attribute__((visibility("default")))
void post_app_specialize(void* module, const void* args) {
    (void)module;
    (void)args;
    if (is_camera_process()) {
        ALOGI("Camera app detected via post_app_specialize (PID %d)", getpid());
        load_hook();
    }
}

__attribute__((visibility("default")))
void pre_server_specialize(void* module, void* args) {
    (void)module;
    (void)args;
}

__attribute__((visibility("default")))
void post_server_specialize(void* module, const void* args) {
    (void)module;
    (void)args;
    ALOGI("Server specialized (PID %d), loading hook", getpid());
    load_hook();
}

}
