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

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

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

	-- RxLua
local Rx = require 'Rx'
local Observable = Rx.Observable

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

	-- WF.oz_client.managed_catalog
local WFOzSyncConstants = require 'WFOzSyncConstants'
local WFCatCopies = require 'WFCatCopies'

	-- WF.core.utils
local KSTableUtils = require 'KSTableUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF trash
local WFCatTrash = require 'WFCatTrash'

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

local runUntilDone = WFRxRunUntilDone

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

local LrmLibraryDelete = {}

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

local function getPusherControlFuncs( catalog )
	return {
		pauseReturningCounter = function( ... )
			return catalog:getSyncClient():pausePusherReturningCounter( ... )
		end,
		resume = function()
			catalog:getSyncClient():resumePusher()
				:call( runUntilDone )
		end,
	}
end

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

local function getGatherBinaryFilesToMoveFn()

	return function( wfCatalog, srcDocValue, dstDocValue, batchOfFilesToMoveArray )
		
		if WFPlatformInfo.isAndroid then
			-- TODO: this has to be enabled on LrA once Blob Provider feature is implemented in LrA
			return
		end

		-- Original, Proxy, and xmp related paths are part of _localOnly.files which
		-- is copied so no need to move those binaries.

		local copyOptions = {
			needToCopyOriginal = false,
			needToCopyProxy = false,
		}

		local _, filesToMove =
				WFCatCopies.calculateCopyPathsAndUpdateDocValue( wfCatalog, srcDocValue, dstDocValue, copyOptions )
					-- Note that this function DOES NOT update teh dstDocValue in the doc store. It simply applies
					-- an overlay containing the updated paths to the dstDocValue table value. You'll need to update
					--the doc value in the doc store after this is called using updatedDstDocValue.
					
		KSTableUtils.appendArray( batchOfFilesToMoveArray, filesToMove )
		
		return batchOfFilesToMoveArray
		
	end 

end

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

function LrmLibraryDelete.deleteAssetsModel( self, assetIds, origin )

	local result = self._wfCatalogStream
		:flatMap( function( wfCatalog )
			local pusherControl = getPusherControlFuncs( self.catalog )
			local gatherBinaryFilesToMove
			
			if WFPlatformInfo.isiOS then
				gatherBinaryFilesToMove = getGatherBinaryFilesToMoveFn()
			end
			
			return WFCatTrash.moveAssetsToTrash( wfCatalog, assetIds, pusherControl, nil, gatherBinaryFilesToMove, origin )
		end )
		:call( runUntilDone )
	
	return {
		value = result
	}

end

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

function LrmLibraryDelete.restoreTrashAssetsModel( self, trashAssetIds )

	local restoreCountObservable = Rx.ReplaySubject( 1 )
	
	self._wfCatalogStream
		:flatMap( function( wfCatalog )
		
			return self._syncManager:getSyncActivityControl()
				:map( function( shouldSync, hasOzCatalog, isResidualMode, _ )
			
					local restoreOnlyAssetsWithLocalOriginal = false
					
					-- Restore only assets with local original in offline or residual mode
					if ( hasOzCatalog and not shouldSync ) or isResidualMode then
						restoreOnlyAssetsWithLocalOriginal = true
					end
					
					return wfCatalog, restoreOnlyAssetsWithLocalOriginal
			
				end )
			
		end )
		
		:take( 1 )
		
		:flatMap( function( wfCatalog, restoreOnlyAssetsWithLocalOriginal ) 
				
			local pusherControl = getPusherControlFuncs( self.catalog )
			local gatherBinaryFilesToMove
			
			if WFPlatformInfo.isiOS then
				gatherBinaryFilesToMove = getGatherBinaryFilesToMoveFn()
			end
			
			return WFCatTrash.restoreTrashAssets( wfCatalog, trashAssetIds, pusherControl, restoreOnlyAssetsWithLocalOriginal, restoreCountObservable, gatherBinaryFilesToMove )
		
		end )
		
		:call( runUntilDone )
		
	return {
		value = restoreCountObservable
	}
end

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

function LrmLibraryDelete.deleteTrashAssetsModel( self, trashAssetIds )

	local result = self._wfCatalogStream
		:flatMap( function( wfCatalog )
		
			local pusherControl = getPusherControlFuncs( self.catalog )
			return WFCatTrash.deleteTrashAssets( wfCatalog, trashAssetIds, pusherControl )
				
		end )
		:call( runUntilDone )
	
	return {
		value = result
	}
end

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

function LrmLibraryDelete.directlyDeleteAssetsModel( self, assetIds )

	local result = self._wfCatalogStream
		:flatMap( function( wfCatalog )
		
			local pusherControl = getPusherControlFuncs( self.catalog )
			return WFCatTrash.directlyDeleteAssets( wfCatalog, assetIds, pusherControl )
				
		end )
		:call( runUntilDone )
	
	return {
		value = result
	}

end

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

return LrmLibraryDelete
