include_guard()

#This function searches for and includes all libraries a module needs to compile and link, given AftrBurnerEngine.cmake and 
#the user installed AftrConfig.h.

function( include_and_link_aburn_deps arg_target )

   MESSAGE( STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
                  "ENTERING Inside of aburn_include_and_link_deps::include_and_link_aburn_deps.cmake..." )

   MESSAGE( STATUS "arg_target is ${arg_target}" )

   include(CheckSymbolExists)
   include( "${AFTR_PATH_TO_CMAKE_SCRIPTS}/AftrBurnerEngine.cmake" )  #We are using the CMake install target files generated by the engine.
   # This is the path to the aftr libs as specified within the engine build "install" and exported into the aftr-targets.cmake above.
   GET_PROPERTY( aftrLibPathMinSizeRel     TARGET AftrBurnerEngine::AftrBurnerEngine PROPERTY IMPORTED_LOCATION_MINSIZEREL )
   GET_PROPERTY( aftrLibPathRelease        TARGET AftrBurnerEngine::AftrBurnerEngine PROPERTY IMPORTED_LOCATION_RELEASE )
   GET_PROPERTY( aftrLibPathDebug          TARGET AftrBurnerEngine::AftrBurnerEngine PROPERTY IMPORTED_LOCATION_DEBUG )
   GET_PROPERTY( aftrLibPathRelWithDebInfo TARGET AftrBurnerEngine::AftrBurnerEngine PROPERTY IMPORTED_LOCATION_RELWITHDEBINFO )

   MESSAGE( STATUS "aftrLibPathMinSizeRel     is: ${aftrLibPathMinSizeRel}" )
   MESSAGE( STATUS "aftrLibPathRelease        is: ${aftrLibPathRelease}" )
   MESSAGE( STATUS "aftrLibPathDebug          is: ${aftrLibPathDebug}" )
   MESSAGE( STATUS "aftrLibPathRelWithDebInfo is: ${aftrLibPathRelWithDebInfo}" )

   MESSAGE( STATUS "Build path is ${CMAKE_CURRENT_BINARY_DIR}" )
   MESSAGE( "" )

   TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE AftrBurnerEngine::AftrBurnerEngine ) #Can specify release, debug, relminsize, relwithdebinfo here as last param
   #set_target_properties( ${arg_target} PROPERTIES MAP_IMPORTED_CONFIG_RELEASE RELEASE ) #Map release to MinSizeRel
   #set_target_properties( ${arg_target} PROPERTIES MAP_IMPORTED_CONFIG_MINSIZEREL MINSIZEREL ) #Map release to MinSizeRel
   #set_target_properties( ${arg_target} PROPERTIES MAP_IMPORTED_CONFIG_DEBUG DEBUG ) #Map release to MinSizeRel
   #set_target_properties( ${arg_target} PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELWITHDEBINFO ) #Map release to MinSizeRel



   IF( AFTR_CONFIG_USE_BOOST )
         MESSAGE( STATUS "Boost include        : ${Boost_INCLUDE_DIRS}" )   
         MESSAGE( STATUS "Boost component libs : ${Boost_LIBRARIES}" )
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE ${Boost_INCLUDE_DIRS} )
         target_link_libraries(      ${arg_target} PRIVATE ${Boost_LIBRARIES} )
   ENDIF()

   #using find_package for SDL and boost, every platform includes the headers and libraries the same way,
   #but for packages that don't have the Find<MyLib>.cmake in the /engine/src/cmake folder, we have to manually add them per platform
   IF( WIN32 )
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                           "opengl32.lib" #Required for Windows
                           "glu32.lib"
                           SDL2::SDL2 #SDL2::SDL2main
                           SDL2_image::SDL2_image
                           )
      
      IF( AFTR_CONFIG_USE_OGL_GLEW )
         MESSAGE( STATUS "Windows GLEW header ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0/include                   : $ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0")
         MESSAGE( STATUS "Windows GLEW   lib  ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0/lib/Release/x64/glew32.lib: $ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0/lib/Release/x64/glew32.lib")
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE "$ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0/include" ) #Used on Windows (linux should include from above)
            TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                        optimized "$ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0/lib/Release/x64/glew32s.lib"
                            debug "$ENV{AFTR_3RD_PARTY_ROOT}/glew-2.1.0/lib/Release/x64/glew32s_debug.lib" )
      ENDIF()

      IF( AFTR_CONFIG_USE_FREE_TYPE )
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                           "$ENV{AFTR_3RD_PARTY_ROOT}/freetype-2.9/include" )
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                  optimized "$ENV{AFTR_3RD_PARTY_ROOT}/freetype-2.9/lib/freetype.lib"
                      debug "$ENV{AFTR_3RD_PARTY_ROOT}/freetype-2.9/lib/freetype_debug.lib"
                              )
      ENDIF()

      IF( AFTR_CONFIG_USE_CAL3D )
      TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                           "$ENV{AFTR_3RD_PARTY_ROOT}/cal3d-0.11rc2/src" )
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                  optimized "$ENV{AFTR_3RD_PARTY_ROOT}/cal3d-0.11rc2/lib/cal3d.lib"
                      debug "$ENV{AFTR_3RD_PARTY_ROOT}/cal3d-0.11rc2/lib/cal3d_d.lib"
                              )
      ENDIF()

      IF( AFTR_CONFIG_USE_OCULUS_RIFT_OVR )
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                  optimized "$ENV{AFTR_3RD_PARTY_ROOT}/libOVR.lib"
                      debug "$ENV{AFTR_3RD_PARTY_ROOT}/libOVRd.lib" )                           
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                            "$ENV{AFTR_3RD_PARTY_ROOT}/OculusSDK/ovr_sdk_win_1.26.0_public/OculusSDK/LibOVR/Include" )                           
      ENDIF()

      IF( AFTR_CONFIG_USE_GDAL )
      TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                           "${GDAL_INCLUDE_DIR}"
                              )
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                  optimized "${GDAL_LIBRARY}"
                      debug "${GDAL_LIBRARY}"
                     #debug "$ENV{AFTR_3RD_PARTY_ROOT}/gdal-3.0.4/lib/gdal_i.lib"
                              )
      ENDIF()  

      IF( AFTR_CONFIG_USE_ASSIMP )
         target_link_libraries( ${arg_target} PRIVATE assimp::assimp )
      ENDIF()

      IF( AFTR_CONFIG_USE_ODE )
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE ODE::ODE )
         MESSAGE( STATUS "         ode_FOUND: ${ode_FOUND}" )
         MESSAGE( STATUS "      ENV{ode_DIR}: $ENV{ode_DIR}" )
         MESSAGE( STATUS "          ode_DIR : ${ode_DIR}" )
         MESSAGE( STATUS " ODE_INCLUDE_DIRS : ${ODE_INCLUDE_DIRS}" )
         MESSAGE( STATUS " ODE_LIBRARY_DIRS : ${ODE_LIBRARY_DIRS}" )
         MESSAGE( STATUS "    ODE_LIBRARIES : ${ODE_LIBRARIES}" )
      else()
         MESSAGE( STATUS " ODE is disabled." )   
      ENDIF()

      IF( AFTR_CONFIG_USE_3DS )
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                  optimized "${AFTR_USERLAND_LIB_PATH}/lib3ds.lib"
                      debug "${AFTR_USERLAND_LIB_PATH}/lib3ds.lib"
                              )
      ENDIF() 

      IF( AFTR_CONFIG_USE_KEYLOK_DONGLE )
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                  optimized "${AFTR_USERLAND_LIB_PATH}/kfunc${AFTR_NBITS}MD.lib"
                      debug "${AFTR_USERLAND_LIB_PATH}/kfunc${AFTR_NBITS}MD.lib"
                              )
      ENDIF()
   ENDIF()

   ############## UNIX / LINUX Settings
   IF( UNIX )
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE
                           "GL"
                           SDL2::SDL2
                           SDL2_image::SDL2_image
                           "dl"
                  optimized "${SDL2Image_LIBRARY}"
                      debug "${SDL2Image_LIBRARY_DEBUG}"
                           )

      MESSAGE( STATUS " ---GLEW---------------- " )
      FIND_PACKAGE( GLEW )
      if( GLEW_FOUND )
         MESSAGE( STATUS "***Found GLEW." )
         MESSAGE( STATUS "   GLEW Include  (GLEW_INCLUDE_DIRS): ${GLEW_INCLUDE_DIRS}")
         MESSAGE( STATUS "   GLEW Libs (GLEW_STATIC_LIBRARIES): ${GLEW_STATIC_LIBRARIES}" )
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE "${GLEW_INCLUDE_DIRS}" )
         TARGET_LINK_LIBRARIES(      ${arg_target} PRIVATE GLEW::GLEW )
      endif()
      MESSAGE( STATUS " ----------------------- " )                        

      IF( AFTR_CONFIG_USE_ASSIMP )
         target_link_libraries( ${arg_target} PRIVATE assimp::assimp )
      ENDIF()

      IF( AFTR_CONFIG_USE_FREE_TYPE )
         MESSAGE( "{FREETYPE_INCLUDE_DIRS} is ${FREETYPE_INCLUDE_DIRS}" )
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE "${FREETYPE_INCLUDE_DIRS}" )
            TARGET_LINK_LIBRARIES(   ${arg_target} PRIVATE "${FREETYPE_LIBRARIES}" )
      ENDIF()

      IF( AFTR_CONFIG_USE_GDAL )
         TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE "${GDAL_INCLUDE_DIR}" )
         TARGET_LINK_LIBRARIES(      ${arg_target} PRIVATE "${GDAL_LIBRARY}" )
      ENDIF()

      IF( AFTR_CONFIG_USE_3DS )
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE  "3ds" )
      ENDIF()

      IF( AFTR_CONFIG_USE_ODE )
         find_package(ode)
         TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE ODE::ODE )
      ENDIF()
   ENDIF()

   ###############For BOTH Win and Linux
   IF( AFTR_CONFIG_USE_FMT_LIB )
      find_package(fmt)
#      set_target_properties( fmt::fmt PROPERTIES MAP_IMPORTED_CONFIG_RELEASE RELWITHDEBINFO ) #Map release to MinSizeRel
#      set_target_properties( fmt::fmt PROPERTIES MAP_IMPORTED_CONFIG_RELEASE RELEASE ) #Map release to MinSizeRel
#      set_target_properties( fmt::fmt PROPERTIES MAP_IMPORTED_CONFIG_DEBUG   DEBUG ) #Map release to MinSizeRel
#      set_target_properties( fmt::fmt PROPERTIES MAP_IMPORTED_CONFIG_RELEASE MINSIZEREL ) #Map release to MinSizeRel
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE fmt::fmt )
   ENDIF()

   IF( AFTR_CONFIG_USE_RANGE_V3_LIB )
      find_package(range-v3)
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE range-v3::range-v3 )
   ENDIF()

   IF( AFTR_CONFIG_USE_EIGEN_V3 )
      find_package(Eigen3 3.4 REQUIRED NO_MODULE)
      TARGET_LINK_LIBRARIES( ${arg_target} PRIVATE Eigen3::Eigen )
   ENDIF()

   IF( AFTR_CONFIG_USE_GTEST_LIB OR AFTR_USE_GTEST ) #Engine-side GTEST usage
      target_link_libraries( ${arg_target} PRIVATE GTest::GTest GTest::Main )
   ENDIF()

   #These includes are both Linux and Windows, should probably be decomposed like the above in the future
   TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                              "${CMAKE_SOURCE_DIR}"
                              "${AFTR_USR_INCLUDE_DIR}"
                              "${SDL2Image_INCLUDE_DIR}"
                           )

   IF( AFTR_CONFIG_USE_IMGUI )
      TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                                 "${AFTR_EXTERNAL_PROJECTS_ROOT}"
                                 #"${AFTR_EXTERNAL_PROJECTS_ROOT}/imgui" #Used on Windows & linux   
                                 #"${AFTR_EXTERNAL_PROJECTS_ROOT}/imgui/implot" #Used on Windows & linux   
                              )
      MESSAGE( STATUS "Dear ImGui (IMGUI) path is ${AFTR_EXTERNAL_PROJECTS_ROOT}/imgui" )
      MESSAGE( STATUS "Dear ImGui ImPlot path is ${AFTR_EXTERNAL_PROJECTS_ROOT}/imgui/implot" )
   ENDIF()

   MESSAGE( "USE VIMBA IS ${AFTR_CONFIG_USE_VIMBA_X} $AFTR_CONFIG_USE_VIMBA_X")
   if( AFTR_CONFIG_USE_VIMBA_X )
      #if( Vmb_FOUND )
      #message( STATUS "Looking for Vimba X API. Only enable this if you connect to GenICam Cameras." )
      #find_package(Vmb REQUIRED COMPONENTS CPP HINTS $ENV{VIMBA_X_HOME}/api/lib/cmake/vmb )
	   #message( STATUS "Vmb_FOUND is ${Vmb_FOUND}" )
	   target_link_libraries( "${PROJECT_NAME}" PRIVATE Vmb::CPP )
	   set_target_properties( "${PROJECT_NAME}" PROPERTIES
		                        VS_DEBUGGER_ENVIRONMENT "PATH=${VMB_BINARY_DIRS};$ENV{PATH}" )
   endif()

   IF( AFTR_CONFIG_USE_OCULUS_RIFT_OVR )
      TARGET_INCLUDE_DIRECTORIES( ${arg_target} PRIVATE
                                 "${AFTR_EXTERNAL_PROJECTS_ROOT}/OculusSDK" #Used on Windows (linux should include from above)
                              )
   ENDIF()

   MESSAGE( STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
                   "LEAVING aburn_include_and_link_deps::include_and_link_aburn_deps.cmake..." )

endfunction( include_and_link_aburn_deps target)
