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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
and its suppliers and are protected by all applicable intellectual
property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe.


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

local WFPlatformInfo = require 'WFPlatformInfo'

if not WFPlatformInfo.isAndroid then
	return {}
end

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

	-- WF.core.os
local KSPathUtils = require 'KSPathUtils'

	-- WF.core.self_test
local WFRxTestCase = require "WFRxTestCase"

	-- WF.core
local WFLuaNativeTestCase = require 'WFLuaNativeTestCase'

	-- WF.oz_client.managed_catalog
local WFCatUser = require 'WFCatUser'
local WFCatSpec = require 'WFCatSpec'
local WFCatalog = require 'WFCatalog'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFCatAlbumTestUtils = require 'WFCatAlbumTestUtils'

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


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

if not WFPlatformInfo.isAndroid then
	return {}
end

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

local WFAssetListModelTest = {}

local TEST_TIMEOUT = 10

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

local userId = KSIdGenerator.uuidInLowercaseHex()

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

local function createCatalog( rxTest, catalogId )

	local catalogOptions = {
		indexSpecScript = 'LrmCatalogIndexDefinitions.lua',
		enablePlatformIndexStore = true,
	}

	local basePath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )

	local userInfoCachePath = KSPathUtils.child( basePath, 'user' )

	local wfCatUser = WFCatUser( 'https://photos-stage.adobe.io', userId, userInfoCachePath )

	local wfCatalogSpec = WFCatSpec( wfCatUser, catalogId )

	return WFCatalog( wfCatalogSpec, basePath, catalogOptions )
end

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

-- FIXME [jandrew 2025-03-11]: Disabled due to instability.

function WFAssetListModelTest.DISABLED_rxTestBasicAllPhotosAssetList( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local catalogId = KSIdGenerator.uuidInLowercaseHex()
	local catalog = createCatalog( rxTest, catalogId )
	rxTest:finally( function()
        catalog:close()
   	end )

	rxTest:waitFor( WFCatAssetTestUtils.insertAssets( rxTest, catalog, 10 ) )

	-- Run a query to make sure indexing is done
	rxTest:waitFor(
		catalog:indexStore():queryOnce( {
			sql = [[SELECT count(*) FROM coreInfo]],
			quiesceUntilUpToDate = true
		} ) )

	local nativeTestName = "com.adobe.wichitafoundation.lrmmodels.luaTest.WFAssetListModel_testBasicAllPhotosAssetList"
	local nativeTest = WFLuaNativeTestCase( catalogId, nativeTestName )

	local nativeTestFnName = "testBasicAssetList"
	assert( nativeTest:runTest( nativeTestFnName ), string.format( "Native test failed. Test:%s", nativeTestFnName))

	-- Run a query to make sure indexing is done
	rxTest:waitFor(
		catalog:indexStore():queryOnce( {
			sql = [[SELECT count(*) FROM coreInfo]],
			quiesceUntilUpToDate = true
		} ) )

	rxTest:expectSingleValue( WFCatAssetTestUtils.insertAssets( rxTest, catalog, 20 ), ':ignore:' )

	nativeTestFnName = "testAssetListUpdates"
	assert( nativeTest:runTest( nativeTestFnName ), string.format( "Native test failed. Test:%s", nativeTestFnName))

end

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

function WFAssetListModelTest.rxTestBasicAlbumAssetList( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local catalogId = KSIdGenerator.uuidInLowercaseHex()
	local catalog = createCatalog( rxTest, catalogId )
	rxTest:finally( function()
        catalog:close()
   	end )

	local albumId = "9b572d0e423c4654b078893ddd41c230"

	rxTest:expectSingleValue( catalog:docStore():applyOverlay {
			_id = albumId,
			type = 'album',
			docId = albumId,
			overlay = {
				subtype = 'collection',
				private = false,
				download = false,
				updated = '2012-08-15T15:22:29',
				created = '2012-08-15T15:22:29',
			},
		},
		':ignore:' )

	rxTest:waitFor( WFCatAlbumTestUtils.insertAlbumAssets( rxTest, catalog, albumId, 5 ) )

	-- Run a query to make sure indexing is done
	rxTest:waitFor(
		catalog:indexStore():queryOnce( {
			sql = [[SELECT count(*) FROM coreInfo]],
			quiesceUntilUpToDate = true
		} ) )

	local nativeTestName = "com.adobe.wichitafoundation.lrmmodels.luaTest.WFAssetListModel_testBasicAlbumAssetList"
	local nativeTest = WFLuaNativeTestCase( catalogId, nativeTestName )

	local nativeTestFnName = "testBasicAssetList"
	assert( nativeTest:runTest( nativeTestFnName ), string.format( "Native test failed. Test:%s", nativeTestFnName))

end

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

return WFRxTestCase.wrapSuite( 'WFAssetListModelTest', WFAssetListModelTest )
