--[[----------------------------------------------------------------------------

WFOzContentTypes.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2013 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

local WFZString = require 'WFZString'
local ZSTR = assert( WFZString.fromZString )

local WFOzContentTypes = {}
--------------------------------------------------------------------------------

local kImageContentTypeNumberLowEnd = 1000
local kImageContentTypeNumber = 1000
local kImageJpegContentTypeNumber = 1001
local kImagePngContentTypeNumber = 1002
local kImageRawContentTypeNumber = 1003
local kImageDngContentTypeNumber = 1004
local kImageTiffContentTypeNumber = 1005
local kImagePsdContentTypeNumber = 1006
local kImageHeicContentTypeNumber = 1007
local kImageAvifContentTypeNumber = 1008
local kImageJxlContentTypeNumber = 1009
local kImageContentTypeNumberHighEnd = 1999

local kVideoContentTypeNumberLowEnd = 2000
		-- Warning: This number is hard-coded in WFOzAssetInfo.c for performance
		-- reasons.  Don't change here without changing the value there.
		
local kVideoContentTypeNumber = 2000
local kVideoMp4ContentTypeNumber = 2001
local kVideoQuicktimeContentTypeNumber = 2002
local kVideoMpegContentTypeNumber = 2003
local kVideoAviContentTypeNumber = 2004
local kVideoWmvContentTypeNumber = 2005
local kVideoMtsContentTypeNumber = 2006
local kVideoM4vContentTypeNumber = 2007
local kVideo3gpContentTypeNumber = 2008
local kVideoContentTypeNumberHighEnd = 2999
		-- Warning: This number is hard-coded in WFOzAssetInfo.c for performance
		-- reasons.  Don't change here without changing the value there.
		
--------------------------------------------------------------------------------
-- Content type strings
local kImageContentType = 'image/*'
local kImageJpegContentType = 'image/jpeg'
local kImagePngContentType = 'image/png'
local kImagePsdContentType = 'image/vnd.adobe.photoshop'
local kImageTiffContentType = 'image/tiff'
local kImageHeicContentType = 'image/heic'
local kImageAvifContentType = 'image/avif'
local kImageJxlContentType = 'image/jxl'
local kImageDngContentType = 'image/dng'
local kImageRawContentType = 'application/octet-stream'

local kVideoContentType = 'video/*'
local kVideoMp4ContentType = 'video/mp4'
local kVideoMpegContentType = 'video/mpeg'
local kVideoMtsContentType = 'video/MP2T'
local kVideoQuicktimeContentType = 'video/quicktime'
local kVideoAviContentType = 'video/x-msvideo'
local kVideoWmvContentType = 'video/x-ms-wmv'
local kVideoM4vContentType = 'video/x-m4v'
local kVideo3gpContentType = 'video/3gpp'

--------------------------------------------------------------------------------

local kContentTypeStringToNumber = {}
kContentTypeStringToNumber[ kImageContentType ] = kImageContentTypeNumber
kContentTypeStringToNumber[ kImageJpegContentType ] = kImageJpegContentTypeNumber
kContentTypeStringToNumber[ kImagePngContentType ] = kImagePngContentTypeNumber
kContentTypeStringToNumber[ kImagePsdContentType ] = kImagePsdContentTypeNumber
kContentTypeStringToNumber[ kImageTiffContentType ] = kImageTiffContentTypeNumber
kContentTypeStringToNumber[ kImageHeicContentType ] = kImageHeicContentTypeNumber
kContentTypeStringToNumber[ kImageAvifContentType ] = kImageAvifContentTypeNumber
kContentTypeStringToNumber[ kImageJxlContentType ] = kImageJxlContentTypeNumber
kContentTypeStringToNumber[ kImageDngContentType ] = kImageDngContentTypeNumber
kContentTypeStringToNumber[ kImageRawContentType ] = kImageRawContentTypeNumber

kContentTypeStringToNumber[ kVideoContentType ] = kVideoContentTypeNumber
kContentTypeStringToNumber[ kVideoMp4ContentType ] = kVideoMp4ContentTypeNumber
kContentTypeStringToNumber[ kVideoMpegContentType ] = kVideoMpegContentTypeNumber
kContentTypeStringToNumber[ kVideoMtsContentType ] = kVideoMtsContentTypeNumber
kContentTypeStringToNumber[ kVideoQuicktimeContentType ] = kVideoQuicktimeContentTypeNumber
kContentTypeStringToNumber[ kVideoAviContentType ] = kVideoAviContentTypeNumber
kContentTypeStringToNumber[ kVideoWmvContentType ] = kVideoWmvContentTypeNumber
kContentTypeStringToNumber[ kVideoM4vContentType ] = kVideoM4vContentTypeNumber
kContentTypeStringToNumber[ kVideo3gpContentType ] = kVideo3gpContentTypeNumber


local kContentTypeNumberToString = {}

for contentType,number in pairs( kContentTypeStringToNumber ) do
	kContentTypeNumberToString[ number ] = contentType
end


WFOzContentTypes.kRawContentType = 'application/octet-stream'
--------------------------------------------------------------------------------
-- Extension information
local kExtensionInfo = {
	{ format = 'jpg', type = kImageJpegContentType, desc = ZSTR "$$$/WFOzContentTypes/image_jpg=Jpg photos" },
	{ format = 'jpeg', type = kImageJpegContentType, desc = ZSTR "$$$/WFOzContentTypes/image_jpeg=Jpeg photos" },
	{ format = 'jpe', type = kImageJpegContentType, desc = ZSTR "$$$/WFOzContentTypes/image_jpe=Jpe photos" },
	{ format = 'jfif', type = kImageJpegContentType, desc = ZSTR "$$$/WFOzContentTypes/image_jfif=Jfif photos" },
	{ format = 'png', type = kImagePngContentType, desc = ZSTR "$$$/WFOzContentTypes/image_png=Png photos" },
	{ format = 'psd', type = kImagePsdContentType, desc = ZSTR "$$$/WFOzContentTypes/image_psd=Psd photos" },
	{ format = 'tiff', type = kImageTiffContentType, desc = ZSTR "$$$/WFOzContentTypes/image_tiff=Tiff photos" },
	{ format = 'tif', type = kImageTiffContentType, desc = ZSTR "$$$/WFOzContentTypes/image_tif=Tif photos" },
	{ format = 'heic', type = kImageHeicContentType, desc = ZSTR "$$$/WFOzContentTypes/image_heic=Heic photos" },
	{ format = 'heif', type = kImageHeicContentType, desc = ZSTR "$$$/WFOzContentTypes/image_heif=Heif photos" },
	{ format = 'hif', type = kImageHeicContentType, desc = ZSTR "$$$/WFOzContentTypes/image_hif=Hif photos" },
	{ format = 'avif', type = kImageAvifContentType, desc = ZSTR "$$$/WFOzContentTypes/image_avif=Avif photos" },
	{ format = 'jxl', type = kImageJxlContentType, desc = ZSTR "$$$/WFOzContentTypes/image_jxl=Jxl photos" },
	{ format = 'dng', type = kImageDngContentType, desc = ZSTR "$$$/WFOzContentTypes/image_dng=Dng photos" },
	
	{ format = 'mpe', type = kVideoMpegContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mpe=Mpe videos" },
	{ format = 'mpeg', type = kVideoMpegContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mpeg=Mpeg videos" },
	{ format = 'mpg', type = kVideoMpegContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mpg=Mpg videos" },
	{ format = 'mpg4', type = kVideoMp4ContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mpg4=Mpg4 videos" },
	{ format = 'mp4', type = kVideoMp4ContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mp4=Mp4 videos" },
	{ format = 'mov', type = kVideoQuicktimeContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mov=Mov videos" },
	{ format = 'qt', type = kVideoQuicktimeContentType, desc = ZSTR "$$$/WFOzContentTypes/video_qt=Qt videos" },
	{ format = 'avi', type = kVideoQuicktimeContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mov=Mov videos" },
	{ format = 'wmv', type = kVideoWmvContentType, desc = ZSTR "$$$/WFOzContentTypes/video_wmv=Wmv videos" },
	{ format = 'm4v', type = kVideoM4vContentType, desc = ZSTR "$$$/WFOzContentTypes/video_m4v=M4v videos" },
	--r3d = , --AWS as of now, doesn't support RED( or anyother ) video format
	{ format = 'mts', type = kVideoMtsContentType, desc = ZSTR "$$$/WFOzContentTypes/video_mts=Mts videos" },
	{ format = 'm2t', type = kVideoMtsContentType, desc = ZSTR "$$$/WFOzContentTypes/video_m2t=M2t videos" },
	{ format = 'm2ts', type = kVideoMtsContentType, desc = ZSTR "$$$/WFOzContentTypes/video_m2ts=M2ts videos" },
	{ format = '3gp', type = kVideo3gpContentType, desc = ZSTR "$$$/WFOzContentTypes/video_3gp=3gp videos" },
	{ format = '3gpp', type = kVideo3gpContentType, desc = ZSTR "$$$/WFOzContentTypes/video_3gpp=3gpp videos" },
}

--------------------------------------------------------------------------------
-- Extension mappings

local kExtensionToContentType = {}
local kContentTypeToLocalizedString = {}
-- localized string have to match with kExtensionToContentType data
-- localized strings are needed for search and hinting

for i,info in ipairs( kExtensionInfo ) do
	kExtensionToContentType[ info.format ] = info.type
	kContentTypeToLocalizedString[ info.format ] = info.desc
end

kContentTypeToLocalizedString[ WFOzContentTypes.kRawContentType ] = ZSTR "$$$/WFOzContentTypes/image_raw=Raw photos"

--------------------------------------------------------------------------------
-- these are the mime type starts
WFOzContentTypes.kImageContentTypeStartString = "image/"
WFOzContentTypes.kVideoContentTypeStartString = "video/"

--------------------------------------------------------------------------------
-- localized string have to match with kExtensionToContentType data
-- localized strings are needed for search and hinting
--------------------------------------------------------------------------------

WFOzContentTypes.kVideoSubTypeString = 'video'
WFOzContentTypes.kImageSubTypeString = 'image'
--------------------------------------------------------------------------------

function WFOzContentTypes.getContentTypeNumberRangeForSubType( subtype )
	
	if subtype == WFOzContentTypes.kVideoSubTypeString then
		return kVideoContentTypeNumberLowEnd, kVideoContentTypeNumberHighEnd
	elseif subtype == 'jpeg' or subtype == WFOzContentTypes.kImageSubTypeString or subtype == nil then
		return kImageContentTypeNumberLowEnd, kImageContentTypeNumberHighEnd
	end
	error( "Unknown subtype: " .. subtype )
	
end

--------------------------------------------------------------------------------

function WFOzContentTypes.isVideo( contentType )
	if type( contentType ) ~= 'string' then
		return false
	end
	return not not ( string.find( contentType, "^video/" ) )
		or contentType == 'application/octet-stream; video/*'
end

--------------------------------------------------------------------------------

function WFOzContentTypes.isRawImage( contentType )

	if type( contentType ) ~= 'string' then
		return false
	end
	
	local contentTypeNumber = WFOzContentTypes.getContentTypeNumberForContentType( contentType )
		
	if contentTypeNumber == kImageDngContentTypeNumber or contentTypeNumber == kImageRawContentTypeNumber then
		return true
	end
	
	return false
	
end


--------------------------------------------------------------------------------

function WFOzContentTypes.getContentTypeNumberForContentType( contentType )
	
	if not contentType then
		return
	end
	
	contentType = contentType:gsub( ";.*", "" )
	
	local result = kContentTypeStringToNumber[ contentType ]
	
	if not result then
		contentType = string.gsub( contentType, '/.+$', '/*' )
		result = kContentTypeStringToNumber[ contentType ]
	end
	
	return result
	
end

--------------------------------------------------------------------------------

function WFOzContentTypes.contentTypeForContentTypeNumber( contentTypeNumber, optOmitJpegImage )
	if optOmitJpegImage and contentTypeNumber == kImageJpegContentTypeNumber then
		return
	end
	return contentTypeNumber and kContentTypeNumberToString[ contentTypeNumber ]
end

--------------------------------------------------------------------------------

function WFOzContentTypes.contentTypeForExtention( extension )
	return kExtensionToContentType[ extension ]
end

--------------------------------------------------------------------------------

function WFOzContentTypes.getlocalizedStringForExtention( extension )
	return kContentTypeToLocalizedString[ extension ]
end

--------------------------------------------------------------------------------

function WFOzContentTypes.contentTypeForSubtype( subtype )

	-- This should only be used if the contentType has not been set you should
	-- use docValue.importSource.contentType as it is specific to the actual
	-- content.

	if subtype == 'preset' or subtype == 'profile' or subtype == 'lens_profile' then
		return 'application/rdf+xml'
	elseif subtype == 'camera_profile' then
		return 'application/octet-stream'
	elseif subtype == 'image' or subtype == 'deleted_image' then
		return 'application/octet-stream'
	elseif subtype == 'video' or subtype == 'deleted_video' then
		return 'application/octet-stream; video/*'
	else
		return 'application/octet-stream'
	end

end

--==============================================================================

return WFOzContentTypes
