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

WFCatBenchmarkTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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
local Subject = Rx.Subject

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.utils
local KSPathUtils = require 'KSPathUtils'
local WFRxRunUntilDone = require 'WFRxRunUntilDone'
local WFLogger = require 'WFLogger'
local KSDate = require 'KSDate'
local WFFileUtils = require 'WFFileUtils'
local KSData = require 'KSData'

	-- WF.oz_client.managed_catalog
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFCatIdleDownloader = require 'WFCatIdleDownloader'
local WFRxHttpClient = require 'WFRxHttpClient'
local WFCatUser = require 'WFCatUser'
local WFCatSpec = require 'WFCatSpec'
local WFCatalog = require 'WFCatalog'

	-- WF.oz_client.managed_catalog.pixel_provider
local PixelProviderProcessor = require 'PixelProviderProcessor'
	
local WFRxScheduler = require 'WFRxScheduler'
local WFRxDifferenceFilterWithWindowObservable = require 'WFRxDifferenceFilterWithWindowObservable'

local WFOzTestUtils = require 'WFOzTestUtils'

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

local WFCatBenchmarkTest = {}

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

local child = KSPathUtils.child

local runUntilDone = WFRxRunUntilDone

local testDataPath = child( KSPathUtils.getStandardFilePath( 'testdata' ), 'benchmark' )

local logger = WFLogger 'WFCatBenchmarkTest'
:enable( 'print' )

local filterDifferenceWithWindow = WFRxDifferenceFilterWithWindowObservable

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

local KSIdGenerator = require 'KSIdGenerator'

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

local numberOfAssets = 507
local assetURL = 'http://noir.sea.adobe.com/graphics/noir_large_detailed.png'
local startingTime
local startingDelta

local userId = KSIdGenerator.uuidInLowercaseHex()
local catalogId = KSIdGenerator.uuidInLowercaseHex()

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

local function timeDelta( id )

	startingDelta = startingDelta or startingTime
	local now = KSDate.timeNow()
	local delta = now - startingDelta
	startingDelta = now
	
	--logger:infof( "%s took %f seconds", id, delta )

end

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

local testName

local function totalTime()

	local now = KSDate.timeNow()
	local delta = now - startingTime
	
	logger:infof( "%s TOTAL TIME: %f seconds", testName, delta )

end

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

local function resetTimeDelta()
	startingTime = KSDate.timeNow()
	startingDelta = nil
end

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

local function getPerf( rxTest, writeFile )

	testName = rxTest:testName()

	resetTimeDelta()

	local datas = {}
	
	local runUntilDone = require 'WFRxRunUntilDone'

	WFFileUtils.mkdir( testDataPath )

	rxTest:setTimeLimit( numberOfAssets )

	local o = {}

	local count = 0

	for i=1,numberOfAssets do 

		
		o[ i ] = WFRxHttpClient.get(
				assetURL,
				{
					requestMethod = "GET",
					timeout = 10,
				} )
				:tap( function( data )
					
					count = count + 1
					
					datas[ i ] = data
					
				end )
				:call( runUntilDone )

	end

	for i=1,numberOfAssets do 

			rxTest:expectSingleValue(

				o[ i ], ':ignore:', ':ignore:'

			)

	
	end

	timeDelta( "HTTP Requests" )

	for i=1, numberOfAssets do
		local data = datas[ i ]
		if writeFile then
				local destPath = KSPathUtils.child( testDataPath, KSIdGenerator.uuidInLowercaseHex( ) .. '.png' )
				KSData.saveToUniquelyNamedFile( data, destPath )
		end
		timeDelta( i .. ' of ' .. numberOfAssets )
	end
	
	timeDelta( "File Writes" )
	
	totalTime()
	
	WFFileUtils.delete( testDataPath )

end

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

function WFCatBenchmarkTest.rxTest1_AddHttp_testPerformance( rxTest )

	getPerf( rxTest, false )

end

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

function WFCatBenchmarkTest.rxTest2_AddFileWrite_testPerformance( rxTest )

	getPerf( rxTest, true )

end

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

local counter = 1

local function modifyDoc( docValue )

	local localOnly = {
			files = {
				proxy = {}
			}
		}

	local smp = {}
	
	localOnly.files.proxy = { _delete = true }
	
	local links = {} 
	links[ '/rels/proxy_type/2560' ] = { href = assetURL } 

	smp.links = links
	
	docValue._localOnly = localOnly
	docValue.subtype = 'image'
	docValue._serverProvidedMetadata = smp
	docValue.reviews = {}
	docValue._id = string.format( '%032x', counter )
	
	counter = counter + 1
	
	return docValue

end

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

local function getAddAssetFn( rxTest, docStore )

	return WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, modifyDoc )

end

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

local bulkOverlay = {}

local downloadStream = Subject( 1 )

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

local function createIdleDownloader( docStore, addMoreDocStoreAccess, bulkAnnotation )

	WFFileUtils.mkdir( testDataPath )
	
	local startingObservable = Observable.just( true )
	
	return function( localId, binaryType )

		if addMoreDocStoreAccess then
			startingObservable = docStore:docValue( localId )
		end

		--logger:infof( "Downloading %s for %d", binaryType, localId )

		return startingObservable
		
			:flatMapLatest( function()
			
				return WFRxHttpClient.get(
					assetURL,
					{
						requestMethod = "GET",
						timeout = 10,
					} )
			
			end )
		
			:flatMapLatest( function( data )

				local localOnly = {
					files = {
						original = {},
						proxy = {}
					}
				}

				local destPath = KSPathUtils.child( testDataPath, KSIdGenerator.uuidInLowercaseHex( ) .. '.png' )

				localOnly.files.proxy[ '2560' ] = {
					relativePath = destPath,
					volumeGuid = KSIdGenerator.uuidInLowercaseHex()
				}
		
				KSData.saveToUniquelyNamedFile( data, destPath )
		
				if bulkAnnotation then
				
					bulkOverlay[ #bulkOverlay + 1 ] = {
						_id = localId,
						_localOnly = localOnly,
					}
			
					return startingObservable
						:flatMapLatest( function()
							return docStore:bulkApplyOverlay( bulkOverlay )
								:call( filterDifferenceWithWindow, 25, 10, WFRxScheduler.sharedScheduler() )
								:tap( function()
									bulkOverlay = {}
									--logger:infof( "DONE downloading %s for %d", binaryType, localId )
								end )
						end )
				else
		
					return startingObservable
						:flatMapLatest( function()
							return docStore:applyOverlay {
								_id = localId,
								_localOnly = localOnly,
							}
							:tap( function()
							
								--logger:infof( "DONE downloading %s for %d", binaryType, localId )
							
							end )
						end )
				
				end

			end )
			
			:call( runUntilDone )
			
			:subscribe( function()
			
				downloadStream:onNext( true, localId, true, binaryType )
			
			end )

	end

end

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

local function idleTest( rxTest, addMoreDocStoreAccess, bulkAnnotation )

	testName = rxTest:testName()

	rxTest:setTimeLimit( numberOfAssets )

	local managedCatalogMode = 'managed'

	local uniqueTestName = testName .. KSIdGenerator.uuidInLowercaseHex()
	local tempFolder = KSPathUtils.getStandardFilePath 'temp'
	local tempDocStoreFolder = KSPathUtils.child( tempFolder, uniqueTestName )
	WFFileUtils.mkdir( tempDocStoreFolder )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, tempDocStoreFolder )
	
	local docStore = wfCatalog:docStore()
	
	local downloader = createIdleDownloader( docStore, addMoreDocStoreAccess, bulkAnnotation )
	
	local wfCatIdleDownloader = WFCatIdleDownloader( wfCatalog, downloader, downloadStream, managedCatalogMode )
	
	local addAsset = getAddAssetFn( rxTest, wfCatalog:docStore() )
	
	for i = 1, numberOfAssets do
		addAsset()
	end
	
	local dls = rxTest:observe( downloadStream )
	
	resetTimeDelta()
	
	wfCatIdleDownloader:start()

	-- Wait for everything to download
	
	for i = 1, numberOfAssets do
		dls:expectData( true, ':ignore:', true, ':ignore:' )
		--logger:infof( "%d of %d", i, numberOfAssets )
	end

	totalTime()

	wfCatIdleDownloader:stop()

	wfCatIdleDownloader:close()
	
	rxTest:finally( function()
		WFFileUtils.delete( testDataPath )
		WFFileUtils.delete( tempDocStoreFolder )
	end )
	
end

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

function WFCatBenchmarkTest.rxTest3_AddIdleDownloader_testPerformance( rxTest )

	idleTest( rxTest )
	
end

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

function WFCatBenchmarkTest.rxTest4_AddMoreDocStoreAccess_testPerformance( rxTest )

	idleTest( rxTest, true )

end

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

local function buildDownloadBinaryStream( pixelProviderProcessor, _pixStreamId )

	return pixelProviderProcessor
			:pixStreamIdPixelPathStream( _pixStreamId )
			:tap( 

				function( pixStreamId, localAssetId )

					pixelProviderProcessor
						:runUntilDone( 
							"removeRequest", 
								pixStreamId, localAssetId )

				end )

end

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

local function buildDownloadBinaryFunc( pixelProviderProcessor, pixStreamId )

	return function( localAssetId, binaryType, binarySize, isMarkedForOffline )

		pixelProviderProcessor
			:runUntilDone( 
				"requestBinaryWhenIdle", 
					pixStreamId, localAssetId, binaryType, binarySize,
					isMarkedForOffline, false )
		
	end

end

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

local function pixelProviderTest( rxTest )

	testName = rxTest:testName()

	rxTest:setTimeLimit( 1000 )

	local catalogOptions = {
			indexSpecScript = 'WFCatIndexDefinitions.lua',
	}

	local basePath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )

	local userInfoCachePath = KSPathUtils.child( basePath, 'user' )

	local wfCatUser = WFCatUser( 'https://photos-stage.adobe.io', userId, userInfoCachePath )
	
	local wfCatalogSpec = WFCatSpec( wfCatUser, catalogId )

	local wfCatalog = WFCatalog( wfCatalogSpec, basePath, catalogOptions )

	local pixelProviderProcessor = PixelProviderProcessor( wfCatUser,
															wfCatalogSpec, 
															basePath, 
															catalogOptions )

	wfCatalog:addCloseTarget( pixelProviderProcessor )
					

	local idlePixStreamId = string.format( "%s:%s", 'WFCatSynchronizer', KSIdGenerator.uuidInLowercaseHex() )
	local idleDownloadStream = buildDownloadBinaryStream( pixelProviderProcessor, idlePixStreamId )
	local idleDownloadFunc = buildDownloadBinaryFunc( pixelProviderProcessor, idlePixStreamId )
	local wfCatIdleDownloader = WFCatIdleDownloader( wfCatalog, idleDownloadFunc, idleDownloadStream, 'managed' )

	local addAsset = getAddAssetFn( rxTest, wfCatalog:docStore() )
	
	for i = 1, numberOfAssets do
		addAsset()
	end
	
	local dls = rxTest:observe( idleDownloadStream )
	
	resetTimeDelta()
	
	wfCatIdleDownloader:start()

	-- Wait for everything to download
	
	for i = 1, numberOfAssets do
		dls:expectData( ":ignore:", ':ignore:', 'negative_path', true, ":ignore:", 'proxy' )
		--logger:infof( "%d of %d", i, numberOfAssets )
	end

	totalTime()

	wfCatIdleDownloader:stop()

	wfCatIdleDownloader:close()
	
	rxTest:finally( function()
		WFFileUtils.delete( testDataPath )
	end )
	
end


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

-- SPAM [jandrew 8/26/2016]: In order to run this test, you must first modify 
-- WFCatBinaryDownloader to GET the same URL that's used in this test case.

function WFCatBenchmarkTest.DISABLE_rxTest5_AddPixelProvider_testPerformance( rxTest )

	pixelProviderTest( rxTest )
	
end


--------------------------------------------------------------------------------
return {}
-- return WFRxTestCase.wrapSuite( 'WFCatBenchmarkTest', WFCatBenchmarkTest )
