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

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.self_test
local WFAsyncTestCase = require 'WFAsyncTestCase'

	-- WF.core.rxlua
local WFRxTestCase = require 'WFRxTestCase'

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

	-- Lrmobile_models
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'	
local LrmDocValueTestFactory = require 'LrmDocValueTestFactory'
local LrmLibraryAssetOrganizationModelImp = require 'LrmLibraryAssetOrganizationModelImp'

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

local LrmLibraryAssetOrganizationTest = {}

local catalog

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

function LrmLibraryAssetOrganizationTest.setUp()

	catalog = LrmCatalogTestHelper.makeCatalogService()
	
end

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

function LrmLibraryAssetOrganizationTest.tearDown()

	if catalog then
		
		catalog:close()
		
	end
	
end

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

function LrmLibraryAssetOrganizationTest.testHasAssetsInAlbum( test )

	local docValue = LrmDocValueTestFactory.makeCompleteAsset()
	
	local parentModelMock = {
		catalog = catalog
	}
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local assetId2 = KSIdGenerator.uuidInLowercaseHex()
	local assetId3 = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId2 = KSIdGenerator.uuidInLowercaseHex()
	
	local resultB
	
	WFAsyncTestCase( test )
	
		:waitFor( function()
		
			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId,
				overlay = docValue,
			}
			
		end )
		
		:waitFor( function()
		
			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId2,
				overlay = docValue,
			}
			
		end )
		
		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId3,
				overlay = docValue,
			}

		end )

		:waitFor( function()
		
			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'album',
				docId = albumId,
				overlay = {
					name = 'Test album',
				},
			}
			
		end )
		
		:waitFor( function()
		
			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'album_asset',
				docId = albumAssetId,
				overlay = LrmDocValueTestFactory.makeAlbumAsset( albumId, assetId ),
			}
			
		end )
		
		:continueWhenValueMatches( 'data', ':ignore:' )
		
		:waitFor( function()
		
			resultB = LrmLibraryAssetOrganizationModelImp.hasAssetsInAlbum( 
						parentModelMock, albumId, { assetId } ).valueB
						
			return resultB
						
		end )
		
		:continueWhenValueMatches( {
			state = true,
			assetIds = {
				[ assetId ] = true
			}
		} )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'album_asset',
				docId = albumAssetId2,
				overlay = LrmDocValueTestFactory.makeAlbumAsset( albumId, assetId3 ),
			}

		end )

		:continueWhenValueMatches( 'data', ':ignore:' )

		:waitFor( function()

			resultB = LrmLibraryAssetOrganizationModelImp.hasAssetsInAlbum(
						parentModelMock, albumId, { assetId, assetId2, assetId3 } ).valueB

			return resultB

		end )

		:continueWhenValueMatches( {
			state = true,
			assetIds = {
				[ assetId ] = true,
				[ assetId3 ] = true
			}
		} )

end

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

function LrmLibraryAssetOrganizationTest.testHasAssetsByLocalUrl( test )

	local docValue = LrmDocValueTestFactory.makeCompleteAsset()

	local parentModelMock = {
		catalog = catalog
	}

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local assetId2 = KSIdGenerator.uuidInLowercaseHex()
	local assetId3 = KSIdGenerator.uuidInLowercaseHex()

	local resultB

	WFAsyncTestCase( test )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId,
				overlay = docValue,
			}

		end )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId2,
				overlay = docValue,
			}

		end )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId3,
				overlay = docValue,
			}

		end )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId,
				overlay = {
					_localOnly = {
						localUrl = 'local_url_of_asset1',
					}
				},
			}

		end )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docType = 'asset',
				docId = assetId3,
				overlay = {
					_localOnly = {
						localUrl = 'url',
					}
				},
			}

		end )

		:waitFor( function()

			resultB = LrmLibraryAssetOrganizationModelImp.hasAssetsByLocalUrl(
						parentModelMock, { 'url', 'another_url', 'a_third_url', 'local_url_of_asset1' } ).valueB

			return resultB

		end )

		:continueWhenValueMatches( {
			local_url_of_asset1 = assetId,
			url = assetId3,
		} )


end

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

return WFRxTestCase.wrapSuite(  LrmLibraryAssetOrganizationTest )

