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


# 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
  #
  ColorSelector.cpp
  IsotopicClusterGeneratorDlg.cpp
  IsotopicClusterShaperDlg.cpp
  IsotopicDataTableView.cpp
  IsotopicDataTableViewModel.cpp
  MassDataClientServerConfigDlg.cpp
  MassPeakShaperConfigDlg.cpp
  MassPeakShaperConfigWidget.cpp)

set(XpertMassGui_UIS
  ui/IsotopicClusterGeneratorDlg.ui
  ui/MassPeakShaperConfigDlg.ui
  ui/IsotopicClusterShaperDlg.ui
  ui/ElementGroupBoxWidget.ui
  ui/MassDataClientServerConfigDlg.ui
  ui/MassPeakShaperConfigWidget.ui
)

qt6_wrap_ui(XpertMassGui_UIS_H ${XpertMassGui_UIS})

qt6_add_resources(XpertMassGui_QRC_CPP ressources.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/libXpertMassGui")
# Use the directory as is (see below, will be modified)
# to create the list of header files.
file(GLOB XpertMassGui_HEADERS ${INCLUDES_DIR}/*.hpp)
list(APPEND XpertMassGui_HEADERS ${INCLUDES_DIR}/exportimportconfig.h)
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 OUTPUT_NAME XpertMassGui
  LINK_FLAGS "-Wl,--whole-archive")

target_link_libraries(Gui_static

  PappsoMSpp::Core

  IsoSpec++::IsoSpec++

  Qt6::Core
  Qt6::Gui
  Qt6::Widgets
  Qt6::PrintSupport
  Qt6::Svg
  Qt6::Xml
  Qt6::Network

  Core_static)

# The install interface that is ${prefix}/include
target_include_directories(Gui_static PUBLIC
  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

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

get_target_property(Gui_static_INCLUDES Gui_static INCLUDE_DIRECTORIES)

message(STATUS "Gui_static_INCLUDES: \
${Gui_static_INCLUDES}")

install(TARGETS Gui_static
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

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

set_target_properties(Gui
  PROPERTIES OUTPUT_NAME XpertMassGui
  VERSION ${PROJECT_VERSION}
  SOVERSION ${PROJECT_VERSION_MAJOR}
  LINK_FLAGS "-Wl,--no-as-needed"
)

target_link_libraries(Gui

  PappsoMSpp::Core

  IsoSpec++::IsoSpec++

  Qt6::Core
  Qt6::Gui
  Qt6::Widgets
  Qt6::PrintSupport
  Qt6::Svg
  Qt6::Xml
  Qt6::Network

  Core)

# The install interface that is ${prefix}/include
# These include directories are for users of this lib.
target_include_directories(Gui PUBLIC
  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

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

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()


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

install(FILES ${XpertMassGui_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libXpertMassGui")

# See parent directory's list file.

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