cmake_minimum_required(VERSION 3.21)

project(ProtocolParser VERSION ${CMAKE_PROJECT_VERSION} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(QT_VERSION VERSION_GREATER_EQUAL "6.3")
    qt_standard_project_setup()
    qt_policy(SET QTP0001 NEW)
else()
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_AUTOUIC ON)
endif()

if(APPLE)
    set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
endif()

set(QML_IMPORT_PATH ${CMAKE_BINARY_DIR}/app_source CACHE STRING "Qt Creator extra QML import paths" FORCE)

set(PROJECT_COMPANY "Mentalflow's Lab")
set(PROJECT_COPYRIGHT "Copyright (c) 2023 Mentalflow's Lab. All rights reserved.")
set(PROJECT_DOMAIN "dev.ourdocs.cn.protocolparser")
set(PROJECT_BUNDLE_NAME ${PROJECT_NAME})
set(version_str "${CMAKE_PROJECT_VERSION_MAJOR},${CMAKE_PROJECT_VERSION_MINOR},${CMAKE_PROJECT_VERSION_PATCH}")
add_definitions(-DPROTOCOLPARSER_VERSION=${version_str})

find_package(Qt6 COMPONENTS Quick SerialPort LinguistTools REQUIRED)

set(
    sources_files source/main.cpp include/serialportmanager.h source/serialportmanager.cpp include/eventsbus.h source/eventsbus.cpp
    dlln3x/DLLN3X.h dlln3x/DLLN3X.cpp dlln3x/zigbeeframe.h
    include/zigbeeparser.h source/zigbeeparser.cpp source/crypto.cpp include/crypto.h
    include/protocol.h source/protocol.cpp include/protocol_data.h
    include/config.h source/config.cpp
    include/serialdataresolver.h source/serialdataresolver.cpp
    include/zigbeedataresolver.h source/zigbeedataresolver.cpp
    include/eventbusresolver.h source/eventbusresolver.cpp
    languages/zh_CN.ts languages/en_US.ts
)

set(
    qml_files qml/Main.qml qml/components/RibbonWindow.qml qml/components/ZigBeeFrame.qml
    qml/components/RibbonMessageListView.qml qml/components/RibbonMessage.qml
    qml/components/ZigBeeMessage.qml qml/components/SerialPortAssistant.qml
    qml/components/ZigBeeDataView.qml qml/components/TabBar.qml qml/components/CenterView.qml
    qml/components/ListTable.qml qml/components/DeviceList.qml qml/components/KeysList.qml
    qml/components/EventsHistoryList.qml qml/components/FrameChooser.qml
)

INCLUDE_DIRECTORIES(dlln3x include)
add_subdirectory(sm_crypto)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/app/debug)
else()
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/app/release)
endif()

if (WIN32)
    set(app_icon_resource_windows resources/icon.rc)
    qt_add_executable(${PROJECT_NAME}
        ${sources_files}
        resources/icon.rc
    )
file(TO_CMAKE_PATH "/" PATH_SEPARATOR)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    set(DLLPATH ../3rdparty/msvc/*.dll)
else()
    set(DLLPATH ../3rdparty/mingw/*.dll)
endif()
string(REPLACE "/" ${PATH_SEPARATOR} DLLPATH "${DLLPATH}")
file(GLOB DLL_FILES ${DLLPATH})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
    ${DLL_FILES}
    ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
elseif(APPLE)
    set(MACOSX_BUNDLE_ICON_FILE AppIcon)
    set(App_ICON "resources/imgs/AppIcon.icns")
    set_source_files_properties(${App_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
    qt_add_executable(${PROJECT_NAME}
        ${sources_files}
        ${App_ICON}
    )
else ()
    set(PROJECT_BUNDLE_NAME Protocol-Parser)
    qt_add_executable(${PROJECT_NAME}
        ${sources_files}
    )
endif ()

foreach(qmlfile ${qml_files})
    string(REPLACE "qml/" "" fixedfile ${qmlfile})
    set_source_files_properties(${qmlfile} PROPERTIES QT_RESOURCE_ALIAS ${fixedfile})
endforeach(qmlfile)

qt_add_qml_module(${PROJECT_NAME}
    URI ${PROJECT_NAME}
    QML_FILES ${qml_files}
    RESOURCE_PREFIX "/qt/qml/"
    VERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER ${PROJECT_DOMAIN}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_BUNDLE_NAME}
    MACOSX_BUNDLE_COPYRIGHT ${PROJECT_COPYRIGHT}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
    OUTPUT_NAME ${PROJECT_BUNDLE_NAME}
)

qt_add_translations(${PROJECT_NAME}
    TS_FILES languages/zh_CN.ts languages/en_US.ts
    RESOURCE_PREFIX "/translations"
)

if(RIBBONUI_BUILD_STATIC_LIB)
    add_definitions(-DRIBBONUI_BUILD_STATIC_LIB)
    target_link_libraries(${PROJECT_NAME} PRIVATE
        Qt::Quick
        Qt::SerialPort
        RibbonUIplugin
        sm_crypto
        FramelessHelper::Core
        FramelessHelper::Quick
    )

else()
    target_link_libraries(${PROJECT_NAME} PRIVATE
        Qt::Quick
        Qt::SerialPort
        RibbonUI
        sm_crypto
        FramelessHelper::Core
        FramelessHelper::Quick
    )

endif()
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lupdate)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lrelease)
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
