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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain 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 may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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


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

local LrmLibraryAlbumListModelImp = {}

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

local function albumsDateSorted( indexStore, ascending )

	local sorting = "DESC"
	if ascending then
		sorting = "ASC"
	end
	
	return indexStore:query{
		sql = [[SELECT DISTINCT a.docId as id, a.name as name, a.useOffline AS useOffline, MAX(
			CASE WHEN a.updateTimeStamp IS NULL THEN 0 ELSE a.updateTimeStamp END,
			CASE WHEN a.localTimeStamp IS NULL THEN 0 ELSE a.localTimeStamp END,
			CASE WHEN b.maxUpdateTimeStamp IS NULL THEN 0 ELSE b.maxUpdateTimeStamp END,
			CASE WHEN b.maxUpdateTimeStamp IS NULL THEN 0 ELSE b.maxUpdateTimeStamp END) as timeStamp, a.coverId as coverAssetId FROM
				(SELECT docId, name, updateTimeStamp, localTimeStamp, useOffline, coverId FROM albums WHERE subtype == 'collection') a
				LEFT JOIN
				(SELECT albumId, MAX(updateTimeStamp) AS maxUpdateTimeStamp FROM album_asset GROUP BY albumId) b
					ON a.docId == b.albumId ORDER BY timeStamp ]] .. sorting,
		calm = 0.25
	}
end

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

local function albumsNameSorted( indexStore, ascending )
	
	local sorting = "DESC"
	if ascending then
		sorting = "ASC"
	end
	
	return indexStore:query{
		sql = [[SELECT docId as id, name, useOffline, updateTimeStamp, coverId AS coverAssetId FROM albums 
				WHERE subtype == 'collection' ORDER BY nameLC COLLATE i18n_collate ]] .. sorting,
		calm = 0.25
	}
end

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

local function albumsCountSorted( indexStore, ascending )
	
	local sorting = "DESC"
	if ascending then
		sorting = "ASC"
	end
	
	return indexStore:query{
		sql = [[SELECT a.docId as id, a.name, a.useOffline, a.updateTimeStamp, a.coverId as coverAssetId FROM albums AS a
	            LEFT OUTER JOIN (SELECT albumId, count(albumId) AS albumAssetCount FROM album_asset group by albumId) AS b
						ON a.docId == b.albumId WHERE a.subtype == 'collection' ORDER BY ifnull(albumAssetCount, 0) ]] .. sorting,
		calm = 0.25 }
end

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

local function albumsStatusSorted( indexStore, ascending )

-- Watson #3681078 downloaded albums come first, so reverse the ordering

	local sorting = "DESC"
	if not ascending then
		sorting = "ASC"
	end
	
	return indexStore:query{
		sql = [[SELECT docId AS id, name, useOffline, updateTimeStamp, coverId AS coverAssetId FROM albums WHERE subtype == 'collection' ORDER BY useOffline ]] .. sorting,
		calm = 0.25
	}
end

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

local function albumsSizeSorted( indexStore, ascending )

	local sorting = "DESC"
	if ascending then
		sorting = "ASC"
	end
	
	return indexStore:query{
		sql = [[SELECT docId AS id, name, useOffline, updateTimeStamp, coverId AS coverAssetId FROM albums WHERE subtype == 'collection' 
				LEFT OUTER JOIN ( select albumId, sum(cr.size) AS size FROM
							cacheReferences AS cr INNER JOIN album_asset ON album_asset.assetId = cr.assetId WHERE cr.isTrash == 0
							GROUP BY album_asset.albumId ) sq ON sq.albumId == id ORDER BY ifnull(size,0) ]] .. sorting,
		calm = 0.25
	}
end

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

local function makeAlbumList( collection )

	return collection:map( function( data )
			
		local albums = {}
		for i = 1, #data do
			albums[ i ] = {}
			albums[ i ].id = data[ i ][ 1 ]
			albums[ i ].name = data[ i ][ 2 ]
			albums[ i ].useOffline = data[ i ][ 3 ] == 1
			albums[ i ].updateTimeStamp = data[ i ][ 4 ]
			albums[ i ].coverAssetId = data[ i ][ 5 ]
		end
		return {
			albums = albums
		}
	end )
end

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

function LrmLibraryAlbumListModelImp:albumsModel( sortCriteria, ascending )
	
	local albums = nil
	
	if sortCriteria == "modifiedDate" then
		albums = makeAlbumList( albumsDateSorted( self.catalog:indexStore(), ascending ) )
	elseif sortCriteria == "name" then
		albums = makeAlbumList( albumsNameSorted( self.catalog:indexStore(), ascending ) )
	elseif sortCriteria == "count" then
		albums = makeAlbumList( albumsCountSorted( self.catalog:indexStore(), ascending ) )
	elseif sortCriteria == "status" then
		albums = makeAlbumList( albumsStatusSorted( self.catalog:indexStore(), ascending ) )
	elseif sortCriteria == "size" then
		albums = makeAlbumList( albumsSizeSorted( self.catalog:indexStore(), ascending ) )
	end
	
	return {
		value = albums
	}

end

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

return LrmLibraryAlbumListModelImp
