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

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

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

local kSwapOrientations = {
	AD = true,
	DA = true,
	BC = true,
	CB = true,
}

local function formatDimensions( width, height, orientation )

	if width and height then

		if orientation and kSwapOrientations[ orientation ] then
			width, height = height, width
		end
	
		return 'ZSTR', "$$$/AgLibrary/Exif/DimensionsTemplate=^1 x ^2",
							tostring( width ), tostring( height )

	end

end

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

add {
	id = 'com.adobe.combinedImageFileDimensions',
	title = "$$$/AgLibrary/MetadataFormatters/imageFileDimensions=Dimensions",

	format_asset = function( self, assetDoc, options )

		local cwidth = derefOpt( assetDoc, 'develop', 'croppedWidth' )
		local cheight = derefOpt( assetDoc, 'develop', 'croppedHeight' )
		local fwidth = derefOpt( assetDoc, 'importSource', 'originalWidth' )
		local fheight = derefOpt( assetDoc, 'importSource', 'originalHeight' )

		if cwidth and cheight and ( cwidth ~= fwidth or cheight ~= fheight ) then

			return 'ZSTR',
				"$$$/AgLibrary/MetadataFormatters/combinedImageFileDimensions=^1 cropped to ^2",
				{ formatDimensions( fwidth, fheight, assetDoc.orientation ) },
				{ formatDimensions( cwidth, cheight, assetDoc.orientation ) }

		end

		return formatDimensions( fwidth, fheight, assetDoc.orientation )

	end,

}

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

add {
	id = 'com.adobe.megapixels',
	title = "$$$/AgLibrary/MetadataFormatters/Megapixels=Megapixels",

	format_asset = function( self, assetDoc, options )

		local width = derefOpt( assetDoc, 'importSource', 'originalWidth' )
		local height = derefOpt( assetDoc, 'importSource', 'originalHeight' )

		if width and height then

			local megapixels = width * height / 1000000
			return 'ZSTR', "$$$/AgLibrary/MetadataFormatters/MegapixelsTemplate=^1 MP",
					{ 'number', megapixels, 1 }

		end

	end

}

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

add {
	id = 'com.adobe.imageFileDimensions',
	title = "$$$/AgLibrary/MetadataFormatters/imageFileDimensions=Dimensions",
	description = "$$$/AgLibrary/MetadataFormatters/imageFileDimensions/description=File dimensions",

	format_asset = function( self, assetDoc, options )

		local width = derefOpt( assetDoc, 'importSource', 'originalWidth' )
		local height = derefOpt( assetDoc, 'importSource', 'originalHeight' )

		if width and height then

			return formatDimensions( width, height )

		end

	end,

}

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

add {
	id = 'com.adobe.imageCroppedDimensions',
	title = "$$$/AgLibrary/MetadataFormatters/imageCroppedDimensions=Cropped",
	full_title = "$$$/AgLibrary/MetadataFormatters/imageCroppedDimensionsFull=Cropped Dimensions",

	-- FIX_ME_ABR : if the same as dimension, return nil

	format_asset = function( self, assetDoc, options )

		local subtype = derefOpt( assetDoc, '_serverProvidedMetadata', 'subtype' )

		if subtype == 'video' then

			return

		end

		local cwidth = derefOpt( assetDoc, 'develop', 'croppedWidth' )
		local cheight = derefOpt( assetDoc, 'develop', 'croppedHeight' )
		local fwidth = derefOpt( assetDoc, 'importSource', 'originalWidth' )
		local fheight = derefOpt( assetDoc, 'importSource', 'originalHeight' )

		if not cwidth then cwidth = fwidth end

		if not cheight then cheight = fheight end

		if 		( cwidth ~= fwidth or cheight ~= fheight )

			or	( options and not options.pruneRedundantFields ) then
			return formatDimensions( cwidth, cheight, assetDoc.orientation )

		end

	end,

}

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

return formatters
