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

ADOBE CONFIDENTIAL
------------------
Copyright 2014 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 KSTableUtils = require "KSTableUtils"

local WFCrypto = require "WFCrypto"

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

local LrmAlbumUtils = {}

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

local derefOpt = assert( KSTableUtils.derefOpt )

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

function LrmAlbumUtils.isAlbumShared( indexStore, albumId )

	return indexStore:query{ 
			sql = [[SELECT
					  sa.docId, sa.spaceId FROM space_album sa
				  INNER JOIN space s ON sa.spaceId == s.docId
				  WHERE
					sa.albumId == ? AND
						((s.private == 0)
					OR
						(s.private == 1 AND s.allowInviteRequests == 1)
					OR
					EXISTS
					  (SELECT spaceId from space_invite where space_invite.spaceId = sa.spaceId
						   AND
							   ((space_invite.subtype = 'request' and space_invite.status == 'open')
						   OR
							   (space_invite.subtype = 'single_use' and space_invite.status == 'open'))
					   UNION
					   SELECT spaceId from space_user where space_user.spaceId = sa.spaceId AND space_user.status = 'accepted'))
				  ]],
			calm = 5,
			albumId,	
		}
		:map( function( rows )
			if rows and rows[ 1 ] and rows[ 1 ][ 1 ] and rows[ 1 ][ 2 ] then
				return true
			else
				return false
			end
		end )
	
end

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

function LrmAlbumUtils.hasAnySharedAlbums( indexStore )

	return indexStore:query{
			sql = [[SELECT
						space_album.docId
					FROM
						space_album
					INNER JOIN
						space
					ON
						space_album.spaceId == space.docId
					WHERE
						space.private == 0
					LIMIT 1]],
					calm = 5,
		}
		:map( function( rows )
			return derefOpt( rows, 1, 1 ) ~= nil
		end )
		
end

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

function LrmAlbumUtils.getDocTypeForAlbumId (albumId)

	if #albumId ~= 32 or albumId == "10002000300040005000600070008000" then
	
		return 'gen-album'
	
	end
	
	return 'album'
	
end

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

function LrmAlbumUtils.calculateAssetListHash( photoList )
	
	if not photoList then
		return nil
	end
	
	local n = #photoList

	if n == 0 then
		return nil
	end
	
	local digest = WFCrypto.MD5.init()
	
	for i = 1, n do
		digest:update( photoList[ i ].id )
	end
	
	local result = digest:digest()
	return result
end

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

-- Check to see whether the lists disagree on a field we actually care about

function LrmAlbumUtils.photoListsCloseEnough( aList, bList )

	if aList == bList then
		return true
	end

	if aList == nil and bList ~= nil then
		return false
	end
	
	if aList ~= nil and bList == nil then
		return false
	end

	local a_n = #aList
	local b_n = #bList
	
	if a_n ~= b_n then
		return false
	end
	
	for i = 1, a_n do
		local a_item = aList[ i ]
		local b_item = bList[ i ]
		if a_item.id ~= b_item.id then
			return false
		end
		if a_item.flag ~= b_item.flag then
			return false
		end
		
		if a_item.rating ~= b_item.rating then
			return false
		end
		
		if a_item.commentCount ~= b_item.commentCount then
			return false
		end
		
		if a_item.favoriteCount ~= b_item.favoriteCount then
			return false
		end
		
		if a_item.developUserUpdated ~= b_item.developUserUpdated then
			return false
		end
		
		if a_item.croppedWidth ~= b_item.croppedWidth then
			return false
		end
		
		if a_item.croppedHeight ~= b_item.croppedHeight then
			return false
		end
	end
	
	return true

end

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

return LrmAlbumUtils
