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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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.

LrmLibraryCacheModelImp.lua

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

local WFArguments = require 'WFArguments'

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

local Model = {}

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

local check = assert( WFArguments.check )

local runUntilDone = require 'WFRxRunUntilDone'

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

function Model:purgeableCacheSizeForAlbum( albumId )

	check( 'table', 'string' )
	
	local sql = [[ 
		SELECT 
			SUM(size)
		FROM 
			cacheReferences 
		WHERE 
			cacheReferences.hasUploaded == 1 AND
			cacheReferences.renditionName != 'thumbnail2x' AND 
			cacheReferences.assetId 
		IN ( 
			SELECT 
				album_asset.assetId 
			FROM 
				album_asset
			INNER JOIN 
				albums 
			ON 
				album_asset.albumId == albums.docId
			WHERE 
				album_asset.albumId == ?
			
			EXCEPT 
			
			SELECT 
				album_asset.assetId
			FROM 
				album_asset 
			INNER JOIN 
				albums 
			ON 
				album_asset.albumId == albums.docId
			WHERE 
				albums.useOffline == 1 AND
				album_asset.albumId != ?
		)  ]]
	
	return {
		value =	self.catalog:indexStore():queryP( sql, albumId, albumId )
			:mapDataP_new( function( size )
			
				local WFPlatformInfo = require 'WFPlatformInfo'
				if WFPlatformInfo.isAndroid then
                    return {
						size = size and size[ 1 ] and size[ 1 ][ 1 ] or 0,
						albumId = albumId,		-- android expects albumid as it does not maintain state
					}
				end
				return size and size[ 1 ] and size[ 1 ][ 1 ] or 0
				
			end )
	}
	
end

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

function Model:purgeableCacheSize()

	check( 'table' )
	
	local sql = [[ 
		SELECT 
			SUM(size)
		FROM 
			cacheReferences 
		WHERE 
			cacheReferences.renditionName != 'thumbnail2x' AND 
			cacheReferences.assetId 
		IN ( 
			SELECT 
				album_asset.assetId
			FROM 
				album_asset 
			INNER JOIN 
				albums 
			ON 
				album_asset.albumId == albums.docId
			WHERE 
				albums.useOffline == 0 
		) ]]
	
	return {
		value =	self.catalog:indexStore():queryP( sql )
			:mapDataP_new( function( size )
			
				return size and size[ 1 ] and size[ 1 ][ 1 ] or 0
				
			end )
	}
	
end

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

function Model:purgeImmediately( noPurgeLimit )

	check( "table", "?boolean" )
	
	return {
		value = self.catalog:getSyncClient():purgeImmediately( nil, noPurgeLimit ):call( runUntilDone )
	}

end

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

function Model:purgeForAlbum( albumId, noPurgeLimit )

	check( "table", "string", "?boolean" )
	
	return {
		value = self.catalog:getSyncClient():purgeForAlbum( albumId, nil, noPurgeLimit ):call( runUntilDone )
	}

end

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

return Model
