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

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.

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

	-- WF.lrmobile
local LrmConfig = require 'LrmConfig'

	-- WF.core.utils
local WFArguments = require 'WFArguments'

	-- WF.oz_client.lrmobile
local LrmCatSyncStatus = require 'LrmCatSyncStatus'

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

local check = assert( WFArguments.check )

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

-- @mark LrmLibraryAlbumStatusModelImp namespace

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

local LrmLibraryAlbumStatusModelImp = {}

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

LrmConfig.registerBinaryStrategyCheck()

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

function LrmLibraryAlbumStatusModelImp:unsynchronizedAssetsForAlbum( albumId, binaryDownloadStrategy )

	check( 'table', 'string', 'binaryStrategy' )
	
	local masterRenditionNames, proxyRenditionNames = LrmCatSyncStatus.getDownloadRenditionNames()

	local masterSql = [[
		SELECT COUNT( distinct r.assetId ) FROM renditionRevisions as r 
		INNER JOIN album_asset ON album_asset.albumId == ? AND album_asset.assetId = r.assetId
		INNER JOIN albums ON albums.docId = album_asset.albumId
		LEFT JOIN space s ON s.docId = albums.spaceId
		LEFT JOIN space_user su ON s.spaceUserId = su.fullDocId
		WHERE
			( albums.spaceId IS NULL OR su.role == 'editor' ) AND renditionName IN CARRAY(?,?,'char*')
			OR ( albums.spaceId IS NOT NULL AND ( su.role IS NULL OR su.role != 'editor' ) AND renditionName IN CARRAY(?,?,'char*') )
	]]

	local proxySql = [[
		SELECT COUNT( distinct r.assetId ) FROM renditionRevisions as r 
		INNER JOIN album_asset ON album_asset.albumId == ? AND album_asset.assetId = r.assetId 
		WHERE 
			renditionName IN CARRAY(?,?,'char*')
	]]

	local queryArgs
	
	if binaryDownloadStrategy == LrmConfig.StrategyPreferMaster then
		queryArgs = {
			sql = masterSql,
			calm = 1,
			singleColumnQuery = true,
			albumId,
			masterRenditionNames,
			#masterRenditionNames,
			proxyRenditionNames,
			#proxyRenditionNames
		}
	else
		queryArgs = {
			sql = proxySql,
			calm = 1,
			singleColumnQuery = true,
			albumId,
			proxyRenditionNames,
			#proxyRenditionNames
		}
	end
	
	local query = self.catalog:indexStore():query( queryArgs )
		:map( function( rows )
			if rows and #rows > 0 then
				return rows[1]
			end
			
			return nil
		end )

	return {
		value = query,
	}
	
end

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

function LrmLibraryAlbumStatusModelImp:missingCacheSizeForAlbum( albumId, binaryDownloadStrategy )

	check( 'table', 'string', 'binaryStrategy' )
	
	-- FIXME [jandrew 2022-05-25]: On LrA, we never download video originals/proxies when
	-- an album is marked for offline use. I _think_ this query should be updated
	-- to ignore video binaries, but I'm not certain. This needs to be looked at
	-- in the future.
	
	local masterSql = [[
		SELECT SUM(rr.size)
		FROM renditionRevisions as rr
			INNER JOIN album_asset ON album_asset.albumId == ? AND album_asset.assetId = rr.assetId
			INNER JOIN albums ON albums.docId = album_asset.albumId
			LEFT JOIN space s ON s.docId = albums.spaceId
			LEFT JOIN space_user su ON s.spaceUserId = su.fullDocId
		WHERE
			( albums.spaceId IS NULL OR su.role == 'editor' ) AND renditionName IN ( 'thumbnail2x', 'preview', 'proxyWithoutMaster', 'master', 'xmp_develop', 'aux-original' )
			OR ( albums.spaceId IS NOT NULL AND ( su.role IS NULL OR su.role != 'editor' ) AND renditionName IN ( 'thumbnail2x', 'preview', 'proxy', 'proxyPreGenerated', 'xmp_develop', 'aux-proxy-2560' ) )
	]]

	local proxySql = [[
		SELECT SUM(rr.size)
		FROM renditionRevisions as rr
			INNER JOIN album_asset ON album_asset.albumId == ? AND album_asset.assetId = rr.assetId
		WHERE
			renditionName IN ( 'thumbnail2x', 'preview', 'proxy', 'proxyPreGenerated', 'xmp_develop', 'aux-proxy-2560' )
	]]
	
	local sql
	
	if binaryDownloadStrategy == LrmConfig.StrategyPreferMaster then
		sql = masterSql
	else
		sql = proxySql
	end
	
	local query = self.catalog:indexStore():query( { 
			sql = sql, 
			calm = 1,
			singleColumnQuery = true,
			albumId
		} )
		:map( function( row )

			return row and row[ 1 ] or 0

		end )

	return {
		value = query
	}
	
end

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

return LrmLibraryAlbumStatusModelImp
