# set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES /usr/include)
# set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES /usr/include)

# Not sure we actually need this for the analyzer?
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json")
    execute_process(
        COMMAND cmake -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
                ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
    )
endif()

# Whatever the platform, this always works.
find_package(
    Qt6
    COMPONENTS Widgets
               Core
               Gui
               PrintSupport
               Svg
               Xml
               Network
    REQUIRED
)

message("\n${BoldGreen}Now configuring library libXpertMassGui${ColourReset}\n")

########################################################
# Files
set(XpertMassGui_SRCS
    #
    # Utilities
    src/ColorSelector.cpp
    src/DecimalPlacesOptionsDlg.cpp
    src/ToleranceWidget.cpp
    src/ActionManager.cpp
    src/ActionManagerTableWidget.cpp
    #
    # Scripting
    src/JavaScriptingEnvironment.cpp
    src/JavaScriptingGuiUtils.cpp
    src/JavaScriptingWnd.cpp
    src/JavaScriptWorker.cpp
    src/ScriptingHistoryListWidget.cpp
    src/ScriptingObjectTreeWidgetItem.cpp
    src/JavaScriptQObjectExposureRecord.cpp
    src/JavaScriptQObjectExposureRegistry.cpp
    src/XpertMassGuiJavaScript.cpp
    #
    # Network
    src/MassDataClientServerConfigDlg.cpp
    #
    # Isotopic cluster and peak shaping
    src/IsotopicClusterGeneratorDlg.cpp
    src/IsotopicClusterShaperDlg.cpp
    src/IsotopicDataTableView.cpp
    src/IsotopicDataTableViewModel.cpp
    src/MassPeakShaperConfigDlg.cpp
    src/MassPeakShaperConfigWidget.cpp
    #
    # Labelled widget-based QAction classes
    src/LabelledDoubleSpinBoxAction.cpp
    src/LabelledNumberLineEditAction.cpp
    src/LabelledCheckBoxAction.cpp
)

set(XpertMassGui_UIS
    src/ui/DecimalPlacesOptionsDlg.ui
    src/ui/IsotopicClusterGeneratorDlg.ui
    src/ui/MassPeakShaperConfigDlg.ui
    src/ui/IsotopicClusterShaperDlg.ui
    src/ui/ElementGroupBoxWidget.ui
    src/ui/MassDataClientServerConfigDlg.ui
    src/ui/MassPeakShaperConfigWidget.ui
    src/ui/ToleranceWidget.ui
    src/ui/JavaScriptingWnd.ui
)

qt6_wrap_ui(XpertMassGui_UIS_H ${XpertMassGui_UIS})

qt6_add_resources(XpertMassGui_QRC_CPP xpertmassgui_resources.qrc)

# Because the header files are in their own directory and not along the source
# files, we need to have them listed explicitely for automoc to work properly
# below.

# Create a variable to hold the 'includes' directory *relative* to the
# current CMakeLists.txt file.

set(INCLUDES_DIR "${CMAKE_CURRENT_LIST_DIR}/includes")
file(GLOB_RECURSE XpertMassGui_HEADERS ${INCLUDES_DIR}/*.h ${INCLUDES_DIR}/*.hpp)
message(STATUS "Included the header files from ${INCLUDES_DIR}: \n\
  ${XpertMassGui_HEADERS}"
)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

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

# Only now can we add the library, because we have defined the sources.

#############################################################
# Build the static lib
add_library(
    Gui_static STATIC
    ${XpertMassGui_HEADERS}
    ${XpertMassGui_SRCS}
    ${XpertMassGui_UIS_H}
    ${XpertMassGui_QRC_CPP}
    ${PLATFORM_SPECIFIC_SOURCES}
)

set_target_properties(
    Gui_static
    PROPERTIES # Set target properties for namespace
               EXPORT_NAME "Gui_static"
               OUTPUT_NAME XpertMassGui
               LINK_FLAGS "-Wl,--whole-archive"
)

target_link_libraries(
    Gui_static
    PUBLIC PappsoMSpp::Core
           PappsoMSpp::Gui
           IsoSpec++::IsoSpec++
           Qt6::Core
           Qt6::Gui
           Qt6::Widgets
           Qt6::PrintSupport
           Qt6::Svg
           Qt6::Xml
           Qt6::Network
           QScintilla::QScintilla
           Qt6::Qml
           Core_static
)

if(PROFILING)
    target_link_options(Gui_static PRIVATE -pg)
endif()
# The INSTALL_INTERFACE that is ${prefix}/include

target_include_directories(
    Gui_static
    PUBLIC # These include directories are for building of this lib.
           $<BUILD_INTERFACE:${INCLUDES_DIR}>
           $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
           $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/source/XpertMass/includes>
           # These include directories are for users of this lib.
           $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

get_target_property(Gui_static_INCLUDES Gui_static INCLUDE_DIRECTORIES)
message(STATUS "Gui_static_INCLUDES: ${Gui_static_INCLUDES}")

install(
    TARGETS Gui_static
    EXPORT XpertMassGuiStaticTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# Export static targets
install(
    EXPORT XpertMassGuiStaticTargets
    FILE XpertMassGuiStaticTargets.cmake
    NAMESPACE XpertMass::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/XpertMass
)

export(
    EXPORT XpertMassGuiStaticTargets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/XpertMassGuiStaticTargets.cmake"
    NAMESPACE XpertMass::
)

#############################################################
# Build the shared lib
add_library(
    Gui SHARED
    ${XpertMassGui_HEADERS}
    ${XpertMassGui_SRCS}
    ${XpertMassGui_UIS_H}
    ${XpertMassGui_QRC_CPP}
    ${PLATFORM_SPECIFIC_SOURCES}
)

set_target_properties(
    Gui
    PROPERTIES # Set target properties for namespace
               EXPORT_NAME "Gui"
               OUTPUT_NAME XpertMassGui
               VERSION ${PROJECT_VERSION}
               SOVERSION ${PROJECT_VERSION_MAJOR}
               LINK_FLAGS "-Wl,--no-as-needed"
               POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(
    Gui
    PUBLIC PappsoMSpp::Core
           PappsoMSpp::Gui
           IsoSpec++::IsoSpec++
           Qt6::Core
           Qt6::Gui
           Qt6::Widgets
           Qt6::PrintSupport
           Qt6::Svg
           Qt6::Xml
           Qt6::Network
           QScintilla::QScintilla
           Qt6::Qml
           Core
)

if(PROFILING)
    target_link_options(Gui PRIVATE -pg)
endif()

# The install interface that is ${prefix}/include

target_include_directories(
    Gui
    PUBLIC # These include directories are for building of this lib.
           $<BUILD_INTERFACE:${INCLUDES_DIR}>
           $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
           $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src/XpertMass/includes>
           # These include directories are for users of this lib.
           $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

get_target_property(Gui_INCLUDES Gui INCLUDE_DIRECTORIES)

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

target_compile_definitions(Gui PRIVATE "EXPORT_LIB_SYMBOLS")

# This is to avoid the "include_next(math.h) file not found" error.
if(WIN32)
    set_target_properties(Gui_static PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
    set_target_properties(Gui PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
endif()

install(
    TARGETS Gui
    EXPORT XpertMassGuiSharedTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# Export shared targets
install(
    EXPORT XpertMassGuiSharedTargets
    FILE XpertMassGuiSharedTargets.cmake
    NAMESPACE XpertMass::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/XpertMass
)

export(
    EXPORT XpertMassGuiSharedTargets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/XpertMassGuiSharedTargets.cmake"
    NAMESPACE XpertMass::
)

#############################################################
# Common installation configuration

message("The directory containing the includes: ${CMAKE_CURRENT_LIST_DIR}/includes/MsXpS/libXpertMassGui")

install(
    DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/includes/MsXpS/libXpertMassGui
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/MsXpS"
)

message("")
message(STATUS "CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")
message("")
message(STATUS "${BoldGreen}Finished configuration of libXpertMassGui.${ColourReset}")
message("")
