set(MODULE_NAME windows)

set (platform_INCLUDES "")
set (platform_LIBS "")

if(UNIX)
    message(STATUS "Checking dependencies for module: window")
    find_package(X11 QUIET)
    if(X11_FOUND)
        list(APPEND platform_INCLUDES
            ui_interfaces/x11/XcbInterface.cpp
            ui_interfaces/x11/XcbEventInterface.cpp
            ui_interfaces/x11/XcbWindow.cpp
            ui_interfaces/x11/XcbScreen.cpp
            ui_interfaces/x11/XcbImage.cpp
            ui_interfaces/x11/XcbTextInterface.cpp
            ui_interfaces/x11/XcbKeyboard.cpp
            ui_interfaces/x11/XcbGlInterface.cpp
            ui_interfaces/x11/XcbExtensionInterface.cpp
            ui_interfaces/x11/XcbGlWindowInterface.cpp
            )
        list(APPEND platform_LIBS ${X11_LIBRARIES} ${X11_xcb_LIB} ${X11_X11_xcb_LIB} ${X11_xkbcommon_LIB} ${X11_xkbcommon_X11_LIB} libxcb-image.so)
        list(APPEND X11_INCLUDE_DIRS ${X11_xkbcommon_INCLUDE_PATH})      
        list(APPEND DEFINES "HAS_X11")   
    else()
        message(STATUS "X11 not found - skipping support")   
    endif()
    
    find_package(Wayland QUIET)
    if(WAYLAND_FOUND)
        list(APPEND platform_INCLUDES
            ui_interfaces/wayland/WaylandInterface.cpp
            ui_interfaces/wayland/WaylandSurface.cpp
            ui_interfaces/wayland/WaylandBuffer.cpp
            ui_interfaces/wayland/WaylandPointerInterface.cpp
            ui_interfaces/wayland/WaylandSeatInterface.cpp
            ui_interfaces/wayland/WaylandKeyboard.cpp
            ui_interfaces/wayland/WaylandEglInterface.cpp
            ui_interfaces/wayland/WaylandEglWindowInterface.cpp
            ${WAYLAND_XDG_SOURCE}
        )
        list(APPEND platform_LIBS OpenGL::EGL)
        list(APPEND DEFINES "HAS_WAYLAND")

        set(OpenGL_GL_PREFERENCE "GLVND")
        find_package(OpenGL QUIET)
        if (OpenGL_FOUND)
            list(APPEND platform_LIBS ${WAYLAND_LIBRARIES})
        endif()
    else()
        Message(STATUS "Wayland not found - skipping support")	
    endif()
else()
	list(APPEND platform_INCLUDES
	    ui_interfaces/win32/Win32UIInterface.h
	    ui_interfaces/win32/Win32UIInterface.cpp
	    ui_interfaces/win32/Win32WindowInterface.h
	    ui_interfaces/win32/Win32WindowInterface.cpp
	    ui_interfaces/win32/Win32Window.h
	    ui_interfaces/win32/Win32Window.cpp
	    ui_interfaces/win32/directx/Win32DxWindowInterface.h
	    ui_interfaces/win32/directx/Win32DxWindowInterface.cpp		
        ui_interfaces/win32/directx/DirectXDescriptors.h
	    ui_interfaces/win32/directx/DirectXDescriptors.cpp	
        ui_interfaces/win32/directx/DirectX2dIntegration.h
	    ui_interfaces/win32/directx/DirectX2dIntegration.cpp
        ui_interfaces/win32/directx/DirectXBuffers.h
	    ui_interfaces/win32/directx/DirectXBuffers.cpp
		ui_interfaces/win32/directx/DirectXCommandList.h
	    ui_interfaces/win32/directx/DirectXCommandList.cpp	
		)

        list(APPEND platform_LIBS D3D12.lib D3DCompiler.lib DXGI.lib Dwrite.lib D2d1.lib D3D11.lib)   
        
        find_package(DirectX-Headers REQUIRED)
        list(APPEND platform_LIBS Microsoft::DirectX-Headers)  
endif()

list(APPEND windows_LIB_INCLUDES
		ui_interfaces/AbstractUiInterface.h
        ui_interfaces/UiInterfaceFactory.cpp
        managers/WindowManager.h
        managers/DesktopManager.h
        managers/EventManager.h
        managers/WindowManager.cpp
        managers/DesktopManager.cpp
        managers/EventManager.cpp
)

# add the library
add_library(windows SHARED ${windows_LIB_INCLUDES} ${platform_INCLUDES}) 

target_include_directories(windows PUBLIC
                          ${CMAKE_CURRENT_SOURCE_DIR}
                          ${CMAKE_CURRENT_SOURCE_DIR}/managers
                          ${CMAKE_CURRENT_SOURCE_DIR}/ui_interfaces
                          ${CMAKE_CURRENT_SOURCE_DIR}/ui_interfaces/x11
                          ${CMAKE_CURRENT_SOURCE_DIR}/ui_interfaces/wayland
                          ${CMAKE_CURRENT_SOURCE_DIR}/ui_interfaces/win32
                          ${CMAKE_CURRENT_SOURCE_DIR}/ui_interfaces/win32/directx
                          ${WAYLAND_INCLUDE_DIRS}
                          ${X11_INCLUDE_DIRS}
                          )
                          
target_compile_definitions(${MODULE_NAME} PRIVATE ${DEFINES})
if(UNIX)
target_compile_options(${MODULE_NAME} PRIVATE -Wno-attributes) # From xdg shell autogen code
endif()
target_link_libraries(${MODULE_NAME} PUBLIC ${platform_LIBS} core geometry graphics ui_elements)

set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src/ui)
set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) 
