#[[

Create imported targets:
  * QAD::Utils
  * QAD::Widgets
  * QAD::Application
  * QAD::Blockview
  * QAD::Graphic
  * QAD::SQLTable
  * QAD::TouchWidgets
  * QAD::Doc
  * QAD::PIQt
  * QAD::PIQtUtils

These targets include directories and dependencies

]]

cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
cmake_policy(SET CMP0020 NEW) # Automatically link Qt executables to qtmain target on Windows

if(NOT BUILDING_qad)
	find_package(PIP QUIET)
	include(QtWraps)
	include(SHSTKMacros)
endif()

shstk_is_parent_exists(hasParent PARENT_DIRECTORY)

shstk_set_find_dirs(qad)
set(_SEARCH_DIR ${qad_LIBDIR})

qad_find_qt(Core QUIET)
if(NOT SOME_QT_FOUND)
	message(WARNING "No qt found, please install qt")
	set(QAD_FOUND OFF CACHE BOOL "")
	return()
endif()

if (NOT BUILDING_qad)
	list(APPEND _SEARCH_DIR $ENV{SMSDK_DIR}/lib)
	shstk_find_header(qad QAD "qad_version.h" "")
endif()

if(QAD_FIND_VERSION VERSION_GREATER QAD_VERSION)
	message(FATAL_ERROR "QAD version ${QAD_VERSION} is available, but ${QAD_FIND_VERSION} requested!")
endif()

set(__libs "utils;widgets;application;blockview;graphic;sql_table;touch_widgets;doc")
if (PIP_FOUND OR BUILDING_pip)
	list(APPEND __libs "piqt;piqt_utils")
endif()

set(__module_utils          Utils        )
set(__module_widgets        Widgets      )
set(__module_application    Application  )
set(__module_blockview      Blockview    )
set(__module_graphic        Graphic      )
set(__module_sql_table      SQLTable     )
set(__module_touch_widgets  TouchWidgets )
set(__module_doc            Doc          )
set(__module_piqt           PIQt         )
set(__module_piqt_utils     PIQtUtils    )

foreach (_l ${__libs})
	set( __inc_${_l} "")
	set(__deps_${_l} "")
	set(__libs_${_l} "")
endforeach()

set(__deps_widgets       "QAD::Utils")
set(__deps_application   "QAD::Widgets")
set(__deps_blockview     "QAD::Widgets")
set(__deps_graphic       "QAD::Widgets")
set(__deps_sql_table     "QAD::Widgets")
set(__deps_piqt          "QAD::Widgets;PIP")
set(__deps_piqt_utils    "QAD::Blockview;QAD::PIQt")

#message("find QAD ${BUILDING_qad}")

if (BUILDING_qad)

	if (NOT SET_TARGETS_qad)
		set(SET_TARGETS_qad ON CACHE BOOL "")
		#message("create aliases")
		foreach(_l ${__libs})
			foreach(_v ${_QT_VERSIONS_})
				set(_m ${__module_${_l}})
				if((NOT TARGET QAD::${_m}${_v}) AND TARGET qad_${_l}${_v})
					#message("alias QAD::${_m}${_v} = qad_${_l}${_v}")
					set(MULTILIB_QAD__${_m}_SUFFIX_Qt${_v} ${_v} CACHE STRING "")
					add_library(QAD::${_m}${_v} ALIAS qad_${_l}${_v})
				endif()
			endforeach()
			list(APPEND QT_MULTILIB_LIST QAD::${_m})
		endforeach()
	endif()
	
	set(QAD_FOUND ON CACHE BOOL "")

else()

	#message("create interfaces")
	foreach(_l ${__libs})
		foreach(_v ${_QT_VERSIONS_})
			set(_m ${__module_${_l}})
			find_library(QAD_LIBRARY_${_l}${_v} qad_${_l}${_v} HINTS ${_SEARCH_DIR})
			#message("found ${_l}${_v} = ${QAD_LIBRARY_${_l}${_v}}")
			if((NOT TARGET QAD::${_m}${_v}) AND QAD_LIBRARY_${_l}${_v})
				#message("imported QAD::${_m}${_v} = ${QAD_LIBRARY_${_l}${_v}}")
				set(MULTILIB_QAD__${_m}_SUFFIX_Qt${_v} ${_v} CACHE STRING "")
				add_library(QAD::${_m}${_v} UNKNOWN IMPORTED)
				set(_deps)
				foreach (_d ${__deps_${_l}})
					if (${_d} MATCHES "QAD::.*")
						list(APPEND _deps ${_d}${_v})
					else()
						list(APPEND _deps ${_d})
					endif()
				endforeach()
				set_target_properties(QAD::${_m}${_v} PROPERTIES
					IMPORTED_LOCATION "${QAD_LIBRARY_${_l}${_v}}"
					INTERFACE_INCLUDE_DIRECTORIES "${qad_INCLUDES}"
					INTERFACE_LINK_LIBRARIES "${_deps}")
			endif()
		endforeach()
		list(APPEND QT_MULTILIB_LIST QAD::${_m})
	endforeach()

endif()

if(hasParent)
	set(QT_MULTILIB_LIST ${QT_MULTILIB_LIST} PARENT_SCOPE)
endif()

set(QAD_FOUND ON CACHE BOOL "")
