find_package(Qt5 REQUIRED COMPONENTS Core Widgets)

include(${PARAVIEW_USE_FILE})
include (ParaViewPlugins)

# We need to add the current value of VTK_MODULES_DIR to the module path
# so that when the plugins are built all the modules can be found. Otherwise,
# modules that aren't loaded as direct dependencies of CMB modules will
# not be found.
list(APPEND CMAKE_MODULE_PATH "${VTK_MODULES_DIR}")


include_directories(${SMTK_INCLUDE_DIR})

set(ui_files smtkTruchasCoilsView.ui)
set(header_files
  smtkTruchasCoilsView.h
  smtkTruchasMaterialsView.h)

# We must add custom views in the plugin dir instead of qt dir
# due to the fact that add_smtk__ui_view marcro assumes files
# are in the same dir.
set(view_source_files
  smtkTruchasCoilsView.cxx
  smtkTruchasMaterialsView.cxx)
set(qt_source_files ${view_source_files})


include_directories(${SMTK_INCLUDE_DIR})

qt5_wrap_cpp(mocs ${header_files})
qt5_wrap_ui(uisrc ${ui_files})

source_group("Qt_Source" FILES ${qt_source_files} )
source_group("Qt_Generated" FILES ${mocs} ${uisrc})
source_group("Qt_Header" FILES ${header_files})

set(viewInterfaces)
set(viewSources)
foreach(src ${view_source_files})
  string(REPLACE ".cxx" "" view_class ${src})
  set(vitmp)
  set(vstmp)
  add_smtk_ui_view(
    vitmp
    vstmp
    CLASS_NAME ${view_class}
    VIEW_NAME ${view_class}
  )
  list(APPEND viewInterfaces ${vitmp})
  list(APPEND viewSources ${vstmp})
endforeach()

include(SMTKPluginMacros)

add_smtk_plugin(
  smtkTruchasPlugin "1.0"
  GUI_INTERFACES ${viewInterfaces}
  GUI_SOURCES
    ${viewSources}
    ${qt_source_files}
    ${mocs}
    ${uisrc}
  SOURCES
    ${non_ui_srcs}
  CS_KITS
    vtkSMTKWidgetsExt
  REGISTRAR
    smtk::simulation::truchas::Registrar
  MANAGERS
    smtk::operation::Manager smtk::resource::Manager
)

target_link_libraries(smtkTruchasPlugin
  LINK_PUBLIC
    smtkCore
    smtkQtExt
    smtkTruchas
    smtkTruchasQtExt
    Qt5::Core
    Qt5::Widgets
    vtkjsoncpp
    vtkPVServerManagerApplication
  LINK_PRIVATE
    vtkPVServerManagerApplicationCS
)

target_include_directories(smtkTruchasPlugin PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  $<INSTALL_INTERFACE:include>
)

generate_export_header(smtkTruchasPlugin EXPORT_FILE_NAME Exports.h)

smtk_get_kit_name(name dir_prefix)

# Install the header files
install(
  FILES
    ${headers}
    ${CMAKE_CURRENT_BINARY_DIR}/Exports.h
  DESTINATION
    include/${PROJECT_NAME}/${PROJECT_VERSION}/${dir_prefix})

# Install the library and exports
install(
  TARGETS smtkTruchasPlugin
  EXPORT  TruchasPlugin
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
  PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}/${PROJECT_VERSION}/${dir_prefix})
