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

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.

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

	-- WF.core.reactive
local KSReactive = require 'KSReactive'

	-- WF.core.utils
local WFUnknown = require 'WFUnknown'
local WFCloseSupport = require 'WFCloseSupport'

	-- WF.sqlite.database
local WFSQLiteUtils = require 'WFSQLiteUtils'

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

local LrmLibraryAssetOrganizationModelImp = {}

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

function LrmLibraryAssetOrganizationModelImp:hasAssetsInAlbum( albumId, assetIds )

	local resultB = KSReactive.variableB( WFUnknown.value() )
	
	local catalog = self.catalog
	local function checkForExistingAssetsP()
		
		local assetsPresent = {}
		local sqlQuery = string.format(
			[[ SELECT assetId FROM album_asset WHERE albumId == ? AND assetId IN ( %s ) ]],
			WFSQLiteUtils.createVariableArgList( #assetIds ) )
			
		return catalog:indexStore():queryP( sqlQuery, albumId, unpack( assetIds ) )
		:mapDataP( function( _, idRows )
			for i = 1, #idRows do
				assetsPresent[ idRows[ i ][ 1 ] ] = true
			end
			return 'data', assetsPresent
		end )
	end
	
	checkForExistingAssetsP()
	:whenResolvedDo( function( status, data )
	
		if not WFCloseSupport.hasBeenOrWillBeClosed( resultB ) then
		
			if status == 'data' then
				resultB:set( { state = true, assetIds = data } )
			else
				resultB:set( { state = false, error = data } )
			end
			
		end
		
	end )
	:runUntilDone()
	
	return {
		valueB = resultB,
	}
end

--------------------------------------------------------------------------------
function LrmLibraryAssetOrganizationModelImp:hasAssetBySha256( --[[string]] sha256 )

	local resultB = KSReactive.variableB( WFUnknown.value() )

	self.catalog:hasPhotoBySha256P( sha256 )
	:whenResolvedDo( function( status, data )
		if status == 'data' then
			if type( data ) == 'boolean' then
				local res = {
					state = false,
					error = 'No asset found',
				}
				resultB:set( res )
			else
				resultB:set( { state = true, assetId = data } )
			end
		else
			resultB:set( { state = false, error = data } )
		end
	end )
	:runUntilDone()

	return {
		valueB = resultB,
	}
end

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

function LrmLibraryAssetOrganizationModelImp:hasAssetByLocalUrl( localUrls, userId )

	local resultB = KSReactive.variableB( WFUnknown.value() )

	localUrls = localUrls or {}
	
	local queryStr = string.format(
		[[SELECT docId FROM coreInfo WHERE importedBy = ? AND localUrl IN ( %s ) LIMIT 1 ]],
		WFSQLiteUtils.createVariableArgList( #localUrls ) )

	self.catalog:indexStore()
	
		:queryP( queryStr, userId, unpack( localUrls ) )
		
		:mapDataP( function( _, idRows )
		
			return 'data',
				   ( idRows and idRows[ 1 ] and idRows[ 1 ][ 1 ] ) or false
		
		end )
		
		:whenResolvedDo( function( status, data )
		
			if status == 'data' then
			
				if type( data ) == 'string' then
				
					local res = {
						state = true,
						assetIds = { assetId = data }
					}
					
					resultB:set( res )
					
				else
				
					resultB:set( { state = false, error = 'No assets found' } )
					
				end
				
			else
			
				resultB:set( { state = false, error = data } )
				
			end
			
		end )
		
		:runUntilDone()
	
	return {
		valueB = resultB,
	}
	
end

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

function LrmLibraryAssetOrganizationModelImp:hasAssetsByLocalUrl( localUrls )

	-- WF-861: on mass import this method might be called with 1000s of localUrls at once.

	localUrls = localUrls or {}

	local queryStr = [[SELECT coreInfo.docId, localUrl FROM coreInfo WHERE coreInfo.localUrl IN CARRAY(?,?,'char*')]]

	local resultB = self.catalog:indexStore()

		:queryP( {
					sql = queryStr,
					localUrls,
					#localUrls
				} )

		:mapDataP_new( function( idRows )
		
			local assetIds = {}
			
			for i, v in ipairs( idRows ) do
			
				assetIds[ v[ 2 ] ] = v[ 1 ]
				
			end
			
			return assetIds
		
		end )
		
		:mapP( function( status, assetIds )
		
			if status == 'data' then
			
				return status, assetIds
				
			else
			
				return 'error', assetIds
				
			end
			
		end )
		
		:untilResolvedB( 'data', WFUnknown.value() )
	
	return {
		valueB = resultB,
	}
	
end


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

return LrmLibraryAssetOrganizationModelImp
