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

ADOBE CONFIDENTIAL
------------------
Copyright 2022 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.

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

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

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

	-- WF.oz_client.managed_catalog
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'

	-- WF.oz_client.managed_catalog.search_sort
local WFCatSearchByAssetType = require "WFCatSearchByAssetType"
local WFCatSearchByCamera = require "WFCatSearchByCamera"
local WFCatSearchByImportedByUser = require "WFCatSearchByImportedByUser"
local WFCatSearchByKeywords = require "WFCatSearchByKeywords"
local WFCatSearchByLocation = require "WFCatSearchByLocation"
local WFCatSearchBySyncStatus = require "WFCatSearchBySyncStatus"

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

local WFCatSearchByQueriesTest = {}

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

local function createAssetDocument( rxTest, wfCatalog, optArgs )

	optArgs = optArgs or {}

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local assetDocValue = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId, optArgs )

	if optArgs.cameraMake then
		KSTableUtils.setNested( assetDocValue, optArgs.cameraMake, 'xmp', 'tiff', 'Make' )
	end
	
	if optArgs.cameraModel then
		KSTableUtils.setNested( assetDocValue, optArgs.cameraModel, 'xmp', 'tiff', 'Model' )
	end
	
	if optArgs.keyword then
		KSTableUtils.setNested( assetDocValue, true, 'xmp', 'dc', 'subject', optArgs.keyword )
	end

	if optArgs.country then
		KSTableUtils.setNested( assetDocValue, optArgs.country, 'xmp', 'photoshop', 'Country' )
	end

	rxTest:waitFor( wfCatalog:docStore():setDocValue( assetDocValue ) )
	
	local assetLocalId = rxTest:getFirstValue( wfCatalog:docStore():docValue( assetId ):pluck( "_localId" ) )
	
	return assetLocalId

end

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

function WFCatSearchByQueriesTest.rxTestSearchByAssetType( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local assetLocalId = createAssetDocument( rxTest, wfCatalog )
	
	local optArgs = {
		docSubtype = 'video'
	}
	
	createAssetDocument( rxTest, wfCatalog, optArgs )
	
	local expectedData = { { "image" } }
	
	local observer = rxTest:observe( 
			WFCatSearchByAssetType.collectAssetTypeDetailsOfRequiredAssets( wfCatalog, { assetLocalId } ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
	expectedData = { { "image" }, { "video" } }
	
	observer = rxTest:observe( 
			WFCatSearchByAssetType.collectAssetTypeDetailsOfAllAssets( wfCatalog ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
end

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

function WFCatSearchByQueriesTest.rxTestSearchByCamera( rxTest )
	
	rxTest:setTimeLimit( 5 )
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local assetLocalId = createAssetDocument( rxTest, wfCatalog )
	
	local optArgs = {
		docSubtype = 'video',
		cameraMake = "Make2",
		cameraModel = "Model2"
	}
	
	createAssetDocument( rxTest, wfCatalog, optArgs )
	
	local expectedData = { { "Make Model" } }
	
	local observer = rxTest:observe( 
			WFCatSearchByCamera.collectCameraDetailsOfRequiredAssets( wfCatalog, { assetLocalId } ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
	expectedData = { { "Make Model" }, { "Make2 Model2" } }
	
	observer = rxTest:observe( 
			WFCatSearchByCamera.collectCameraDetailsOfAllAssets( wfCatalog ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
end

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

function WFCatSearchByQueriesTest.rxTestSearchByImportedByUser( rxTest )
	
	rxTest:setTimeLimit( 5 )
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local assetLocalId = createAssetDocument( rxTest, wfCatalog )
	
	createAssetDocument( rxTest, wfCatalog )
	
	local expectedData = { "kMyself" }
	
	local observer = rxTest:observe( 
			WFCatSearchByImportedByUser.collectImportedByUserOfRequiredAssets( wfCatalog, { assetLocalId } ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
	observer = rxTest:observe( 
			WFCatSearchByImportedByUser.collectImportedByUserOfAllAssets( wfCatalog ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
end

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

function WFCatSearchByQueriesTest.rxTestSearchByKeywords( rxTest )
	
	rxTest:setTimeLimit( 5 )
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local optArgs = {
		keyword = 'Jeff'
	}
	
	local assetLocalId = createAssetDocument( rxTest, wfCatalog, optArgs )
	
	optArgs = {
		keyword = 'Jeff2'
	}
	
	createAssetDocument( rxTest, wfCatalog, optArgs )
	
	local expectedData = { { "jeff" } }
	
	local observer = rxTest:observe( 
			WFCatSearchByKeywords.collectKeywordDetailsOfRequiredAssets( wfCatalog, { assetLocalId } ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
	expectedData = { { "jeff" }, { "jeff2" } }
	
	observer = rxTest:observe( 
			WFCatSearchByKeywords.collectLCKeywordDetailsOfAllAssets( wfCatalog ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
end

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

function WFCatSearchByQueriesTest.rxTestSearchBySyncStatus( rxTest )
	
	rxTest:setTimeLimit( 5 )
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local assetLocalId = createAssetDocument( rxTest, wfCatalog )

	createAssetDocument( rxTest, wfCatalog )
	
	local expectedData = { { "Syncing" }, { "Synced" }, { "OriginalStoredLocally" } }
	
	local observer = rxTest:observe( 
			WFCatSearchBySyncStatus.collectSyncStatusDetailsOfRequiredAssets( wfCatalog, { assetLocalId } ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
	expectedData = { { "Syncing" }, { "Synced" }, { "OriginalStoredLocally" } }
	
	observer = rxTest:observe( 
			WFCatSearchBySyncStatus.collectSyncStatusDetailsOfAllAssets( wfCatalog ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
end

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

function WFCatSearchByQueriesTest.rxTestSearchByLocation( rxTest )
	
	rxTest:setTimeLimit( 5 )
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local optArgs = {
		country = 'Chile'
	}
	
	local assetLocalId = createAssetDocument( rxTest, wfCatalog, optArgs )
	
	optArgs = {
		country = 'Brazil'
	}
	
	createAssetDocument( rxTest, wfCatalog, optArgs )
	
	local expectedData = { { "Chile" } }
	
	local observer = rxTest:observe( 
			WFCatSearchByLocation.collectLocationDetailsOfRequiredAssets( wfCatalog, { assetLocalId } ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
	expectedData = { { "Brazil" }, { "Chile" } }
	
	observer = rxTest:observe( 
			WFCatSearchByLocation.collectLocationDetailsOfAllAssets( wfCatalog ) ) 

	observer:expectDataWithinTime( 2, expectedData )
	
end

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

return WFRxTestCase.wrapSuite( 'WFCatSearchByQueriesTest', WFCatSearchByQueriesTest )
