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

WFCatImportReadMetadataTask.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.

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

	-- WF.core.logging
local WFLogger = require 'WFLogger'

	-- WF.core.performance
local WFPerformanceReport = require 'WFPerformanceReport'

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local _ = require 'WFArguments_uuid'

	-- WF.nativevideo
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.managed_catalog.video
local WFVideoUtils = require 'WFVideoUtils'

	-- lightroomcc
local NmbAcr = require 'NmbAcr'

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

local check = assert( WFArguments.check )

local log = WFLogger 'WFCatImportReadMetadataTask'
	-- :setLevel( 'debug' )

local WFCrContext = assert( NmbAcr.WFCrContext )
local WFNativeVideoContext = WFPlatformInfo.useNativeVideo and assert( NmbAcr.WFNativeVideoContext )

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

-- @mark WFCatImportReadMetadataTask namespace

local WFCatImportReadMetadataTask = {}

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

function WFCatImportReadMetadataTask.readMetadataForFileAtPath( filePath )

	check( '+string' )

	log:infof( "readMetadataForFileAtPath %s", filePath )

	--[[
	SPAM [ ashj:2016-04-29 ] We are able to use the same function
	on the context irrespective of the whether it is originating
	form native video or from acr, because both the functions have
	the same signature.
	
	Here we are ignoring the sidecar file for metadata reading.
	--]]
	
	local isVideo = WFVideoUtils.isVideoFile( filePath )
	
	local context
	if isVideo and WFPlatformInfo.useNativeVideo then

		context = WFNativeVideoContext( filePath )

	else

		context = WFCrContext( filePath )

	end
	
	-- Read metadata from file.

	return WFPerformanceReport.wrapObservable( "WFCatImportReadMetadataTask.readMetadataForFileAtPath",

		context:readMetadata()

			:map( function( metadataResults )

				metadataResults.filePath = filePath
				metadataResults.isVideo = isVideo
				return metadataResults

			end )

		:finally( function()

			-- TODO [scouten 2015-12-04]: Maybe someday we'll want to hang onto this
			-- in case we want to use it later. Not for now.

			context:close()

		end ) )

end

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

return WFCatImportReadMetadataTask
