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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All 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 WFArguments = require 'WFArguments'
local KSTableUtils = require 'KSTableUtils'
local KSPathUtils = require "KSPathUtils"
local WFRxThrottledBatchedApplyOverlayToIds = require 'WFRxThrottledBatchedApplyOverlayToIds'

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

local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.core.rxlua
local WFRxRunUntilDone = require 'WFRxRunUntilDone'

local WFRxPromiseBridge = require "WFRxPromiseBridge"

	-- WF.oz_client.catalog
local OzCatalogUploadStatus = require 'OzCatalogUploadStatus'

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

local deref = KSTableUtils.derefOpt

local runUntilDone = WFRxRunUntilDone

local asKSPromise = WFRxPromiseBridge

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

local LrmCatAsset = {}

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

function LrmCatAsset.deleteFilesForDocValue( docValue )

	local localOnly = docValue._localOnly
	
	local originalFilePath = deref( localOnly, 'originalFilePath' )
	originalFilePath = OzCatalogUploadStatus.checkAndCorrectLocalPath( originalFilePath )

	local latestOriginalFilePath = deref( localOnly, 'latestOriginal', 'filePath' )

	latestOriginalFilePath = OzCatalogUploadStatus.checkAndCorrectLocalPath( latestOriginalFilePath )

	if originalFilePath then
		pcall( WFFileUtils.deleteFile, originalFilePath )
	end

	if latestOriginalFilePath then
		pcall( WFFileUtils.deleteFile, latestOriginalFilePath )
	end

	local proxyFilePath = deref( localOnly, 'proxy', 'filePath' )

	proxyFilePath = OzCatalogUploadStatus.checkAndCorrectLocalPath( proxyFilePath )

	if proxyFilePath then
		pcall( WFFileUtils.deleteFile, proxyFilePath )
	end

	local externalXmp = deref( localOnly, 'files', 'xmp_develop', 'path' )
	if externalXmp then

		local documentsDir = KSPathUtils.getStandardFilePath 'carouselDocuments'

		pcall( WFFileUtils.deleteFile, KSPathUtils.child( documentsDir, externalXmp ) ) 
	end
end

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

function LrmCatAsset.deleteAssets( catalog, assetIds )

	WFArguments.check( 'OzCatalogService', 'table' )

	local docStore = catalog:docStore()

	local delete =  docStore:bulkDocValue( assetIds )
		:flatMapLatest( function( docValueArray )

			for i, docValue in ipairs( docValueArray ) do

				LrmCatAsset.deleteFilesForDocValue( docValue )

			end

			local overlay = {
				_deleted = true,
				_localOnly = {
					originalFilePath = { _delete = true },
					latestOriginal = {
						filePath = { _delete = true },
					},
					proxy = {
						filePath = { _delete = true },
					},
					files = {
						xmp_develop = {
							path = { _delete = true },
						}
					}
				}
			}

			return WFRxThrottledBatchedApplyOverlayToIds( "deleteAssets", docStore, overlay, assetIds, 100, 10 )

		end )
		
	if WFPlatformInfo.isUnitTest() then
		return delete
	end
	
	return delete
		:call( runUntilDone )

end

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

function LrmCatAsset.deleteAssetsP( catalog, assetIds )

	return LrmCatAsset.deleteAssets( catalog, assetIds )
		:call( asKSPromise )


end
--------------------------------------------------------------------------------

return LrmCatAsset
