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

WFCatSearchTest.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.

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

	-- RxLua
local Rx = require 'Rx'

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

	-- WF.core.utils
local KSPathUtils = require 'KSPathUtils'
local WFFileUtils = require 'WFFileUtils'

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


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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog


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

local WFCatSearchTest = {}

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

local kOzUserId = 'abad1dea00000000abad1dea00000000'

local basePath = KSPathUtils.getStandardFilePath( 'carouselDocuments' )
local volumeGuid = WFFileUtils.getVolumeGuidForPath( basePath )

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

local function addSixAssetsWithSixDifferentRatings( rxTest, wfCatalog )

	local docStore = wfCatalog:docStore()
	local docs = {}
	for i = 0, 5 do
		docs[ i + 1 ] = {
					_id = string.format( '9b572d0e423c4654b018893ddd41c23%x', i ),
					type = 'asset',
					subtype = 'image',
					_localOnly = { files = { original = { master = { 
							relativePath = "hopefullythispathisgoodenough",
							volumeGuid = volumeGuid
						} } } },
					captureDate = '2015-07-20T14:03:00',
					ratings = {
						[ kOzUserId ] = {
							rating = i,
						},
					},
					importSource = {
						filePath = '/path/',
						originalHeight = 150,
						originalWidth = 100,
					}
				}
	end

	rxTest:expectSingleValue(
		docStore:bulkSetDocValue( docs ),
		':ignore:' )

end

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

local kFourStarAndBetterSearch = {
	predicates = {
		type = 'rating',
		user = kOzUserId,
		rating = 4,
		operator = '>=',
	}
}

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

function WFCatSearchTest.rxTestMakeCountModel( rxTest )

	local WFCatSearch = require 'WFCatSearch'

	local wfCatalog = createFakeWFCatalog( rxTest )

	addSixAssetsWithSixDifferentRatings( rxTest, wfCatalog )

	wfCatalog = Rx.Observable.just( wfCatalog )
		-- WFCatSearch model APIs accept an observable for the catalog

	local allSearch = WFCatSearch.makeCountModel( wfCatalog, nil, 0 ).value

	rxTest:observe( allSearch )
		:expectData( 6 )

	rxTest:observe( WFCatSearch.makeCountModel( wfCatalog, kFourStarAndBetterSearch, 0 ).value )
		:expectData( 2 )

end

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

function WFCatSearchTest.rxTestGetQueryTermCategories( rxTest )

	local WFCatSearch = require 'WFCatSearch'

	local queryModel = WFCatSearch.makeQueryTermCategoriesModel(  )
	rxTest:expectEqual( 'general', queryModel.value[ 1 ].term )

end

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

function WFCatSearchTest.rxTestQueryTerms( rxTest )

	local WFCatSearch = require 'WFCatSearch'

	local wfCatalog = createFakeWFCatalog( rxTest )
	wfCatalog = Rx.Observable.just( wfCatalog )
		-- WFCatSearch model APIs accept an observable for the catalog

	local queryModel = WFCatSearch.makeQueryTermsModel( wfCatalog, 'locations', "" )
	local lots, some, fewer

	rxTest:observe(
			queryModel.value:map( function( n )
				lots = #n
				return lots > 0
			end
		) )
		:expectData( true )

	queryModel = WFCatSearch.makeQueryTermsModel( wfCatalog, 'locations', {
		version = 1,
		predicates = {
			operator = "union",
			terms = {
				{
					type = 'pick',
					pick = 'flagged',
				}
			}
		}
	} )

	rxTest:observe(
			queryModel.value:map( function( n )
				some = #n
				return lots > some
			end
		) )
		:expectData( true )

	queryModel = WFCatSearch.makeQueryTermsModel( wfCatalog, 'locations', { string.rep( "a", 1000 ) } )

	rxTest:observe(
			queryModel.value:map( function( n )
				fewer = #n
				return some > fewer
			end
		) )
		:expectData( true )

end

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

return WFRxTestCase.wrapSuite( WFCatSearchTest )
