set(TARGET ${GRPC_QUICK_PLUGIN_NAME})

set(TARGET_EXPORT ${TARGET}Targets)
set(TARGET_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${TARGET})
set(TARGET_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

find_package(Qt5 COMPONENTS Core Qml REQUIRED)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOMOC_MOC_OPTIONS -Muri=QtGrpc)
set(CMAKE_AUTORCC ON)

include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufCommon.cmake)

extract_qt_variable(QT_INSTALL_QML)

set(TARGET_IMPORTS_DIR ${QT_INSTALL_QML}/QtGrpc)

file(GLOB SOURCES
    qquickgrpcsubscription.cpp
    qtgrpcquickplugin.cpp)

file(GLOB HEADERS
    qtgrpcquickplugin.h
    qquickgrpcsubscription_p.h
    qtgrpcquick_global.h)

add_library(${TARGET} ${SOURCES})
if(NOT BUILD_SHARED_LIBS)
    if(WIN32)
        message(WARNING "Static version of QtProtobuf is not fully tested on Win32 platforms")
    endif()
    target_compile_definitions(${TARGET} PRIVATE QT_PROTOBUF_STATIC QT_STATICPLUGIN PUBLIC QT_GRPC_QUICK_PLUGIN_NAME="QtGrpcQuickPlugin")
endif()

target_link_libraries(${TARGET} PRIVATE Qt5::Core Qt5::Qml ${QT_PROTOBUF_PROJECT}::QtGrpc)
set_target_properties(${TARGET} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/QtGrpc"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/QtGrpc"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}/QtGrpc"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}/QtGrpc")
target_compile_definitions(${TARGET} PRIVATE QT_GRPC_QUICK_LIB)

if(NOT BUILD_SHARED_LIBS)
    add_library(${QT_PROTOBUF_PROJECT}::${TARGET} ALIAS ${TARGET})
    install(TARGETS ${TARGET} COMPONENT lib
        EXPORT ${TARGET_EXPORT} COMPONENT dev
        ARCHIVE DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib
        RUNTIME DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib
        LIBRARY DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib)
    install(EXPORT ${TARGET_EXPORT} NAMESPACE ${QT_PROTOBUF_PROJECT}:: FILE ${TARGET_EXPORT}.cmake DESTINATION ${TARGET_CMAKE_DIR} COMPONENT dev)
    export(TARGETS ${TARGET} NAMESPACE ${QT_PROTOBUF_PROJECT}:: FILE ${TARGET_EXPORT}.cmake)
else()
    install(TARGETS ${TARGET} COMPONENT lib
        ARCHIVE DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib
        RUNTIME DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib
        LIBRARY DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib)
endif()

add_custom_command(TARGET ${TARGET}
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/qmldir $<TARGET_FILE_DIR:${TARGET}>/qmldir
    COMMENT "Copying qmldir to binary directory")

install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/qmldir DESTINATION "${TARGET_IMPORTS_DIR}" COMPONENT lib)
