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

WFCatFormatter_iptc.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, unused = WFCatFormatterUtils.newSuite {
	default_category = 'com.adobe.formatterCategories.iptc',
}

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

local function maybeFlattenVal( val )
	if type( val ) == "table" then
		val = val[ 1 ]
	end
	return val
end
--------------------------------------------------------------------------------

local function addFields( fieldTable )

	for _, info in ipairs( fieldTable ) do
		
		if type( info ) == "table" then
			local fullField = info[ 1 ]

			add {
				id = fullField,
								
				format_asset = function( self, assetDoc )
					local val = derefOpt( assetDoc, 'xmp', unpack( info, 2, #info ) )
					
					local fieldType = fieldTable.fieldTypes or 'literal'
					
					if val then
					
						val = maybeFlattenVal( val )
					
						return fieldType, val
						
					end
				end,
				
				asset_overlay = function( self, value )
					local step = {}
					local overlay = { xmp = step }
					for i = 2, #info-1 do
						local next = {}
						step[ info[ i ] ] = next
						step = next
					end
					step[ info[ #info ] ] = value
					return overlay
				end
			}
		end
	end

end

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

local function addFieldsWithReadOnlyFallback( fieldTable )

	for _, info in ipairs( fieldTable ) do
		
		if type( info ) == "table" then
			local fullField = info[ 1 ]

			add {
				id = fullField,
								
				format_asset = function( self, assetDoc )
				
					local fieldType = fieldTable.fieldTypes or 'literal'
					
					local val = derefOpt( assetDoc, 'xmp', unpack( info[ 2 ] ) )
					
					if not val then
						
						-- no value in the editable area - check for read only fallback value
						val = derefOpt( assetDoc, unpack( info[ 3 ] ) )
					
					end 
					
					if val then 
						
						val = maybeFlattenVal( val )
					
						return fieldType, val
						
					end
				end,
				
				asset_overlay = function( self, value )
					local writablePath = info[2]
					local step = {}
					local overlay = { xmp = step }
					for i = 1, #writablePath - 1 do
						local next = {}
						step[ writablePath[ i ] ] = next
						step = next
					end
					step[ writablePath[ #writablePath ] ] = value
					return overlay
				end
			}
		end
	end

end

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

unused 'com.adobe.creatorAddress'
unused 'com.adobe.creatorCity'
unused 'com.adobe.creatorState'
unused 'com.adobe.creatorZip'
unused 'com.adobe.creatorCountry'
unused 'com.adobe.creatorWorkPhone'
unused 'com.adobe.creatorWorkEmail'
unused 'com.adobe.creatorWorkWebsite'
unused 'com.adobe.creatorJobTitle'

addFields {
	category = 'com.adobe.formatterCategories.iptc.creator',
	
	{ 'com.adobe.creator', 'dc', 'creator' },
}

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

add {
	category = 'com.adobe.formatterCategories.keywords',
	
	id = 'com.adobe.keywords',
    title = "$$$/AgLibrary/MetadataFormatters/Keywords=Keywords",
    category = 'com.adobe.formatterCategories.keywords',
    
    format_asset = function( self, assetDoc )
         local keywords = {}
         for k in pairs( derefOpt( assetDoc, 'xmp', 'dc', 'subject' ) or {} ) do
             table.insert( keywords, k )
         end
         -- TODO [arahn 2015-01-28]: Use localized sort here
         table.sort( keywords )
         -- TODO [arahn 2015-01-28]: Is , localization friendly??
         return 'literal', table.concat( keywords, ", " )
     end,
}

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

unused 'com.adobe.iptcSubjectCode'
unused 'com.adobe.supplementalCategories'

addFields {
	category = 'com.adobe.formatterCategories.iptc.content',
	
	{ 'com.adobe.headline', 'photoshop', 'Headline' },
	{ 'com.adobe.descriptionWriter', 'photoshop', 'CaptionWriter' },
	{ 'com.adobe.category', 'photoshop', 'Category' },
}

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

unused 'com.adobe.intellectualGenre'
unused 'com.adobe.scene'

addFields {
	category = 'com.adobe.formatterCategories.iptc.image',
	
	{ 'com.adobe.location', 'iptcCore', 'Location' },
	{ 'com.adobe.altTextAccessibility', 'iptcCore', 'AltTextAccessibility' },
	{ 'com.adobe.extendedDescriptionAccessibility', 'iptcCore', 'ExtDescrAccessibility' },
}

addFieldsWithReadOnlyFallback {
	{ 'com.adobe.city', { 'photoshop', 'City' }, { 'location', 'city' } },
	{ 'com.adobe.state', { 'photoshop', 'State' }, { 'location', 'state' } },
	{ 'com.adobe.country', { 'photoshop', 'Country' }, { 'location', 'country' } },
	{ 'com.adobe.isoCountryCode', { 'iptcCore', 'CountryCode' }, { 'location', 'isoCountryCode' } },
}

addFields {
	category = 'com.adobe.formatterCategories.iptc.image',
	
	-- FIXME [jandrew 5/14/2015]: The incoming dates are not guaranteed
	-- to be full ISO 8601 timestamps. Uncomment the following when
	-- we correclty handle all 8601 Date formats in the date formatters
	-- found in WFCatClientMetadataUtils.
	--fieldTypes  = 'mediumDateTime',
	
	{ 'com.adobe.dateCreated', 'photoshop', 'DateCreated' },
}


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

addFields {
	category = 'com.adobe.formatterCategories.iptc.status',
	
	{ 'com.adobe.title', 'dc', 'title' },
	{ 'com.adobe.jobIdentifier', 'photoshop', 'TransmissionReference' },
	{ 'com.adobe.instructions', 'photoshop', 'Instructions' },
	{ 'com.adobe.provider', 'photoshop', 'Credit' },
	{ 'com.adobe.source', 'photoshop', 'Source' },
}

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

addFields {
	category = 'com.adobe.formatterCategories.iptc.copyright',
	
	{ 'com.adobe.rightsUsageTerms', 'xmpRights', 'UsageTerms' },
	{ 'com.adobe.copyrightInfoURL', 'xmpRights', 'WebStatement' },
}

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

unused 'com.adobe.personInImage'
unused 'com.adobe.locationCreated'
unused 'com.adobe.locationShown'
unused 'com.adobe.organisationInImageName'
unused 'com.adobe.organisationInImageCode'

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

unused 'com.adobe.artworkOrObject'

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

unused 'com.adobe.additionalModelInfo'
unused 'com.adobe.modelAge'
unused 'com.adobe.minorModelAgeDisclosure'
unused 'com.adobe.modelReleaseStatus'
unused 'com.adobe.modelReleaseID'

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

unused 'com.adobe.imageSupplier'
unused 'com.adobe.registryId'
unused 'com.adobe.imageSupplierImageId'
unused 'com.adobe.maxAvailHeight'
unused 'com.adobe.maxAvailWidth'
unused 'com.adobe.digitalSourceType'

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

unused 'com.adobe.imageCreator'
unused 'com.adobe.copyrightOwner'
unused 'com.adobe.licensor'
unused 'com.adobe.propertyReleaseID'
unused 'com.adobe.propertyReleaseStatus'

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

unused 'com.adobe.digImageGUID'
unused 'com.adobe.plusVersion'

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

return formatters
