cmake_minimum_required(VERSION 3.10)
project(NuklearDemo)

# Set C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Find required packages
find_package(OpenGL REQUIRED)

# Include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Add gl3w source (OpenGL loader)
set(GL3W_SRC
    gl3w/src/gl3w.c
)

# Add executable
add_executable(nuklear_demo main.c ${GL3W_SRC})

# Include gl3w headers
target_include_directories(nuklear_demo PRIVATE gl3w/include)

# Platform-specific settings
if(WIN32)
    # Windows: Try to find GLFW3 using find_package or manual setup
    find_package(glfw3 QUIET)
    if(glfw3_FOUND)
        target_link_libraries(nuklear_demo glfw ${OPENGL_LIBRARIES})
    else()
        # Manual GLFW3 setup for Windows - user needs to install GLFW3
        message(STATUS "GLFW3 not found via find_package. Please ensure GLFW3 is installed.")
        message(STATUS "You can install GLFW3 via vcpkg: vcpkg install glfw3")
        message(STATUS "Or download from: https://www.glfw.org/download.html")

        # Try common GLFW3 installation paths
        find_library(GLFW3_LIBRARY
            NAMES glfw3 glfw
            PATHS
            "E:/External Includes/glfw-3.4/build/src/Release"
            "E:/External Includes/glfw-3.4/build/src/Debug"
            "E:/External Includes/glfw-3.4/lib"
            "C:/Program Files/GLFW/lib"
            "C:/vcpkg/installed/x64-windows/lib"
            "${CMAKE_PREFIX_PATH}/lib"
        )

        find_path(GLFW3_INCLUDE_DIR
            NAMES GLFW/glfw3.h
            PATHS
            "E:/External Includes/glfw-3.4/include"
            "C:/Program Files/GLFW/include"
            "C:/vcpkg/installed/x64-windows/include"
            "${CMAKE_PREFIX_PATH}/include"
        )

        if(GLFW3_LIBRARY AND GLFW3_INCLUDE_DIR)
            target_include_directories(nuklear_demo PRIVATE ${GLFW3_INCLUDE_DIR})
            target_link_libraries(nuklear_demo ${GLFW3_LIBRARY} ${OPENGL_LIBRARIES})
        else()
            # Fallback: assume GLFW3 will be available at link time
            target_link_libraries(nuklear_demo glfw3 ${OPENGL_LIBRARIES})
        endif()
    endif()

    # Windows specific libraries
    target_link_libraries(nuklear_demo opengl32 gdi32)

elseif(APPLE)
    # macOS: Use find_package or pkg-config
    find_package(glfw3 QUIET)
    if(glfw3_FOUND)
        target_link_libraries(nuklear_demo glfw ${OPENGL_LIBRARIES})
    else()
        find_package(PkgConfig)
        if(PkgConfig_FOUND)
            pkg_check_modules(GLFW3 REQUIRED glfw3)
            target_include_directories(nuklear_demo PRIVATE ${GLFW3_INCLUDE_DIRS})
            target_link_libraries(nuklear_demo ${GLFW3_LIBRARIES} ${OPENGL_LIBRARIES})
        else()
            message(FATAL_ERROR "GLFW3 not found. Please install via Homebrew: brew install glfw")
        endif()
    endif()

    # macOS specific frameworks
    find_library(COCOA_LIBRARY Cocoa)
    find_library(IOKIT_LIBRARY IOKit)
    find_library(COREVIDEO_LIBRARY CoreVideo)
    target_link_libraries(nuklear_demo
        ${COCOA_LIBRARY}
        ${IOKIT_LIBRARY}
        ${COREVIDEO_LIBRARY}
    )

elseif(UNIX)
    # Linux: Use pkg-config
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(GLFW3 REQUIRED glfw3)

    target_include_directories(nuklear_demo PRIVATE ${GLFW3_INCLUDE_DIRS})
    target_link_libraries(nuklear_demo
        ${GLFW3_LIBRARIES}
        ${OPENGL_LIBRARIES}
        m dl
    )
    target_compile_options(nuklear_demo PRIVATE ${GLFW3_CFLAGS_OTHER})
endif()

# Set output directory
set_target_properties(nuklear_demo PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
