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

WFCatFormatter_attributes.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.basic',
}

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

add {
	id = 'com.adobe.rating',
	title = "$$$/AgLibrary/MetadataFormatters/Rating=Rating",
	data_type = 'rating',

	format_asset = function( self, assetDoc, options, wfOzUser )

		local rating = derefOpt( assetDoc, 'ratings', wfOzUser:ozUserId(), 'rating' )
		if rating then
			return 'rating', rating
		end

	end,

	asset_overlay = function( self, value, wfCatalog )

		-- this field is runtime enforced to be an integer from 1 to 5
		-- nil is also permitted

		if type( value ) == 'string' then

			value = tonumber( value )

		end

		if value == 0 or value == nil then
			value = { _delete = true }
		else
			value = {
				date = rawget( wfCatalog, 'changeDate' ),
					-- FIXME [scouten 2015-04-09]: Used by testing code.
				rating = value,
			}
		end

		return {
			ratings = {
				[ wfCatalog:ozUserId() ] = value
			}
		}

	end,
}

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

add {
	id = 'com.adobe.ratingAndLabel',
	title = "$$$/AgLibrary/MetadataFormatters/RatingAndLabel=Rating and Label",

	format_asset = function( self, assetDoc, options, wfCatalog )

		local l = derefOpt( assetDoc, 'xmp', 'xmp', 'Label' )
		local rating = derefOpt( assetDoc, 'ratings', wfCatalog:ozUserId(), 'rating' )

		if not l or l == "" then l = "-" end

		return 'literal', l .. " / " .. tostring( rating or "-" )

	end,
}

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

local function pickName( pick )

	if pick == 'pick' then
		return { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Pick/Flagged=Flagged" }
	elseif pick == 'reject' then
		return { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Pick/Rejected=Rejected" }
	else
		return { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Pick/Unflagged=Unflagged" }
	end

end

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

add {
	id = 'com.adobe.compositeAttributes',
	title = "$$$/AgLibrary/MetadataFormatters/Attributes=Common Attributes",

	format_asset = function( self, assetDoc, options, wfCatalog )

		local label = derefOpt( assetDoc, 'xmp', 'xmp', 'Label' )
		local rating = derefOpt( assetDoc, 'ratings', wfCatalog:ozUserId(), 'rating' )

		if label == "" then

			label = nil

		end

		local review = derefOpt( assetDoc, 'reviews', wfCatalog:ozUserId(), 'flag' )
		return 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Attributes/Display=^1  ^U+00B7  ^2  ^U+00B7  ^3^4",
				pickName( review ),
				( rating and rating ~= 0 )
					and { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Attributes/Rating/Labeled=Rating ^1", rating }
					or  { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Attributes/Rating/None=No Rating" },
				label
					and { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Attributes/QuotedLabel=^1", label }
					or  { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Attributes/Label/None=No Label" },
				assetDoc.isVirtualCopy
					and { 'ZSTR', "$$$/AgLibrary/MetadataFormatters/Attributes/VC=  ^U+00B7  Virtual Copy" }
					or  ""

	end,
}

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

add {
	id = 'com.adobe.rating.string',
	title = "$$$/AgLibrary/MetadataFormatters/Rating=Rating",

	format_asset = function( self, assetDoc, options, wfCatalog )

		local r = derefOpt( assetDoc, 'ratings', wfCatalog:ozUserId(), 'rating' )
		if r then
			return 'literal', tostring( r )
		end

	end,
}

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

add {
	id = 'com.adobe.pick',
	title = "$$$/AgLibrary/MetadataFormatters/Pick=Pick",
	data_type = 'pick',

	format_asset = function( self, assetDoc, options, wfOzUser )

		local flag = derefOpt( assetDoc, 'reviews', wfOzUser:ozUserId(), 'flag' )
		return 'pick', flag or 0

	end,

	asset_overlay = function( self, value, wfCatalog )

		-- this field is runtime enforced to be an integer from -1 to 1
		-- nil is also permitted

		if type( value ) == 'string' then
			value = tonumber( value )
		end

		if value == 0 then
			value = 'unflagged'
		elseif value == 1 then
			value = 'pick'
		elseif value == -1 then
			value = 'reject'
		else
			-- TODO [arahn 2015-04-20]: How do we handle invalid input.
			assert( false, 'Invalid pick value' )
		end

		if value then
			value = {
				date = rawget( wfCatalog, 'changeDate' ),
					-- FIXME [scouten 2015-04-09]: Used by testing code.
				flag = value,
			}

			return {
				reviews = {
					[ wfCatalog:ozUserId() ] = value
				}
			}
		end

	end,
}

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

add {
	ids = {
		'com.adobe.colorLabels',
		'com.adobe.colorLabels.string',
	},
	title = "$$$/AgLibrary/MetadataFormatters/ColorLabel=Label",
	data_type = 'colorLabel',

	format_asset = function( self, assetDoc )

		local r = derefOpt( assetDoc, 'xmp', 'xmp', 'Label' )
		if r then
			return 'literal', r
		end

	end,

	asset_overlay = function( self, value )

		if value == nil then
			value = { _delete = true }
		end

		return {
			xmp = {
				xmp = {
				 Label = value,
				}
			}
		}

	end,
}

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

add {
	id = 'com.adobe.importedByUser',
	title = "$$$/AgLibrary/MetadataFormatters/ImportedByUser=Imported By",

	format_asset = function( self, assetDoc, options, wfCatalog )

		local r = derefOpt( assetDoc, '_serverProvidedMetadata', 'importedByUser', 'id' )
		
		if not r and assetDoc._spaceId then
			r = wfCatalog:ozUserId()
		end
		
		if r then
			return 'literal', tostring( r )
		end

	end,
}

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

return formatters
