CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

# PNG
IF(${DSEED_USE_PNG})
	FIND_PACKAGE(ZLIB QUIET)
	FIND_PACKAGE(PNG QUIET)
	
	IF(NOT ZLIB_FOUND)
		MESSAGE(STATUS "[dseed] # Not found zlib")
		SET(DSEED_USE_PNG OFF)
	ENDIF()

	IF(NOT PNG_FOUND)
		MESSAGE(STATUS "[dseed] # Not found libpng")
		SET(DSEED_USE_PNG OFF)
	ENDIF()

	IF(${DSEED_USE_PNG})
		MESSAGE(STATUS "[dseed] # Use libpng")
		LIST(APPEND DSEED_INCLUDE_DIRS ${ZLIB_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${ZLIB_LIBRARY} ${PNG_LIBRARY})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_PNG)
	ENDIF()
ENDIF()

# JPEG
IF(${DSEED_USE_JPEG})
	FIND_PACKAGE(JPEG QUIET)
	IF(NOT JPEG_FOUND)
		SET(DSEED_USE_JPEG OFF)
		MESSAGE(STATUS "[dseed] # Not found libjpeg or mozjpeg")
	ENDIF()

	IF(${DSEED_USE_JPEG})
		MESSAGE(STATUS "[dseed] # Use libjpeg")
		LIST(APPEND DSEED_INCLUDE_DIRS ${JPEG_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${JPEG_LIBRARY})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_JPEG)
	ENDIF()
ENDIF()

# JPEG2000
IF(${DSEED_USE_JPEG2000})
	FIND_PACKAGE(openjpeg QUIET)
	IF(NOT OPENJPEG_FOUND)
		SET(DSEED_USE_JPEG2000 OFF)
		MESSAGE(STATUS "[dseed] # Not found openjpeg")
	ENDIF()

	IF(${DSEED_USE_JPEG2000})
		MESSAGE(STATUS "[dseed] # Use openjpeg")
		LIST(APPEND DSEED_INCLUDE_DIRS ${OPENJPEG_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${OPENJPEG_LIBRARY} ${OPENJPEG_LIBRARIES})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_JPEG2000)
	ENDIF()
ENDIF()

# WebP
IF(${DSEED_USE_WEBP})
	FIND_PACKAGE(webp QUIET)
	IF(NOT WEBP_FOUND)
		SET(DSEED_USE_WEBP OFF)
		MESSAGE(STATUS "[dseed] # Not found libwebp")
	ENDIF()

	IF(${DSEED_USE_WEBP})
		MESSAGE(STATUS "[dseed] # Use libwebp")
		LIST(APPEND DSEED_INCLUDE_DIRS ${WEBP_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${WEBP_LIBRARY} ${WEBP_LIBRARIES})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_WEBP)
	ENDIF()
ENDIF()

# libtiff
IF(${DSEED_USE_TIFF})
	FIND_PACKAGE(LibLZMA QUIET)
	FIND_PACKAGE(ZLIB QUIET)
	FIND_PACKAGE(TIFF QUIET)
	IF(NOT ZLIB_FOUND OR NOT LZMA_FOUND OR NOT TIFF_FOUND)
		SET(DSEED_USE_TIFF OFF)
		MESSAGE(STATUS "[dseed] # Not found zlib or liblzma or libtiff")
	ENDIF()

	IF(${DSEED_USE_TIFF})
		MESSAGE(STATUS "[dseed] # Use libtiff")
		LIST(APPEND DSEED_INCLUDE_DIRS ${ZLIB_INCLUDE_DIRS} ${LIBLZMA_INCLUDE_DIRS} ${TIFF_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${ZLIB_LIBRARY} ${LIBLZMA_LIBRARY} ${TIFF_LIBRARY})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_TIFF)
	ENDIF()
ENDIF()

# giflib
IF(${DSEED_USE_GIF})
	FIND_PACKAGE(GIF QUIET)
	IF(NOT GIF_FOUND)
		SET(DSEED_USE_GIF OFF)
		MESSAGE(STATUS "[dseed] # Not found giflib")
	ENDIF()

	IF(${DSEED_USE_GIF})
		MESSAGE(STATUS "[dseed] # Use giflib")
		LIST(APPEND DSEED_INCLUDE_DIRS ${GIF_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${GIF_LIBRARY})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_GIF)
	ENDIF()
ENDIF()

# libheif
IF(${DSEED_USE_HEIF})
	FIND_PACKAGE(libheif QUIET)
	IF(NOT libheif_FOUND)
		SET(DSEED_USE_HEIF OFF)
		MESSAGE(STATUS "[dseed] # Not found libheif")
	ENDIF()

	IF(${DSEED_USE_HEIF})
		MESSAGE(STATUS "[dseed] # Use libheif")
		LIST(APPEND DSEED_INCLUDE_DIRS ${libheif_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS heif)
		LIST(APPEND DSEED_DEFINITIONS -DUSE_HEIF)
	ENDIF()
ENDIF()

# Windows Imaging Codecs
IF(${DSEED_USE_WINDOWS_IMAGING_CODECS})
	IF(NOT (PLATFORM_WINDOWS OR PLATFORM_UWP))
		SET(DSEED_USE_WINDOWS_IMAGING_CODECS OFF)
		MESSAGE(STATUS "[dseed] # Not found Microsoft Windows Imaging Codecs")
	ENDIF()

	IF(${DSEED_USE_WINDOWS_IMAGING_CODECS})
		MESSAGE(STATUS "[dseed] # Use Microsoft Windows Imaging Codecs")
		LIST(APPEND DSEED_LINK_LIBS windowscodecs.lib)
	ENDIF()
ENDIF()

# Vorbis
IF(${DSEED_USE_VORBIS})
	FIND_PACKAGE(ogg QUIET)
	FIND_PACKAGE(vorbis QUIET)
	IF(NOT OGG_FOUND OR NOT vorbis_FOUND)
		SET(DSEED_USE_VORBIS OFF)
		MESSAGE(STATUS "[dseed] # Not found vorbis")
	ENDIF()

	IF(${DSEED_USE_VORBIS})
		MESSAGE(STATUS "[dseed] # Use vorbis")
		LIST(APPEND DSEED_INCLUDE_DIRS ${OGG_INCLUDE_DIRS} ${VORBIS_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${OGG_LIBRARY} ${VORBIS_LIBRARY} Vorbis::vorbis Vorbis::vorbisenc Vorbis::vorbisfile)
		LIST(APPEND DSEED_DEFINITIONS -DUSE_VORBIS)
	ENDIF()
ENDIF()

# libopus
IF(${DSEED_USE_OPUS})
	FIND_PACKAGE(ogg QUIET)
	FIND_PACKAGE(opus QUIET)
	FIND_PACKAGE(opusfile QUIET)
	IF(NOT OGG_FOUND OR NOT OPUS_FOUND)
		SET(DSEED_USE_OPUS OFF)
		MESSAGE(STATUS "[dseed] # Not found libogg or libopus or opusfile")
	ENDIF()

	IF(${DSEED_USE_OPUS})
		MESSAGE(STATUS "[dseed] # Use libopus")
		LIST(APPEND DSEED_INCLUDE_DIRS ${OGG_INCLUDE_DIRS} ${OPUS_INCLUDE_DIRS} ${OPUSFILE_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${OGG_LIBRARY} ${OPUS_LIBRARY} ${OPUSFILE_LIBRARY})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_OPUS)
	ENDIF()
ENDIF()

# libFLAC
IF(${DSEED_USE_FLAC})
	FIND_PACKAGE(ogg QUIET)
	FIND_PACKAGE(FLAC QUIET)
	IF(NOT OGG_FOUND OR NOT FLAC_FOUND)
		SET(DSEED_USE_FLAC OFF)
		MESSAGE(STATUS "[dseed] # Not found libFLAC")
	ENDIF()

	IF(${DSEED_USE_FLAC})
		MESSAGE(STATUS "[dseed] # Use libFLAC")
		LIST(APPEND DSEED_INCLUDE_DIRS ${OGG_INCLUDE_DIRS} ${FLAC_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${OGG_LIBRARY} ${FLAC_LIBRARIES} FLAC::FLAC FLAC::FLAC++)
		LIST(APPEND DSEED_DEFINITIONS -DUSE_FLAC)
	ENDIF()
ENDIF()

# Media Foundation
IF(${DSEED_USE_MEDIA_FOUNDATION})
	IF(NOT (PLATFORM_WINDOWS OR PLATFORM_UWP))
		SET(DSEED_USE_MEDIA_FOUNDATION OFF)
		MESSAGE(STATUS "[dseed] # Not found Microsoft Media Foundation")
	ENDIF()

	IF(${DSEED_USE_MEDIA_FOUNDATION})
		MESSAGE(STATUS "[dseed] # Use Microsoft Media Foundation")
		LIST(APPEND DSEED_LINK_LIBS mf.lib mfplat.lib mfuuid.lib mfreadwrite.lib)
	ENDIF()
ENDIF()

IF(${DSEED_USE_BITMAP_ENCODERS})
	MESSAGE(STATUS "[dseed] # Use Bitmap Encoders")
	LIST(APPEND DSEED_DEFINITIONS -DUSE_BITMAP_ENCODERS)
ENDIF()

# X11
IF(${DSEED_USE_X11} AND PLATFORM_LINUX)
	FIND_PACKAGE(X11 QUIET)

	IF(NOT X11_FOUND)
		SET(DSEED_USE_X11 OFF)
		MESSAGE(STATUS "[dseed] # Not found X11")
	ENDIF()

	IF(${DSEED_USE_X11})
		MESSAGE(STATUS "[dseed] # Use X11")
		LIST(APPEND DSEED_INCLUDE_DIRS ${X11_INCLUDE_DIRS})
		LIST(APPEND DSEED_LINK_LIBS ${X11_LIBRARIES})
		LIST(APPEND DSEED_DEFINITIONS -DUSE_X11)
	ENDIF()
ELSE()
	SET(DSEED_USE_X11 OFF)
	MESSAGE(STATUS "[dseed] # Not available X11")
ENDIF()

# GLEW for OpenGL
if(${DSEED_USE_OPENGL})
	IF((PLATFORM_LINUX AND ${DSEED_USE_X11}) OR PLATFORM_WINDOWS OR PLATFORM_MACOS OR PLATFORM_IOS OR PLATFORM_ANDROID)
		FIND_PACKAGE(OpenGL QUIET)
		IF(NOT OpenGL_FOUND)
			MESSAGE(STATUS "[dseed] # Not found OpenGL")
		ELSE()
			MESSAGE(STATUS "[dseed] # Use OpenGL")
			LIST(APPEND DSEED_INCLUDE_DIRS ${OpenGL_INCLUDE_DIRS})
			LIST(APPEND DSEED_LINK_LIBS ${OpenGL_LIBRARY} ${OpenGL_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})
			LIST(APPEND DSEED_DEFINITIONS -DUSE_OPENGL)
		ENDIF()

		FIND_PACKAGE(GLEW QUIET)
		IF(OpenGL_FOUND AND NOT GLEW_FOUND OR PLATFORM_IOS OR PLATFORM_ANDROID OR PLATFORM_UWP)
			MESSAGE(STATUS "[dseed] # Not found GLEW")
		ELSE()
			MESSAGE(STATUS "[dseed] # Use GLEW")
			LIST(APPEND DSEED_INCLUDE_DIRS ${GLEW_INCLUDE_DIRS})
			LIST(APPEND DSEED_LINK_LIBS ${GLEW_LIBRARY} ${GLEW_LIBRARIES})
			LIST(APPEND DSEED_DEFINITIONS -DUSE_GLEW)
		ENDIF()
	ELSE()
		SET(DSEED_USE_OPENGL OFF)
		MESSAGE(STATUS "[dseed] # Not available OpenGL")
	ENDIF()
ENDIF()

# Vulkan
IF(${DSEED_USE_VULKAN})
	IF(PLATFORM_MACOS OR PLATFORM_IOS OR PLATFORM_UWP)
		MESSAGE(STATUS "[dseed] # Vulkan not available")
	ELSE()
		FIND_PACKAGE(Vulkan QUIET)
		IF(NOT VULKAN_FOUND)
			MESSAGE(STATUS "[dseed] # Not found Vulkan SDK")
		ELSE()
			MESSAGE(STATUS "[dseed] # Use Vulkan")
			LIST(APPEND DSEED_INCLUDE_DIRS ${VULKAN_INCLUDE_DIRS})
			LIST(APPEND DSEED_LINK_LIBS ${VULKAN_LIBRARY})
			LIST(APPEND DSEED_DEFINITIONS -DUSE_VULKAN)
		ENDIF()
	ENDIF()
ENDIF()