#include "libkn_api.h"
#include "napi/native_api.h"
#include "hilog/log.h"
#include <rawfile/raw_file_manager.h>
#include <dlfcn.h>

// 避免工程侧未定义 LOG_DOMAIN 时编译失败
#ifndef LOG_DOMAIN
#define LOG_DOMAIN 0x0000
#endif

static napi_value MainArkUIViewController(napi_env env, napi_callback_info info) {
    return reinterpret_cast<napi_value>(MainArkUIViewController(env));
}



static napi_value nativeCreateRootNode(napi_env env, napi_callback_info info) {
    size_t argc = 2;
    napi_value argv[2];
    napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
    return reinterpret_cast<napi_value>(createRootRenderNode());
}

static napi_value nativeOHRenderNodeDraw(napi_env env, napi_callback_info info) {
    size_t argc = 2;
    napi_value argv[2];
    napi_status status;

    status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
    if (status != napi_ok || argc < 2) {
        napi_throw_type_error(env, nullptr, "Expected canvas argument");
        return nullptr;
    }
    renderNodeDraw(env, argv[0], argv[1]);
    return nullptr;
}

static napi_value nativeOHRenderNotifyRedraw(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value argv[1];
    napi_status status;

    status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        napi_throw_type_error(env, nullptr, "Expected renderNode argument");
        return nullptr;
    }
    renderNodeNotifyRedraw(env, argv[0]);
    return nullptr;
}

static napi_value nativeRegisterNodeConstructor(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value argv[1];
    napi_get_cb_info(env, info, &argc, argv, NULL, NULL);

    if (argc < 1) {
        return nullptr;
    }
    setNodeConstructor(env, argv[0]);
    return nullptr;
}

static napi_value nativeSetPixelRatio(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value argv[1];
    napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
    double ratio = 1.0;
    napi_get_value_double(env, argv[0], &ratio);
    setPixelRatio(ratio);
    return nullptr;
}

static napi_value nativeRegisterNodeStatusModifyConstructor(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value argv[1];
    napi_get_cb_info(env, info, &argc, argv, NULL, NULL);

    if (argc < 1) {
        return nullptr;
    }
    buildInstance(env, argv[0]);
    return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    androidx_compose_ui_arkui_init(env, exports);
    napi_property_descriptor desc[] = {
        {"MainArkUIViewController", nullptr, MainArkUIViewController, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nativeCreateRootNode", nullptr, nativeCreateRootNode, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nativeOHRenderNodeDraw", nullptr, nativeOHRenderNodeDraw, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nativeOHRenderNotifyRedraw", nullptr, nativeOHRenderNotifyRedraw, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nativeRegisterNodeConstructor", nullptr, nativeRegisterNodeConstructor, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nativeRegisterNodeStatusModifyConstructor", nullptr, nativeRegisterNodeStatusModifyConstructor, nullptr, nullptr, nullptr, napi_default, nullptr },
        {"nativeSetPixelRatio", nullptr, nativeSetPixelRatio, nullptr, nullptr, nullptr, napi_default, nullptr},
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
