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

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 KSIdGenerator = require 'KSIdGenerator'
local WFFileUtils = require 'WFFileUtils'

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


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

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 function addAlbum( rxTest, wfCatalog )

	local docId = KSIdGenerator.uuidInLowercaseHex()
	local docStore = wfCatalog:docStore()
	
	local doc = {
		_id = docId,
		type = 'album',
		subtype = 'collection',
	}
	rxTest:waitFor( docStore:setDocValue( doc ) )
	return { docId } 
end

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

local function addSpaceAsset( rxTest, wfCatalog )
	
	local docId = KSIdGenerator.uuidInLowercaseHex()
	local docStore = wfCatalog:docStore()
	
	local doc = {
		_id = docId,
		_spaceId = KSIdGenerator.uuidInLowercaseHex(),
		type = 'asset',
		subtype = 'image',
		_localOnly = { 
			files = { 
				original = { 
					master = { 
						relativePath = "hopefullythispathisgoodenough",
						volumeGuid = volumeGuid
					} 
				} 
			} 
		},
	}
	rxTest:waitFor( docStore:setDocValue( doc ) )
	return { docId }
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.rxTestMakeExistsModel( rxTest )

	rxTest:setTimeLimit( 10 )

	local WFCatSearch = require 'WFCatSearch'

	local wfCatalog = createFakeWFCatalog( rxTest )

	-- nothing exists in the catalog
	rxTest:observe( WFCatSearch.makeExistsModel( Rx.Observable.just( wfCatalog ), nil ).value )
		:expectData( false )
	
	-- only empty album exists
	local docIds = addAlbum( rxTest, wfCatalog )
	rxTest:observe( WFCatSearch.makeExistsModel( Rx.Observable.just( wfCatalog ), nil ).value )
		:expectData( false )
	rxTest:waitFor( WFCatDelete.deleteDocs( wfCatalog:docStore(), docIds ) )
	
	-- empty album and spaceID exists
	docIds = addSpaceAsset( rxTest, wfCatalog )
	rxTest:observe( WFCatSearch.makeExistsModel( Rx.Observable.just( wfCatalog ), nil ).value )
		:expectData( false )
	rxTest:waitFor( WFCatDelete.deleteDocs( wfCatalog:docStore(), docIds ) )
	
	-- stuff exists in the catalog
	addSixAssetsWithSixDifferentRatings( rxTest, wfCatalog )	
	rxTest:observe(  WFCatSearch.makeExistsModel( Rx.Observable.just( wfCatalog ), nil ).value )
		:expectData( true )
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', WFCatSearchTest )
