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

WFCatFormatter_dates.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems IncorporatedAll Rights Reserved.

NOTICE: All information contained herein is, and remain 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 may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

	-- WF.core.utils
local KSTableUtils = require 'KSTableUtils'

	-- WF.oz_client.managed_catalog.metadata_formatters
local WFCatFormatterUtils = require 'WFCatFormatterUtils'

local derefOpt = KSTableUtils.derefOpt

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

local formatters, add = WFCatFormatterUtils.newSuite {
	default_category = 'com.adobe.formatterCategories.exif',
	default_data_type = "dateTime",
}

--==============================================================================
-- @mark	Utilities
--==============================================================================

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

local function extractDate( assetDoc, ... )
	
	return derefOpt( assetDoc, 'xmp', ... )
	
end

--==============================================================================
-- @mark	Formatters
--==============================================================================

--add {
--	id = 'com.adobe.goToDate',
--	utility = true,
--
--	format_asset = function( self, assetDoc, options )
--
--		local exif = assetDoc.harvestedExif
--
--		if exif.dateYear and exif.dateMonth and exif.dateDay then
--
--			local dateStr = string.format( "%.4d-%.2d-%.2d", exif.dateYear, exif.dateMonth, exif.dateDay )
--			return dateStr
--
--		end
--
--	end,
--
--}

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

add {
	id = 'com.adobe.dateTime',
	title = "$$$/AgLibrary/Exif/DateTime=Date Time",

	format_asset = function( self, assetDoc, options )

		local val = extractDate( assetDoc, 'xmp', 'ModifyDate' )

		if val then
			return 'mediumDateTime', val
		end

	end,

}

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

add {
	id = 'com.adobe.dateTime.onlyIfUnique',
	title = "$$$/AgLibrary/Exif/DateTime=Date Time",

		-- this formatter is almost identical to com.adobe.dateTime, except
		-- that it's default behavior is to *not* show anything if the dateTimeOriginal
		-- value is the same as dateTime.  Setting options.pruneRedundantFields to
		-- true or false will force the behavior to be the same as com.adobe.dateTimeOriginal
		-- (this formatter only behaves differently when options.pruneRedundantFields is
		-- nil).

	format_asset = function( self, assetDoc, options )

		local dateTimeString = extractDate( assetDoc, 'xmp', 'ModifyDate' )

		if options == nil or options.pruneRedundantFields ~= false then

			local dateTimeOriginalString = extractDate( assetDoc, 'exif', 'DateTimeOriginal' )

			if dateTimeString == dateTimeOriginalString then
				return
			end

		end
		
		if dateTimeString then
			return 'mediumDateTime', dateTimeString
		end

	end,

}

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

add {
	id = 'com.adobe.dateTime.dateOnly',
	title = "$$$/AgLibrary/Exif/DateTime/Date=Date",

	format_asset = function( self, assetDoc, options )

		local val = extractDate( assetDoc, 'xmp', 'ModifyDate' )

		if val then
			return 'mediumDate', val
		end

	end,

}

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

add {
	id = 'com.adobe.dateTime.timeOnly',
	title = "$$$/AgLibrary/Exif/DateTime/Time=Time",

	format_asset = function( self, assetDoc, options )

		local val = extractDate( assetDoc, 'xmp', 'ModifyDate' )

		if val then
			return 'mediumTime', val
		end

	end,

}

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

add {
	id = 'com.adobe.captureDateTimeLong',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal/descriptiveTitle=Capture Date/Time",

	format_asset = function( self, assetDoc, options )

		local val = derefOpt( assetDoc, 'captureDate' )
				or extractDate( assetDoc, 'exif', 'DateTimeOriginal' )
				or extractDate( assetDoc, 'photoshop', 'DateCreated' )
				or extractDate( assetDoc, 'exif', 'DateTimeDigitized' )
				or extractDate( assetDoc, 'xmp', 'CreateDate' )
				or extractDate( assetDoc, 'xmp', 'ModifyDate' )

			-- unlike imageDate (capture time with various fallbacks), this will try to show that date/time, but
			-- will additionally fall back to the dateTime if there's no capture time that can be inferred, assuming
			-- that value is better than showing nothing.
		
		if val then
			
			-- use rawDateTime instead of 'longDateTime'
			-- NSDateFormatter does not use the correct system locale to create the localized date string
			-- in Daemon process on Mac.
			-- For instance, the date string is not localized if you change the system language
			-- to 'Chinese' in the system preferences 'Language & Region'.
			
			return 'rawDateTime', val
		end

	end,

}

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

add {
	id = 'com.adobe.captureDateTime',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal/descriptiveTitle=Capture Date/Time",

	format_asset = function( self, assetDoc, options )

		local val = derefOpt( assetDoc, 'captureDate' )
				or extractDate( assetDoc, 'exif', 'DateTimeOriginal' )
				or extractDate( assetDoc, 'photoshop', 'DateCreated' )
				or extractDate( assetDoc, 'exif', 'DateTimeDigitized' )
				or extractDate( assetDoc, 'xmp', 'CreateDate' )
				or extractDate( assetDoc, 'xmp', 'ModifyDate' )

			-- unlike imageDate (capture time with various fallbacks), this will try to show that date/time, but
			-- will additionally fall back to the dateTime if there's no capture time that can be inferred, assuming
			-- that value is better than showing nothing.
		
		if val then
			return 'mediumDateTime', val
		end

	end,

}

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

add {
	id = 'com.adobe.captureDateTimeShort',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal/descriptiveTitle=Capture Date/Time",

	format_asset = function( self, assetDoc, options )

		local val = derefOpt( assetDoc, 'captureDate' )
				or extractDate( assetDoc, 'exif', 'DateTimeOriginal' )
				or extractDate( assetDoc, 'photoshop', 'DateCreated' )
				or extractDate( assetDoc, 'exif', 'DateTimeDigitized' )
				or extractDate( assetDoc, 'xmp', 'CreateDate' )
				or extractDate( assetDoc, 'xmp', 'ModifyDate' )

			-- unlike imageDate (capture time with various fallbacks), this will try to show that date/time, but
			-- will additionally fall back to the dateTime if there's no capture time that can be inferred, assuming
			-- that value is better than showing nothing.
		
		if val then
			return 'shortDateTime', val
		end

	end,

}

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

add {
	id = 'com.adobe.dateTimeOriginal',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal=Date Time Original",

	format_asset = function( self, assetDoc, options )

		local val = extractDate( assetDoc, 'exif', 'DateTimeOriginal' )
		if val then
			return 'mediumDateTime', val
		end

	end,

}

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

add {
	id = 'com.adobe.dateTimeDigitized',
	title = "$$$/AgLibrary/Exif/DateTimeDigitized=Date Time Digitized",

	format_asset = function( self, assetDoc, options )

		local dateTimeDigitizedString = extractDate( assetDoc, 'exif', 'DateTimeDigitized' )

		if options and options.pruneRedundantFields then

			local dateTimeString = extractDate( assetDoc, 'xmp', 'ModifyDate' )

			if dateTimeString == dateTimeDigitizedString then
				return
			end

		end
		
		if dateTimeDigitizedString then
			return 'mediumDateTime', dateTimeDigitizedString
		end

	end,

}

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

add {
	id = 'com.adobe.captureDate',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal/Date=Capture Date",

	format_asset = function( self, assetDoc, options )

		local val = derefOpt( assetDoc, 'captureDate' )
				or extractDate( assetDoc, 'exif', 'DateTimeOriginal' )
				or extractDate( assetDoc, 'photoshop', 'DateCreated' )
				or extractDate( assetDoc, 'exif', 'DateTimeDigitized' )
				or extractDate( assetDoc, 'xmp', 'CreateDate' )
				or extractDate( assetDoc, 'xmp', 'ModifyDate' )

		if val then
			return 'mediumDate', val
		end

	end,

}

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

add {
	id = 'com.adobe.captureTime',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal/Time=Capture Time",

	format_asset = function( self, assetDoc, options )

		local val = derefOpt( assetDoc, 'captureDate' )
				or extractDate( assetDoc, 'exif', 'DateTimeOriginal' )
				or extractDate( assetDoc, 'photoshop', 'DateCreated' )
				or extractDate( assetDoc, 'exif', 'DateTimeDigitized' )
				or extractDate( assetDoc, 'xmp', 'CreateDate' )
				or extractDate( assetDoc, 'xmp', 'ModifyDate' )

		if val then
			return 'mediumTime', val
		end

	end,

	action_type = "edit",
}

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

add {
	id = 'com.adobe.dateTimeOriginal.onlyIfUnique',
	title = "$$$/AgLibrary/Exif/DateTimeOriginal/Label=Capture Time",

		-- this formatter is almost identical to com.adobe.dateTimeOriginal, except
		-- that it's default behavior is to *not* show anything if the dateTimeOriginal
		-- value is the same as dateTime.  Setting options.pruneRedundantFields to
		-- true or false will force the behavior to be the same as com.adobe.dateTimeOriginal
		-- (this formatter only behaves differently when options.pruneRedundantFields is
		-- nil).

	format_asset = function( self, assetDoc, options )

		local dateTimeOriginalValue = extractDate( assetDoc, 'exif', 'DateTimeOriginal' )

		if options == nil or options.pruneRedundantFields ~= false then

			local dateTimeValue = extractDate( assetDoc, 'xmp', 'ModifyDate' )

			if dateTimeValue == dateTimeOriginalValue then
				return
			end

		end
		
		if dateTimeOriginalValue then
			return 'mediumDateTime', dateTimeOriginalValue
		end

	end,

}

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

return formatters
