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

WFCatSearchByAlbumTest.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.self_test
local WFRxTestCase = require 'WFRxTestCase'

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

	-- WF.oz_client.managed_catalog.search
local WFCatSearchQuery = require 'WFCatSearchQuery'
local WFCatSearchSchema = require 'WFCatSearchSchema'

	-- WF.sqlite.doc_store
local WFRxDocStore = require 'WFRxDocStore'

	-- WF.sqlite.index_store
local WFRxIndexStore = require 'WFRxIndexStore'

local WFOzTestUtils = require 'WFOzTestUtils'

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

local WFCatSearchByAlbumTest = {}

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

local sCoveragePath

function WFCatSearchByAlbumTest.setUp( inCoveragePath )
	sCoveragePath = inCoveragePath
end

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

local function addCaptureDateFn( )
	local counter = 1

	return function( docValue, date )
		
		docValue.importSource.originalHeight = counter + 1
		docValue.captureDate = date

		counter = counter + 1
		
		return docValue
	end

end

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

local function makeSearcher( rxTest, docStore, indexStore, searchSchema )
	return function( predicates )
		local terms = WFCatSearchQuery.createQueryParamsStream( searchSchema, {
			predicates = predicates
		} )

		local query2 = WFCatSearchQuery.resultSetQuery( indexStore,
			[[SELECT docId FROM assets @JOIN ORDER BY height DESC]],
			terms,
			'assets.docId'
		)

		return query2
	end

end

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

function WFCatSearchByAlbumTest.rxTestSavedAlbumSearch( rxTest )

	rxTest:setTimeLimit( 20 )

	local docStore = WFRxDocStore( ':memory:' )
	local indexStore = WFRxIndexStore( docStore, 'WFCatIndexDefinitions.lua' )

	rxTest:finally( function()

		if sCoveragePath and indexStore and not WFCloseSupport.hasBeenOrWillBeClosed( indexStore ) then
			indexStore:DEBUG_dumpCoverage( sCoveragePath )
		end

		indexStore:close()
		indexStore = nil

		docStore:close()
		docStore = nil

	end )

	local searchSchema = WFCatSearchSchema()

	require 'WFCatSearchByAlbum'.addSearchToSearchSchema( searchSchema )

	local addAlbumFn = WFOzTestUtils.v2.makeAdderOfAlbum( rxTest, docStore )
	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, addCaptureDateFn() )
	local addAssetToAlbumFn = WFOzTestUtils.v2.makeAdderOfAssetToAlbum( rxTest, docStore )

	local albumId = addAlbumFn()

	local albumLocalId

	rxTest
		:expectSingleValue( 
			docStore
				:docValue( albumId )
				:tap( 
					function( albumDocValue )

						albumLocalId = albumDocValue._localId

					end ), 
			':ignore:' )


	local assetId = addAssetFn( '1973-03-08T01:02:03.04' )
	addAssetToAlbumFn( albumId, assetId )

	assetId = addAssetFn( '1995-05-27T14:00:00.00' )
	addAssetToAlbumFn( albumId, assetId )

	assetId = addAssetFn( '2006-01-20T16:00:00.00' )
	addAssetToAlbumFn( albumId, assetId )

	local runQueryOfTermsFromSavedSearch = makeSearcher( rxTest, docStore, indexStore, searchSchema )

	rxTest:observe( runQueryOfTermsFromSavedSearch {
		type = 'album',
		dataType = 'albumAssets',
		album = albumLocalId,
		operator = '==',
	} )
	:expectDataWithinTime( 5, { { 6 }, { 4 }, { 2 } } )

	-- make sure we don't return results for project query

	rxTest:observe( runQueryOfTermsFromSavedSearch {
		type = 'project_album',
		dataType = 'projectAlbumAssets',
		project_album = albumLocalId,
	} )
	:expectDataWithinTime( 5, {} )
end

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

function WFCatSearchByAlbumTest.rxTestSavedProjectAlbumSearch( rxTest )

	rxTest:setTimeLimit( 20 )

	local docStore = WFRxDocStore( ':memory:' )
	local indexStore = WFRxIndexStore( docStore, 'WFCatIndexDefinitions.lua' )

	rxTest:finally( function()

		if sCoveragePath and indexStore and not WFCloseSupport.hasBeenOrWillBeClosed( indexStore ) then
			indexStore:DEBUG_dumpCoverage( sCoveragePath )
		end

		indexStore:close()
		indexStore = nil

		docStore:close()
		docStore = nil

	end )

	local searchSchema = WFCatSearchSchema()

	require 'WFCatSearchByAlbum'.addSearchToSearchSchema( searchSchema )

	local addAlbumFn = WFOzTestUtils.v2.makeAdderOfProjectAlbum( rxTest, docStore )
	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, addCaptureDateFn() )
	local addAssetToAlbumFn = WFOzTestUtils.v2.makeAdderOfAssetToAlbum( rxTest, docStore )

	local albumId = addAlbumFn()

	local albumLocalId

	rxTest
		:expectSingleValue( 
			docStore
				:docValue( albumId )
				:tap( 
					function( albumDocValue )

						albumLocalId = albumDocValue._localId

					end ), 
			':ignore:' )


	local assetId = addAssetFn( '1973-03-08T01:02:03.04' )
	addAssetToAlbumFn( albumId, assetId )

	assetId = addAssetFn( '1995-05-27T14:00:00.00' )
	addAssetToAlbumFn( albumId, assetId )

	assetId = addAssetFn( '2006-01-20T16:00:00.00' )
	addAssetToAlbumFn( albumId, assetId )

	local runQueryOfTermsFromSavedSearch = makeSearcher( rxTest, docStore, indexStore, searchSchema )

	rxTest:observe( runQueryOfTermsFromSavedSearch {
		type = 'project_album',
		dataType = 'projectAlbumAssets',
		project_album = albumLocalId,
	} )
	:expectDataWithinTime( 5, { { 6 }, { 4 }, { 2 } } )

	-- make sure we don't return result for normal album query
	rxTest:observe( runQueryOfTermsFromSavedSearch {
		type = 'album',
		dataType = 'albumAssets',
		album = albumLocalId,
		operator = '==',
	} )
	:expectDataWithinTime( 5, {} )
end

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

return WFRxTestCase.wrapSuite( 'WFCatSearchByAlbumTest', WFCatSearchByAlbumTest )
