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

WFCatExportBiggestRenditionProvider.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.

----------------------------------------------------------------------------]]--
	-- RxLua
local Rx = require 'Rx'

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

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

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

local logger = WFLogger 'WFCatExportBiggestRenditionProvider'
	--:setLevel( 'DEBUG' )

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

-- @mark WFCatExportBiggestRenditionProvider
--------------------------------------------------------------------------------

local WFCatExportBiggestRenditionProvider = {
	_canFallbackTo2048 = false,
}

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

function WFCatExportBiggestRenditionProvider:__init( canFallbackTo2048 )
	self._canFallbackTo2048 = canFallbackTo2048
end

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

function WFCatExportBiggestRenditionProvider:exportAsset( request, wfCatalog, exportPixelProviderTask )
	
	--Basic input validation
	local assetId = request.id 
	
	if not assetId then
		logger:warnf( 'Export failed as asset id supplied is nil' )
		return Rx.Observable.throw( 'invalid_asset_id' )
	end
	
	
	local destinationFolder = request.settings and request.settings.destinationFolder
                                                               
	if not destinationFolder or WFFileUtils.exists( destinationFolder ) ~= 'directory' then
		logger:warnf( "Destination folder '%s' seems to be invalid", destinationFolder )
		return Rx.Observable.throw( 'invalid_destination' )
	end
	
	--Check if we really need to update metadata
	
	return exportPixelProviderTask
			:getBiggestRenditionPath( assetId, self._canFallbackTo2048 )
			:take( 1 )
			:map( 
				function( success, biggestRenditionPath, renditionType )
			
					if not success then
						
						logger:warnf( "failed to export biggest rendition for assetId %s because: %s", biggestRenditionPath )

						error( 'rendition_unavailable' )

					end
				
					return biggestRenditionPath

				end )
		
		:flatMapLatest( 
			function( biggestRenditionPath, renditionType )

				request.sourceImagePath = biggestRenditionPath
				request.tryWithProxy = false
				
				if not request.settings then
					request.settings = {}
				end
				
				request.settings.applyXmp = false
				request.settings.fallbackToProxySize = false
				
				if renditionType == "2048" then
					request.warnings = { "exported_with_fallback" }
				end
				
				local exportGeneriProvider = require 'WFCatExportGenericProvider'
				return exportGeneriProvider:exportAsset( request, wfCatalog, exportPixelProviderTask )
			
			end )
	
end


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

return Rx.Class 'AgImportStore'( WFCatExportBiggestRenditionProvider )
