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

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

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

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

local WFCatSortByCustomOrder = {}

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

local function executeSortQueryWith( wfCatalog, assetIds, whereClause, whereClauseParams, order, shouldLimit )	
	local limit = ""
	if shouldLimit then
		limit = "LIMIT 1"
	end
	local substituteNullWithHighOrder = '{'
	local sqlString = string.format( 
			[[ SELECT a.docId, a.assetId FROM assets a   
				INNER JOIN TEMP_ids t ON t.docId == a.docId 
				INNER JOIN album_asset_v2 s ON a.assetId == s.assetId 
				%s
				ORDER BY IFNULL( s.sortOrder, '%s' ) %s, a.captureDate %s, a.filenameLC COLLATE i18n_collate %s %s ]], 
			whereClause, substituteNullWithHighOrder, order, order, order, limit )
	
	return wfCatalog:indexStore():query( {
			sql = sqlString,
			tempIds = assetIds,
			calm = 1,
			unpack( whereClauseParams )
		})
	
end
	
--------------------------------------------------------------------------------
function WFCatSortByCustomOrder.executeSort( wfCatalog, assetIds, order, sortFieldInfo )
	
	local albumDocId = sortFieldInfo.albumId
	local albumSubtype = sortFieldInfo.subtype
	
--	if searchDescription.predicates.type == 'album' then
--		albumDocId = searchDescription.predicates.album
--		
--	elseif searchDescription.predicates.type == 'and' then
--		-- loop for all the search terms to find the album docId
--		local terms = searchDescription.predicates.terms
--		for i,v in ipairs( terms ) do
--			if v.type == 'album' then
--				albumDocId = v.album 
--			end
--		end
--		
--	elseif searchDescription.predicates.type == 'or' then
--		-- there are multiple albums selected. 
--		-- custom sort order is not applicable for multiple apbum selection
--	else
--		
--		albumDocId = nil
--	end
	
	
	-- At this point, there should be exactly one album in the searchDescription
	-- TODO [anmurark 2017-10-31]: Handle case when there are no/multiple albums selected
	local whereClause = ""
	local whereClauseParams = {}
	
	if albumDocId then
	
		local tableName = 'albums'
		if albumSubtype == 'project' then
			tableName = 'project_albums'
		end
		
		whereClause = string.format("WHERE s.albumId IN ( SELECT albumId from %s where docId = ?)", tableName)
		whereClauseParams[ #whereClauseParams + 1 ] = albumDocId
		
	end
	
	
	
	return executeSortQueryWith( wfCatalog, assetIds, whereClause, whereClauseParams, order, nil )
	
end

--------------------------------------------------------------------------------
function WFCatSortByCustomOrder.getAssetWithHighestOrderInAlbum( wfCatalog, albumUuid, assetIds )

	local whereClause = string.format( "WHERE s.albumId == ? AND s.sortOrder notnull " )
	local whereClauseParams = { albumUuid }
	local order = 'DESC'
	local shouldLimit = true
	return executeSortQueryWith( wfCatalog, assetIds, whereClause, whereClauseParams, order, shouldLimit )
	
end

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

return WFCatSortByCustomOrder
