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

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'

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

local check = assert( WFArguments.check )

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

-- @mark LrmLibraryAlbumStatusModelImp namespace

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

local LrmLibraryAlbumStatusModelImp = {}

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

LrmConfig.registerBinaryStrategyCheck()

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

function LrmLibraryAlbumStatusModelImp:unsynchronizedAssetsForAlbum( albumId, binaryDownloadStrategy )

	check( 'table', 'string', 'binaryStrategy' )
	
	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 ( 'thumbnail2x', 'preview', 'proxyWithoutMaster', 'master', 'xmp_develop' )
			OR ( albums.spaceId IS NOT NULL AND ( su.role IS NULL OR su.role != 'editor' ) AND renditionName IN ( 'thumbnail2x', 'preview', 'proxy', 'xmp_develop' ) )
	]]

	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 ( 'thumbnail2x', 'preview', 'proxy', 'xmp_develop' )
	]]
	
	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( 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' )
	
	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' )
			OR ( albums.spaceId IS NOT NULL AND ( su.role IS NULL OR su.role != 'editor' ) AND renditionName IN ( 'thumbnail2x', 'preview', 'proxy', 'xmp_develop' ) )
	]]

	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', 'xmp_develop' )		
	]]
	
	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
