cmake_minimum_required(VERSION 3.22.1)
project("mosis")

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#set(ANDROID_SDK "/Users/omar/Library/Android/sdk")
set(BINDER_DIR "${ANDROID_SDK}/platforms/android-36/optional/libbinder_ndk_cpp")
set(AIDL_EXE "${ANDROID_SDK}/build-tools/36.1.0/aidl.exe")

# Find Lua from vcpkg
find_package(Lua REQUIRED)

# Find nlohmann_json, minizip, sqlite3 and openssl for app management and sandbox
find_package(nlohmann_json CONFIG REQUIRED)
find_package(minizip CONFIG REQUIRED)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)

# Fetch RmlUi from GitHub with Lua bindings enabled
include(FetchContent)
FetchContent_Declare(
    rmlui
    GIT_REPOSITORY https://github.com/mikke89/RmlUi.git
    GIT_TAG 6.2
)
set(RMLUI_LUA_BINDINGS ON CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(RMLUI_SAMPLES OFF CACHE BOOL "" FORCE)
set(RMLUI_TESTS OFF CACHE BOOL "" FORCE)
set(RMLUI_FONT_ENGINE "freetype" CACHE STRING "" FORCE)
set(RMLUI_PRECOMPILED_HEADERS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(rmlui)

#==============================================================================
# Mosis Core Library (shared sandbox APIs)
#==============================================================================
# Enable optional features for Android
set(MOSIS_ENABLE_DATABASE ON CACHE BOOL "" FORCE)
set(MOSIS_ENABLE_NETWORK ON CACHE BOOL "" FORCE)

# Add core library from project root
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../core ${CMAKE_CURRENT_BINARY_DIR}/mosis-core)

# Configure core library for Android
target_compile_definitions(mosis-core PUBLIC MOSIS_PLATFORM_ANDROID)
target_compile_definitions(mosis-core PRIVATE MOSIS_HAS_OPENSSL)
target_include_directories(mosis-core PUBLIC ${LUA_INCLUDE_DIR})
target_link_libraries(mosis-core PUBLIC
    ${LUA_LIBRARIES}
    nlohmann_json::nlohmann_json
    unofficial::sqlite3::sqlite3
    OpenSSL::Crypto
)

#==============================================================================
# AIDL Generated Files
#==============================================================================
add_custom_command(
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/com/omixlab/mosis/IMosisService.cpp"
    COMMAND ${AIDL_EXE} --lang=ndk --min_sdk_version=36 -o ${CMAKE_CURRENT_BINARY_DIR} -h ${CMAKE_CURRENT_BINARY_DIR} -I ..\\aidl ..\\aidl\\com\\omixlab\\mosis\\IMosisService.aidl
    DEPENDS ../aidl/com/omixlab/mosis/IMosisService.aidl
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
    VERBATIM
)

add_custom_command(
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/com/omixlab/mosis/IMosisListener.cpp"
    COMMAND ${AIDL_EXE} --lang=ndk --min_sdk_version=36 -o ${CMAKE_CURRENT_BINARY_DIR} -h ${CMAKE_CURRENT_BINARY_DIR} -I ..\\aidl ..\\aidl\\com\\omixlab\\mosis\\IMosisListener.aidl
    DEPENDS ../aidl/com/omixlab/mosis/IMosisListener.aidl
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
    VERBATIM
)

#==============================================================================
# Mosis Service (Android)
#==============================================================================
add_library(mosis-service SHARED
    # Entry point and core
    mosis-service.cpp
    kernel.cpp

    # AIDL generated
    ${CMAKE_CURRENT_BINARY_DIR}/com/omixlab/mosis/IMosisService.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/com/omixlab/mosis/IMosisListener.cpp

    # Platform-specific utilities
    assets_manager.cpp
    shader.cpp
    external_texture.cpp
    quad.cpp
    egl_context.cpp
    render_target.cpp

    # OpenGL ES
    glad/src/egl.c
    glad/src/gles2.c
    RmlUi_Renderer_GL3.cpp

    # App management (uses core + Android-specific features)
    apps/app_manager.cpp
    apps/app_api.cpp
    apps/update_service.cpp

    # Android-specific sandbox components (hardware interfaces)
    sandbox/sandbox_manager.cpp
    sandbox/websocket_manager.cpp
    sandbox/camera_interface.cpp
    sandbox/microphone_interface.cpp
    sandbox/audio_output.cpp
    sandbox/location_interface.cpp
    sandbox/sensor_interface.cpp
    sandbox/bluetooth_interface.cpp
    sandbox/contacts_interface.cpp
    sandbox/message_bus.cpp
)

target_compile_definitions(mosis-service PUBLIC
    RMLUI_NUM_MSAA_SAMPLES=2
    RMLUI_GL3_CUSTOM_LOADER=<glad/gles2.h>
    MOSIS_PLATFORM_ANDROID
)

target_include_directories(mosis-service PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
    ${BINDER_DIR}
    glad/include
    # Core library headers
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../core/include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../core/include/mosis/sandbox
)

target_link_libraries(mosis-service
    # Core library (provides sandbox APIs)
    mosis-core
    # Android system libraries
    android log binder_ndk EGL GLESv2 nativewindow
    # RmlUi
    rmlui rmlui_lua
    # Dependencies
    nlohmann_json::nlohmann_json
    minizip::minizip
    unofficial::sqlite3::sqlite3
    ${LUA_LIBRARIES}
)

#==============================================================================
# Mosis Test Library
#==============================================================================
add_library(mosis-test SHARED
    ${CMAKE_CURRENT_BINARY_DIR}/com/omixlab/mosis/IMosisService.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/com/omixlab/mosis/IMosisListener.cpp
    mosis-test.cpp
    assets_manager.cpp
    shader.cpp
    external_texture.cpp
    quad.cpp
    egl_context.cpp
    render_target.cpp
    glad/src/egl.c
    glad/src/gles2.c
)

target_include_directories(mosis-test PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
    ${BINDER_DIR}
    glad/include
    # Core library includes
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../core/include
)

target_link_libraries(mosis-test
    mosis-core
    android log binder_ndk EGL GLESv2 nativewindow
)
