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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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 WFPlatformInfo = require 'WFPlatformInfo'

-- FIXME [jandrew 2018-04-02]: Disabling this test suite, as it's
-- unstable on Windows.

if WFPlatformInfo.isWindows then
	return {}
end

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

	-- RxLua
local Rx = require 'Rx'

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

	-- WF.oz_client.catalog
local OzCatalogUrls = require 'OzCatalogUrls'

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

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

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

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

local OzCatalogServiceViaWFCatalogTest = {}

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

local catUser, wfCatalog, wfCatalogStream, catalog, userId

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

function OzCatalogServiceViaWFCatalogTest.setUp()

	local userInfoCachePath = ':memory:'
	userId = KSIdGenerator.uuidInLowercaseHex()
	local catalogId = KSIdGenerator.uuidInLowercaseHex()
	local serverBaseUrl = OzCatalogUrls.baseServerUrl
	local rootCatalogPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), KSIdGenerator.uuidInLowercaseHex() )

	local catalogOptions = {
		indexSpecScript = 'RxTestAspectRatio.lua',
		rxDatabaseScript = 'RxTestNotifications.lua',
		setupConflictResolver = false,
		docStorePath = ':memory:' .. tostring( KSIdGenerator.localCounter() ),
--		docStorePath = KSPathUtils.child( rootCatalogPath, KSIdGenerator.uuidInLowercaseHex() )
	}

	catUser = WFCatUser( serverBaseUrl, userId, userInfoCachePath )

	local catSpec = WFCatSpec( catUser, catalogId )
	wfCatalog = WFCatalog( catSpec, rootCatalogPath, catalogOptions )

	wfCatalogStream = Rx.Observable.just( wfCatalog )

		-- WF.oz_client.docstore
	local OzCatalogServiceViaWFCatalog = require 'OzCatalogServiceViaWFCatalog'

	catalog = OzCatalogServiceViaWFCatalog( wfCatalogStream )

end

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

function OzCatalogServiceViaWFCatalogTest.tearDown()

	if wfCatalog then
		wfCatalog:close()
		wfCatalog = nil
	end

	if catUser then
		catUser:close()
		catUser = nil
	end

	if catalog then
		catalog:close()
		catalog = nil
	end

end

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

function OzCatalogServiceViaWFCatalogTest.rxTestAccessCatUser( rxTest )

	rxTest:expectSingleValue( catalog:getCatUser(), catUser )

end

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

function OzCatalogServiceViaWFCatalogTest.testCallDocStoreAndIndexStoreThroughProxy( test )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	WFAsyncTestCase( test, 30 )

		:waitFor( function()

			return catalog:indexStore():queryB( 'SELECT aspectRatio FROM aspectRatio' )

		end )

		:continueWhenValueMatches( {} )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docId = assetId,
				docType = 'asset',
				overlay = {
					width = 1000,
					height = 500
				}
			}

		end )

		:waitFor( function()

			return catalog:indexStore():queryP( 'SELECT aspectRatio FROM aspectRatio' )

		end )

		:continueWhenValueMatches( 'data', {
			{ 0.5 }
		} )

		:waitFor( function()

			return catalog:docStore():docValueOnceP( 'asset', assetId )
				:mapDataP_new( function( docValue )

					docValue._revId = nil
					return docValue

				end )

		end )

		:continueWhenValueMatches( 'data', {
			_changeCounter = 1,
			_docId = assetId,
			_docType = 'asset',
			_localId = 1,
			height = 500,
			width = 1000,
			type = 'asset',
		} )

end

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

function OzCatalogServiceViaWFCatalogTest.rxTestIndexStoreQueryThroughProxy( rxTest )

	local obs = rxTest:observe(
		catalog:indexStore():query {
			sql = 'SELECT aspectRatio FROM aspectRatio',
			quiesceUntilUpToDate = true,
		} )

	rxTest:expectSingleValue( catalog:docStore():applyOverlay {
		_id = KSIdGenerator.uuidInLowercaseHex(),
		type = 'asset',
		width = 1000,
		height = 500
	}, ':ignore:' )

	obs:expectData( { { 0.5 } } )

end

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

function OzCatalogServiceViaWFCatalogTest.testOzUserViaCatalog( test )

	WFAsyncTestCase( test )

		:waitFor( function()

			return catalog:ozUserIdP()

		end )

		:continueWhenValueMatches( 'data', userId )

		:waitFor( function()

			return catalog:ozUserP()
				:mapDataP_new( function( user )

					return user:userId()

				end )

		end )

		:continueWhenValueMatches( 'data', userId )

end

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

function OzCatalogServiceViaWFCatalogTest.testAccessErrorB( test )

	WFAsyncTestCase( test, 4 )

		:waitFor( function()

			return catalog._accessErrorB:set( 42 )

		end )

		:continueWhenValueMatches( 42 )

end

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

function OzCatalogServiceViaWFCatalogTest.testPhotoInfo( test )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	WFAsyncTestCase( test )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docId = assetId,
				docType = 'asset',
				overlay = {
					width = 1000,
					height = 500
				}
			}

		end )

		:waitFor( function()

			return catalog:photoInfoB( nil )

		end )

		:continueWhenValueMatches( nil )

		:waitFor( catalog:photoInfoB( assetId )
			:closingMapB( function( docValue )

				docValue._revId = nil
				return docValue

			end ) )

		:continueWhenValueMatches( {
			width = 1000,
			height = 500,
			_changeCounter = 1,
			_docId = assetId,
			_docType = 'asset',
			type = 'asset',
			_localId = 1,
		} )

		:waitFor( function()

			return catalog:docStore():applyOverlayToDocumentP {
				docId = assetId,
				docType = 'asset',
				overlay = {
					width = 30,
					height = 100
				}
			}

		end )

		:waitFor( function()

			return catalog:photoInfoP( assetId )
				:mapDataP_new( function( docValue )

					docValue._revId = nil
					return docValue

				end )
		end )

		:continueWhenValueMatches( 'data', {
			width = 30,
			height = 100,
			_changeCounter = 2,
			_docId = assetId,
			_docType = 'asset',
			type = 'asset',
			_localId = 1,
		} )

end

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

function OzCatalogServiceViaWFCatalogTest.rxTestUnimplementedMethods( rxTest )

	rxTest:expectError( function()

		catalog:dataCache()

	end )

	rxTest:expectError( function()

		catalog:getCatalogId()

	end )

	rxTest:expectError( function()

		catalog:setSyncEnabled( true )

	end )

	rxTest:expectError( function()

		catalog:setPurgeEnabled( true )

	end )

end

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

function OzCatalogServiceViaWFCatalogTest.testHaltDocAndIndexStoreAndRxDatabase( test )

	catalog:docStore():halt()
	catalog:indexStore():halt()

end

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

return WFRxTestCase.wrapSuite( 'OzCatalogServiceViaWFCatalogTest', OzCatalogServiceViaWFCatalogTest )
