message("\n${BoldRed}Now configuring src/js_qml for ${TARGET}${ColourReset}\n")

set(NONGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/nongui)
set(GUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gui)

########################################################
# Files
set(${TARGET}_nongui_SRCS
    #
    # The classes that handle the processing flow
    ${NONGUI_DIR}/ProcessingFlow.cpp
    ${NONGUI_DIR}/ProcessingStep.cpp
    ${NONGUI_DIR}/ProcessingType.cpp
    #
    # Some globals.
    ${NONGUI_DIR}/globals.cpp
    #
    # The classes to look into an MS run prior to loading it
    ${NONGUI_DIR}/MsRunProber.cpp
    ${NONGUI_DIR}/vendor/BrukerTimsTofMsRunProber.cpp
    #
    # The main class that hold the mass data.
    ${NONGUI_DIR}/MsRunDataSet.cpp
    #
    # The class to handle the statistics computation about a given MsRunDataSet.
    ${NONGUI_DIR}/MsRunDataSetStats.cpp
    #
    # The class to load MS run data
    ${NONGUI_DIR}/MassSpecDataFileLoader.cpp
    ${NONGUI_DIR}/MassSpecDataFileLoaderToVector.cpp
    #
    # The base class of the integrators.
    ${NONGUI_DIR}/MassDataIntegrator.cpp
    #
    # The various integrators that use BaseMsRunDataSetTreeNodeVisitor.
    ${NONGUI_DIR}/MsRunDataSetTreeMassDataIntegratorToRt.cpp
    #
    # The classe that monitors integrators.
    ${NONGUI_DIR}/MassDataIntegratorTask.cpp
    #
    # The various MsRunDataSetTreeNodeVisitor classes.
    ${NONGUI_DIR}/BaseMsRunDataSetTreeNodeVisitor.cpp
    ${NONGUI_DIR}/MsRunStatisticsTreeNodeVisitor.cpp
    ${NONGUI_DIR}/MultiTreeNodeCombinerVisitor.cpp
    ${NONGUI_DIR}/TicChromTreeNodeCombinerVisitor.cpp
    ${NONGUI_DIR}/TicChromatogramFromMassSpecDataFile.cpp
    # #
    # # The various integrators that use QualifiedMassSpectrumVector.
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegrator.cpp
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegratorToRt.cpp
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegratorToMz.cpp
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegratorToDt.cpp
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegratorToTicInt.cpp
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegratorToDtRtMz.cpp
    ${NONGUI_DIR}/QualifiedMassSpectrumVectorMassDataIntegratorToRtDt.cpp
    # #
    # # The classes that handle the parameters for the various integrations and the
    # # display of traces.
    ${NONGUI_DIR}/MsFragmentationSpec.cpp
    # #
    # # Discoveries preferences
    ${NONGUI_DIR}/DiscoveriesPreferences.cpp
)

set(${TARGET}_gui_SRCS
    #
    ${GUI_DIR}/AboutDlg.cpp
    ${GUI_DIR}/Application.cpp
    ${GUI_DIR}/ApplicationPreferencesWnd.cpp
    ${GUI_DIR}/ProgramWindow.cpp
    #
    ${GUI_DIR}/MsRunLoader.cpp
    ${GUI_DIR}/ConsoleWnd.cpp
    #
    ${GUI_DIR}/vendor/BrukerTimsTofMsRunLoader.cpp
    ${GUI_DIR}/vendor/BrukerBafAsciiMsRunLoader.cpp
    ${GUI_DIR}/vendor/PwizLiteMsRunLoader.cpp
    ${GUI_DIR}/vendor/MzCborMsRunLoader.cpp
    ${GUI_DIR}/XyMsRunLoader.cpp
    ${GUI_DIR}/MsRunReadTask.cpp
    ${GUI_DIR}/MsRunProbeTask.cpp
    # #
    ${GUI_DIR}/OpenMsRunDataSetsDlg.cpp
    #
    ${GUI_DIR}/TaskMonitorCompositeWidget.cpp
    ${GUI_DIR}/TaskMonitorWnd.cpp
    #
    ${GUI_DIR}/MsRunProbeInfoViewerDlg.cpp
    ${GUI_DIR}/MsRunSelectorDlg.cpp
    #
    # The ms run data set tree view classes
    ${GUI_DIR}/MsRunDataSetTableViewWnd.cpp
    ${GUI_DIR}/MsRunDataSetTableViewItem.cpp
    ${GUI_DIR}/MsRunDataSetTableView.cpp
    ${GUI_DIR}/MsRunDataSetTableViewModel.cpp
    ${GUI_DIR}/MsRunDataSetTableViewProxyModel.cpp
    #
    # The MS fragmentation specification dialog
    ${GUI_DIR}/MsFragmentationSpecDlg.cpp
    #
    # The M/Z integration parameters dialog
    ${GUI_DIR}/MzIntegrationParamsDlg.cpp
    #
    # The Savitzky-Golay filter params dialog
    ${GUI_DIR}/SavitzkyGolayFilterParamsDlg.cpp
    #
    # The data plot graph/widget tree classes
    ${GUI_DIR}/DataPlottableNode.cpp
    ${GUI_DIR}/DataPlottableTree.cpp
    #
    # The trace plot composite widgets
    ${GUI_DIR}/BasePlotCompositeWidget.cpp
    ${GUI_DIR}/BaseTracePlotCompositeWidget.cpp
    ${GUI_DIR}/TicXicChromTracePlotCompositeWidget.cpp
    ${GUI_DIR}/MassSpecTracePlotCompositeWidget.cpp
    ${GUI_DIR}/DriftSpecTracePlotCompositeWidget.cpp
    #
    # The color map plot composite widgets
    ${GUI_DIR}/BaseColorMapPlotCompositeWidget.cpp
    ${GUI_DIR}/TicXicChromMassSpecColorMapPlotCompositeWidget.cpp
    ${GUI_DIR}/TicXicChromDriftSpecColorMapPlotCompositeWidget.cpp
    ${GUI_DIR}/DriftSpecMassSpecColorMapPlotCompositeWidget.cpp
    #
    # The plot windows
    ${GUI_DIR}/BasePlotWnd.cpp
    #
    # Trace plot windows
    ${GUI_DIR}/BaseTracePlotWnd.cpp
    ${GUI_DIR}/BaseColorMapPlotWnd.cpp
    ${GUI_DIR}/TicXicChromTracePlotWnd.cpp
    ${GUI_DIR}/MassSpecTracePlotWnd.cpp
    ${GUI_DIR}/DriftSpecTracePlotWnd.cpp
    #
    # Color map plot windows
    ${GUI_DIR}/TicXicChromMassSpecColorMapWnd.cpp
    ${GUI_DIR}/TicXicChromDriftSpecColorMapWnd.cpp
    ${GUI_DIR}/DriftSpecMassSpecColorMapWnd.cpp
    #
    # The save plot to graphics file
    ${GUI_DIR}/SaveToGraphicsFileDlg.cpp
    #
    # The ProcessingFlow viewer dialog window
    ${GUI_DIR}/ProcessingFlowViewerDlg.cpp
    #
    # The XIC chromatogram feature
    ${GUI_DIR}/XicExtractionWnd.cpp
    #
    # Analysis preferences
    ${GUI_DIR}/DiscoveriesPreferencesDlg.cpp
    #
    # Plot overlay transparent widget and vertical rule slider
    ${GUI_DIR}/PlotAreaOverlay.cpp
    ${GUI_DIR}/VerticalRuleSlider.cpp
)

set(${TARGET}_UIS
    ${GUI_DIR}/ui/AboutDlg.ui
    ${GUI_DIR}/ui/MsRunSelectorDlg.ui
    ${GUI_DIR}/ui/OpenMsRunDataSetsDlg.ui
    ${GUI_DIR}/ui/MsRunDataSetTableViewWnd.ui
    ${GUI_DIR}/ui/BasePlotWnd.ui
    ${GUI_DIR}/ui/BasePlotCompositeWidget.ui
    ${GUI_DIR}/ui/TaskMonitorCompositeWidget.ui
    ${GUI_DIR}/ui/TaskMonitorWnd.ui
    ${GUI_DIR}/ui/ConsoleWnd.ui
    ${GUI_DIR}/ui/MsFragmentationSpecDlg.ui
    ${GUI_DIR}/ui/MzIntegrationParamsDlg.ui
    ${GUI_DIR}/ui/SavitzkyGolayFilterParamsDlg.ui
    ${GUI_DIR}/ui/SaveToGraphicsFileDlg.ui
    ${GUI_DIR}/ui/ProcessingFlowViewerDlg.ui
    ${GUI_DIR}/ui/XicExtractionWnd.ui
    ${GUI_DIR}/ui/DiscoveriesPreferencesDlg.ui
    ${GUI_DIR}/ui/MsRunProbeInfoViewerDlg.ui
    ${GUI_DIR}/ui/ApplicationPreferencesWnd.ui
    ${GUI_DIR}/ui/ApplicationPreferencesWndGeneralPageWidget.ui
    ${GUI_DIR}/ui/ApplicationPreferencesWndDiscoveriesPageWidget.ui
    ${GUI_DIR}/ui/DecimalPlacesPreferencesWidget.ui
    ${GUI_DIR}/ui/MaxThreadCountPreferencesWidget.ui
)

set(CMAKE_AUTOUIC_SEARCH_PATHS ${GUI_DIR}/ui)

qt_standard_project_setup(REQUIRES 6.5)

qt6_add_resources(${TARGET}_qrc_CPP ../${TARGET}.qrc)

# if(JS_QML)
#
#     find_package(Qt6 REQUIRED COMPONENTS Quick)
#
#     # If we want scripting, the libPappsoMSplugin.so shared lib
#     # must be found.
#
#     #find_package(PappsoMS REQUIRED COMPONENTS PappsoMS)
#     find_package(PappsoMS REQUIRED)
#
#     if(NOT PappsoMS_FOUND)
#         message(FATAL_ERROR "PappsoMS plugin lib not found.")
#     endif()
#
#     get_target_property(PappsoMS_IMPORTED_CONFIGS PappsoMS::PappsoMS IMPORTED_CONFIGURATIONS)
#     message(STATUS "Available configurations: ${PappsoMS_IMPORTED_CONFIGS}")
#
#     get_target_property(PappsoMS_IMPORTED_LOCATION_DEBUG PappsoMS::PappsoMS IMPORTED_LOCATION_DEBUG)
#     message(STATUS "Debug PappsoMS::PappsoMS location: ${PappsoMS_IMPORTED_LOCATION_DEBUG}")
#
#     get_target_property(PappsoMS_IMPORTED_LOCATION_RELEASE PappsoMS::PappsoMS IMPORTED_LOCATION_RELEASE)
#     message(STATUS "Release PappsoMS::PappsoMS location: ${PappsoMS_IMPORTED_LOCATION_RELEASE}")
#
#     # Check current build type
#     string(TOUPPER "${CMAKE_BUILD_TYPE}" CURRENT_BUILD_TYPE_UPPER)
#     message(STATUS "Current build type: ${CURRENT_BUILD_TYPE_UPPER}")
#
#     # Like this: /usr/local/lib/qml/PappsoMS/libPappsoMSplugin.so
#     get_target_property(PappsoMS_ACTIVE_PATH PappsoMS::PappsoMS IMPORTED_LOCATION_${CURRENT_BUILD_TYPE_UPPER})
#     if(NOT PappsoMS_ACTIVE_PATH)
#         get_target_property(PappsoMS_ACTIVE_PATH PappsoMS::PappsoMS IMPORTED_LOCATION)
#     endif()
#     message(STATUS "Active library path: ${PappsoMS_ACTIVE_PATH}")
#
#     # Like this: /usr/local/lib/qml/PappsoMS
#     get_filename_component(PappsoMS_ACTIVE_DIR_PATH "${PappsoMS_ACTIVE_PATH}" DIRECTORY)
#     message(STATUS "PappsoMS_ACTIVE_DIR_PATH: ${PappsoMS_ACTIVE_DIR_PATH}")
#
#     set_property(GLOBAL PROPERTY PappsoMS_ACTIVE_DIR_PATH ${PappsoMS_ACTIVE_DIR_PATH})
#
#     get_target_property(PappsoMS_INCLUDE_DIRS PappsoMS::PappsoMS INTERFACE_INCLUDE_DIRECTORIES)
#     message(STATUS "PappsoMS_INCLUDE_DIRS: ${PappsoMS_INCLUDE_DIRS}")
#
#     # Also, add some scripting-specific stuff from this project.
#     add_subdirectory(js_qml)
#
#     set(JS_QML_DEPENDENCIES PRIVATE Qt6::Quick PappsoMS::PappsoMS ${TARGET}_qml)
#
#     qt6_add_resources(${TARGET}_qrc_CPP js_qml/${TARGET}_js_qml.qrc)
#
# endif()

###############################################################
# Configuration of the binary to be built
###############################################################

message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")

include_directories(
    # The config file is binary directory-specific !!!
    # For the config.h file generated from config.h.cmake.in
    # For the ui_Xyyyyy.h files
    ${CMAKE_CURRENT_BINARY_DIR}
    # The omp.h include directory as crafted by looking into the
    # libraries' paths after find_package(OpenMP). See CMake code
    # in the main CMakeLists.txt file.
    ${OpenMP_CXX_INCLUDE_DIRS}
    # For all the source subdirectories
    ${NONGUI_DIR}
    ${GUI_DIR}
)

if(WIN32 OR _WIN32)

    qt_add_executable(
        ${TARGET}
        WIN32
        main.cpp
        ${${TARGET}_nongui_SRCS}
        ${${TARGET}_gui_SRCS}
        ${${TARGET}_UIS}
        ${${TARGET}_qrc_CPP}
        ${PLATFORM_SPECIFIC_SOURCES}
        #${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.rc
    )
    # Required for Windows 10/11.
    # Well, this is what I thought. In fact, the program would not run on Win11
    # not because the linker options below were not set, but because the Win11
    # system what a family edition. I know the software runs with the stuff below
    # on Win11 pro system right.
    # FURTHER, since I have added the linker options below the program does
    # not start anymore on Win11 pro machines with infamous error:
    # 0xc000007b. Removing the lines below make MassXpert3 run on Win11 pro
    # again.
    # target_link_options(${TARGET} PRIVATE
    #     # Set OS version to Windows 10
    #     "-Wl,--major-os-version=10"
    #     "-Wl,--minor-os-version=0"
    #     # Set subsystem version
    #     "-Wl,--major-subsystem-version=10"
    #     "-Wl,--minor-subsystem-version=0"
    #     # Ensure modern subsystem
    #     "-Wl,--subsystem,windows:10.0"
    # )

    # In Debug mode we want the console to be opened so that we can read the debug
    # messages. But in Release mode the console is not desirable (the snippet
    # below removes it).
    if(CMAKE_BUILD_TYPE STREQUAL "Release")
        set_property(TARGET ${TARGET} PROPERTY WIN32_EXECUTABLE true)
    endif()

elseif(UNIX)

    qt_add_executable(
        ${TARGET}
        main.cpp
        ${${TARGET}_nongui_SRCS}
        ${${TARGET}_gui_SRCS}
        ${${TARGET}_UIS}
        ${${TARGET}_qrc_CPP}
        ${PLATFORM_SPECIFIC_SOURCES}
        gui/VerticalRuleSlider.hpp
        gui/VerticalRuleSlider.cpp
    )

endif()

# Finally actually set the linking dependencies to the executable.
# Note that there are so many cross-dependencies that it is virtually unfeasible
# to actually link the binary without resorting to the linker dance steps below.

# Still, I discovered that the order below is good with respect of the symbols
# dependencies resolutions when linking

# The general idea is to first list the libraries that *use* symbols and later
# libraries that *provide* symbols. Which is why all the Qt libs are listed at
# the end and the private libs are listed first.
target_link_libraries(
    ${TARGET}
    PRIVATE OpenMP::OpenMP_CXX
            -Wl,--no-as-needed
            XpertMass::Core
            XpertMass::Gui
            -Wl,--as-needed
            -Wl,--no-as-needed
            PappsoMSpp::Core
            PappsoMSpp::Gui
            -Wl,--as-needed
            -Wl,--no-as-needed
            IsoSpec++::IsoSpec++
            -Wl,--as-needed
            -Wl,--no-as-needed
            QScintilla::QScintilla
            -Wl,--as-needed
            QCustomPlotQt6::QCustomPlotQt6
            #QuaZip::QuaZip
            Qt6::Widgets
            Qt6::SvgWidgets
            Qt6::Svg
            Qt6::PrintSupport
            Qt6::Xml
            Qt6::Network
            # For pappsomspp Bruker timsTOF data handling
            Qt6::Sql
            Qt6::Concurrent
            Qt6::Qml
            # ${JS_QML_DEPENDENCIES}
            #
            # This is the object file obtained my compiling
            # the WindRes resource file using windres.exe
            # See corresponding MXE or WIN10UCRT64 toolchains.
            ${PLATFORM_SPECIFIC_LIB_FILES}
)

if(PROFILING)
    message(STATUS "Profiling is requested, adding linker -pg flag.")
    set(CMAKE_EXE_LINKER_FLAGS "-pg")
    target_link_options(${TARGET} PRIVATE -pg)
endif()

# These options allow for printing messages to the console from
# which the program is run (for example, in Linux, with wine MineXpert3).
if(WIN32)
    target_link_options(${TARGET} PRIVATE "-Wl,--subsystem,console")
endif()

# We want to install the binary arch-dependent target in
# specific situations. To have proper control, we define the arch
# component.
install(
    TARGETS ${TARGET}
    RUNTIME COMPONENT arch DESTINATION ${PROJECT_INSTALL_BIN_DIR} # Windows/Linux executable
)

message("")
message(STATUS "${BoldGreen}Finished configuring of ${TARGET}.${ColourReset}")
message("")
