#------------------------------------------------------------------------------
# Declare source files for modelbuilder

# Headers that are not Qt subclasses
set(modelbuilder_HDRS
  mbMenuBuilder.h
)

# Headers that are Qt subclasses (and thus need moc run on them)
set(modelbuilder_MOC_HDRS
  mbMainWindow.h
  mbTestEventPlayer.h
  mbTestEventTranslator.h
)

# Implementation files.
set(modelbuilder_SRCS
  mbMainWindow.cxx
  mbMenuBuilder.cxx
  mbTestEventPlayer.cxx
  mbTestEventTranslator.cxx
)

# Qt UI files
set(modelbuilder_UI_SRCS
  ui/mbMainWindow.ui
  ui/mbFileMenu.ui
)

# Qt resource files
set(modelbuilder_RSRCS
  resource/mbResource.qrc
)

#------------------------------------------------------------------------------
# Add extra library containing custom code for the client.
include(ParaViewQt)
pv_find_package_qt(qt_targets REQUIRED QUIET QT5_COMPONENTS Widgets)
pv_qt_wrap_cpp(MOC_BUILT_SOURCES ${modelbuilder_MOC_HDRS})
pv_qt_wrap_ui(UI_BUILT_SOURCES ${modelbuilder_UI_SRCS})
pv_qt_add_resources(QT_BUILT_RESOURCES ${modelbuilder_RSRCS})
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_BINARY_DIR}
)
# ${CMAKE_CURRENT_SOURCE_DIR}/Documentation)

configure_file(
  mbTestData.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/mbTestData.h
)

set(modelbuilder_SOURCE_FILES
  ${modelbuilder_SRCS}
  ${modelbuilder_HDRS}
  ${modelbuilder_MOC_HDRS}
  ${MOC_BUILT_SOURCES}
  ${UI_BUILT_SOURCES}
  ${QT_BUILT_RESOURCES}
)

#------------------------------------------------------------------------------
# ParaView applications provides a mechanism to add gui xmls from modules.
# This is done by defining variables named ${vtk-module}_PARAVIEW_GUI_XMLS in
# the module.cmake file for the modules pointing to the paths for the
# GUI-xmls.  We process those here.
set (application_gui_xmls
# "${CMAKE_CURRENT_SOURCE_DIR}/ParaViewSources.xml"
# "${CMAKE_CURRENT_SOURCE_DIR}/ParaViewFilters.xml"
)
foreach (module IN LISTS VTK_MODULES_ENABLED)
  get_property(gui_xml GLOBAL PROPERTY ${module}_PARAVIEW_GUI_XMLS)
  if (gui_xml)
    foreach(xml IN LISTS gui_xml)
      list(APPEND application_gui_xmls ${xml})
    endforeach()
  endif()
endforeach()

#if (PARAVIEW_ENABLE_EMBEDDED_DOCUMENTATION)
#  #------------------------------------------------------------------------------
#  # Build Online-Help (aka Embedded Help) for the modelbuilder application.
#  # This is done after the above piece of code that sets the application_gui_xmls
#  # variable. Documentation/CMakeLists.txt depends on it.
#  add_subdirectory(Documentation)
#endif()

#------------------------------------------------------------------------------
# FIXME: This is necessary because the vtkPVStaticPluginsInit library cannot be
# exported (because exporting plugins had other issues). We can't put the
# paraview_static_plugins_init function call anywhere ParaView-specific because
# anything we do would be too late to have any effect (it must be before
# `.plugins` is loaded from the pq@BPC_NAME@Initializer.Initialize ->
# pqApplicationCore::pqApplicationCore -> vtkPVPluginTracker::GetInstance call
# chain occurs).
#
# Basically, what needs to happen to avoid this hack is for, in static builds,
# branded clients to build their own vtkPVStaticPluginsInit library with any
# plugins available at that time and link to it. We cannot do this in
# build_paraview_client right now because the command line executables in
# modelbuilder itself needs vtkPVStaticPluginsInit as well.
set(SUPPORT_STATIC_PLUGINS TRUE)

set(modelbuilder_plugin_candidates
  # Basic SMTK functionality
  smtkPVServerExtPlugin
  smtkPQComponentsPlugin
  smtkRepresentationPlugin
  smtkPQOperatorViewsPlugin
  # Sessions
  smtkCGMSessionPlugin
  smtkDiscreteSessionPlugin
  smtkExodusSessionPlugin
  smtkPolygonSessionPlugin
  smtkRemoteSessionPlugin
  # Custom operators and extensions
  smtkRemusMeshOperatorPlugin
  smtkVTKOperatorsPlugin
  smtkOpenCVPlugin
  smtkVXLOperatorViewsPlugin
  # CMB-specific functionality
  cmbPostProcessingModePlugin
)
set(modelbuilder_plugins)
foreach(plugin ${modelbuilder_plugin_candidates})
  if (TARGET ${plugin})
    list(APPEND modelbuilder_plugins ${plugin})
  endif()
endforeach()

include(CMBTestingMacros)
cmb_plugin_init_file(cmb_plugin_paths modelbuilder_plugins)

set(title "modelbuilder ${PARAVIEW_VERSION_FULL} ${PARAVIEW_BUILD_ARCHITECTURE}-bit")
#------------------------------------------------------------------------------
# Build the client
build_paraview_client(modelbuilder
  APPLICATION_NAME "modelbuilder"
  TITLE "${title}"
  ORGANIZATION  "modelbuilder"
  VERSION_MAJOR ${cmb_version_major}
  VERSION_MINOR ${cmb_version_minor}
  VERSION_PATCH ${cmb_version_patch}
  PVMAIN_WINDOW mbMainWindow
  PVMAIN_WINDOW_INCLUDE mbMainWindow.h
  BUNDLE_ICON   "${CMAKE_CURRENT_SOURCE_DIR}/modelbuilder.icns"
  APPLICATION_ICON  "${CMAKE_CURRENT_SOURCE_DIR}/modelbuilder.ico"
  # GUI_CONFIGURATION_XMLS ${application_gui_xmls}
  SOURCES ${modelbuilder_SOURCE_FILES}
  INSTALL_RUNTIME_DIR "${VTK_INSTALL_RUNTIME_DIR}"
  INSTALL_LIBRARY_DIR "${VTK_INSTALL_LIBRARY_DIR}"
  INSTALL_ARCHIVE_DIR "${VTK_INSTALL_ARCHIVE_DIR}"
  OPTIONAL_PLUGINS "${modelbuilder_plugins}"
)

target_link_libraries(modelbuilder
  LINK_PRIVATE
    ${qt_targets}
    vtkPVServerManagerDefault
)
target_include_directories(modelbuilder
  PUBLIC
    $<BUILD_INTERFACE:${cmb_SOURCE_DIR}>
    $<BUILD_INTERFACE:${cmb_BINARY_DIR}>
)

#if (PARAVIEW_ENABLE_EMBEDDED_DOCUMENTATION)
#  # Link against the documentation module.
#  target_link_libraries(modelbuilder
#    LINK_PRIVATE vtkParaViewDocumentation)
#endif()

# link enabled plugins if not building in shared library mode and
# add dependecies to linked python modules These are non-empty only when
# building statically.
if (PARAVIEW_ENABLE_PYTHON)
  target_link_libraries(modelbuilder
    LINK_PRIVATE
      vtkUtilitiesPythonInitializer
  )
endif()

if(NOT BUILD_SHARED_LIBS)
  target_link_libraries(modelbuilder
    LINK_PRIVATE
      vtkPVStaticPluginsInit
  )
endif()

if (cmb_enable_testing)
  add_subdirectory(testing)
endif ()

#------------------------------------------------------------------------------
if (APPLE AND NOT PARAVIEW_DO_UNIX_STYLE_INSTALLS)
  # For Macs, we add install rule to package everything that's built into a single
  # App. Look at the explanation of MACOSX_APP_INSTALL_PREFIX in the top-level
  # CMakeLists.txt file for details.

  # add install rules to generate the App bundle.
  install(CODE "
   include(\"${ParaView_CMAKE_DIR}/ParaViewBrandingInstallApp.cmake\")

   #fillup bundle with all the libraries and plugins.
   cleanup_bundle(
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app/Contents/MacOS/modelbuilder
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_LIBRARY_DIR}
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${PARAVIEW_INSTALL_PLUGINS_DIR}
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_DATA_DIR})

   # Place the App at the requested location.
   file(INSTALL DESTINATION \"${MACOSX_APP_INSTALL_PREFIX}\"
        TYPE DIRECTORY FILES
          \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app\"
        USE_SOURCE_PERMISSIONS)
   "
   COMPONENT Runtime)
elseif (APPLE AND PARAVIEW_DO_UNIX_STYLE_INSTALLS)
  # This is a unix style install on OsX. Purge the bundle.
  install(CODE
    "
      include(\"${ParaView_CMAKE_DIR}/ParaViewBrandingInstallApp.cmake\")
      convert_bundle_to_executable(
          \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app/Contents/MacOS/modelbuilder
          \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app
          \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR})
    "
    COMPONENT Runtime)
elseif (UNIX)
  configure_file(
    modelbuilder.desktop.in
    "${CMAKE_CURRENT_BINARY_DIR}/modelbuilder.desktop"
    @ONLY)
  install(
    FILES       "${CMAKE_CURRENT_BINARY_DIR}/modelbuilder.desktop"
    DESTINATION share/applications
    COMPONENT   runtime)
  foreach (iconsize 22x22 32x32 96x96)
    install(
      FILES       "resource/modelbuilder-${iconsize}.png"
      DESTINATION "share/icons/hicolor/${iconsize}/apps"
      RENAME      modelbuilder.png
      COMPONENT   runtime)
  endforeach ()
  install(
    FILES       modelbuilder.appdata.xml
    DESTINATION share/appdata
    COMPONENT   runtime)
endif()
