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

WFRxPlatformIndexStoreTest.lua

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

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

--------------------------------------------------------------------------------
	-- Rx
local Rx = require 'Rx'

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

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

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

	-- WF.sqlite.doc_store
local WFRxDocStore = require 'WFRxDocStore'
local WFRxIndexStore = require 'WFRxIndexStore'


if not WFPlatformInfo.isAndroid then 
	return {}
end

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

local TEST_TIMEOUT = 10

local WFRxPlatformIndexStoreTest = {}

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

local docStore, indexStore

local randomDocId = '7206c3ae9a094d69861ee8983e95085d'
local randomDocId2 = '8b572d0e423c4654b078893ddd41c230'


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

local function createDocStore( rxTest )

	local docStorePath = KSPathUtils.getStandardFilePath( "temp" )
	docStorePath = KSPathUtils.child( docStorePath, rxTest:testName() )
	WFFileUtils.delete( docStorePath )
	
	return WFRxDocStore( KSPathUtils.child( docStorePath, 'WFRxIndexStoreTest.mcat' ) )

end

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

local function createIndexStore( rxTest, docStore, indexSpecScript, optEnablePlatformIndexStore )
	
	return WFRxIndexStore( docStore, indexSpecScript, 0, optEnablePlatformIndexStore )
end

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

function WFRxPlatformIndexStoreTest.tearDown()

	local obs = {}
	if indexStore then
		local oldIndexStore = indexStore
		obs[ #obs + 1 ] = indexStore:haltAndDelete()
			:tapOnCompleted( function()
				oldIndexStore:close()
			end )
		indexStore = nil
	end

	if docStore then
		local oldDocStore = docStore
		obs[ #obs + 1 ] = docStore:haltAndDelete()
			:tapOnCompleted( function()
				oldDocStore:close()
			end )
		docStore = nil
	end

	return Rx.Observable.concat( unpack( obs ) )
		:call( require 'WFRxRunUntilDone' )
	

end

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

function WFRxPlatformIndexStoreTest.rxTestPlatformIndexStoreSimpleQuery( rxTest )
	
	rxTest:setTimeLimit( TEST_TIMEOUT )
	docStore = createDocStore( rxTest )
	
	-- Init with platform based indexstore enabled
	indexStore = createIndexStore( rxTest, docStore, 'RxTestAspectRatio.lua', true )
	
	-- Run a query to make sure init is done
	rxTest:waitFor(
		indexStore:queryOnce( {
			sql = [[SELECT count(*) FROM aspectRatio]],
			} ) )

	rxTest:expectSingleValue( Rx.Observable.just( indexStore:DEBUG_getPlatformIndexStore() )
		:map( function( val )
			WFArguments.check( 'WFRxPlatformIndexStore' )
			return true
		end ), true )
	
	
	local nativeTestName = "com.adobe.wichitafoundation.wfsqlite.luaTest.PlatformIndexStoreSimpleQueryTest"
	local nativeTest = WFLuaNativeTestCase( rxTest:testName(), nativeTestName )
	
	local nativeTestFnName = "openQueryAspectRatio"
	assert( nativeTest:runTest( nativeTestFnName ), string.format( "Native test failed. Test:%s", nativeTestFnName))
	
	rxTest:expectSingleValue(

		docStore:setDocValue {
			_id = randomDocId,
			height = 150,
			width = 100,
		}, ':ignore:' )
	

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

		docStore:setDocValue {
			_id = randomDocId2,
			height = 100,
			width = 100,
		}, ':ignore:' )
	
	nativeTestFnName = "validateQueryUpdateAgain"
	assert( nativeTest:runTest( nativeTestFnName ), string.format( "Native test failed. Test:%s", nativeTestFnName))
	
	nativeTest:close()
end

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

return WFRxTestCase.wrapSuite( WFRxPlatformIndexStoreTest )
