/*

 * Copyright (c) 2026 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



#include <iostream>

#include <hilog/log.h>

#include "napi/native_api.h"

#include <ace/xcomponent/native_interface_xcomponent.h>

#include <EGL/egl.h>

#include "common/common.h"

#include "render/image_render.h"

#include "render/render_engine.h"



OH_NativeXComponent_Callback renderCallback_;

OH_NativeXComponent *g_nativeXComponent;

std::unique_ptr<RenderEngine> g_renderEngine = nullptr;

void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)

{

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceCreatedCB");

    if ((component == nullptr) || (window == nullptr)) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",

                     "OnSurfaceCreatedCB: component or window is null");

        return;

    }



    uint64_t width;

    uint64_t height;

    int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);

    if (xSize != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",

                     "OnSurfaceCreatedCB: Unable to get XComponent size");

        return;

    }

    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",

        "OnSurfaceCreatedCB: width = %{public}llu, height = %{public}llu", width, height);



    // 创建 ImageRender 实例

    std::shared_ptr<ImageRender> imageRender = std::make_shared<ImageRender>();



    // 初始化 RenderEngine

    g_renderEngine = std::make_unique<RenderEngine>(imageRender, width, height, window);

    g_renderEngine->OnAppResume();

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "RenderEngine started");

}



void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window)

{

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceChangedCB");

    if ((component == nullptr) || (window == nullptr)) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",

                     "OnSurfaceChangedCB: component or window is null");

        return;

    }



    uint64_t width;

    uint64_t height;

    int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);

    if (xSize != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",

                     "OnSurfaceChangedCB: Unable to get XComponent size");

        return;

    }



    if (g_renderEngine) {

        g_renderEngine->OnSurfaceChanged(width, height);

    }

}



void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window)

{

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceDestroyedCB");

    if (g_renderEngine) {

        g_renderEngine->OnAppPause();

    }

}



EXTERN_C_START

static napi_value Init(napi_env env, napi_value exports)

{

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "napi_init", "init start");

    napi_property_descriptor desc[] = {};

    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);

    napi_value exportInstance = nullptr;

    if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "napi_init", "Export: napi_get_named_property fail");

        return nullptr;

    }



    OH_NativeXComponent *nativeXComponent = nullptr;

    if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "napi_init", "Export: napi_unwrap fail");

        return nullptr;

    }

    g_nativeXComponent = nativeXComponent;

    char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};

    uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;

    if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "napi_init",

                     "Export: OH_NativeXComponent_GetXComponentId fail");

        return nullptr;

    }



    renderCallback_.OnSurfaceCreated = OnSurfaceCreatedCB;

    renderCallback_.OnSurfaceChanged = OnSurfaceChangedCB;

    renderCallback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;

    OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_);

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "napi_init", "init end");

    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);

}