cmake_minimum_required(VERSION 3.16)
project(grpcaddressbook LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

if(NOT DEFINED INSTALL_EXAMPLESDIR)
    set(INSTALL_EXAMPLESDIR "examples")
endif()

set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/grpc/addressbook")

find_package(Qt6 REQUIRED COMPONENTS Core Quick Protobuf Grpc)

qt_add_executable(grpcaddressbook
    main.cpp
    addressbookengine.cpp addressbookengine.h
    ../examples_common/universallistmodel.cpp ../examples_common/universallistmodel.h
    ../examples_common/universallistmodelbase.cpp ../examples_common/universallistmodel.h
)

target_include_directories(grpcaddressbook PRIVATE ../examples_common)

target_link_libraries(grpcaddressbook PRIVATE
    Qt::Core
    Qt::Quick
    Qt::Protobuf
    Qt::Grpc
    Qt::Quick
)

qt_add_qml_module(grpcaddressbook
    URI examples.grpc.addressbook
    VERSION 1.0
    QML_FILES
        qml/AddContactView.qml
        qml/CallPopup.qml
        qml/ContactDetails.qml
        qml/ContactList.qml
        qml/CountryCodesModel.qml
        qml/DropDownColumn.qml
        qml/FloatingRoundButton.qml
        qml/InputRow.qml
        qml/main.qml
        qml/PhoneInput.qml
        qml/PrimaryText.qml
        qml/SecondaryText.qml
        qml/StackItem.qml
        qml/TextInputRow.qml
        qml/TextRow.qml
    RESOURCES
        images/arrow.png
        images/back.png
        images/call.png
        images/check.png
        images/drop.png
        images/minus.png
        images/plus.png
    NO_RESOURCE_TARGET_PATH
)

qt_protobuf_generate(TARGET grpcaddressbook
    PROTO_FILES
        proto/addressbook.proto
    QML
)

install(TARGETS grpcaddressbook
    RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
    BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
    LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

add_custom_command(TARGET grpcaddressbook
    COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/../ssl/cert.pem"
         "$<TARGET_FILE_DIR:grpcaddressbook>"
)

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../ssl/cert.pem"
        DESTINATION "${INSTALL_EXAMPLEDIR}"
)
