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

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.rxlua
local WFRxTestCase = require 'WFRxTestCase'

local KSIdGenerator = require 'KSIdGenerator'

	-- Lrmobile_models
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'	
local LrmLibraryStateModelImp = require 'LrmLibraryStateModelImp'

local WFPlatformInfo = require 'WFPlatformInfo'


local function makeLocalOnlyWithMaster()

	local localOnly

	if WFPlatformInfo.isiOS then
		localOnly =  {
			files = {
				master = {
					path = '/a/path'
				},
			},
		}
	else
		localOnly = {
			originalFilePath = '/a/path'
		}
	end

	return localOnly
end

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

local LrmLibraryStateTest = {}

local catalog

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

function LrmLibraryStateTest.setUp()

	catalog = LrmCatalogTestHelper.makeCatalogService()
	
end

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

function LrmLibraryStateTest.tearDown()

	if catalog then
		
		catalog:close()
		
	end
	
end

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

function LrmLibraryStateTest.rxTestCatalogbackedupAssetCount( rxTest )

	rxTest:setTimeLimit( 10 )

	local mockedModel = {
		catalog = catalog,
	}

	local assetId  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
	local assetId2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
	local assetId3 = "cccccccccccccccccccccccccccccccc"
	
	-- create asset
	rxTest:waitFor (
		catalog:docStore():applyOverlay {
			type = 'asset',
			subtype = 'image',
			_id = assetId,
			importSource = {
				originalWidth = 30,
				originalHeight = 20,
			},
			captureDate = '2012-08-15T15:22:29',
			_localOnly = makeLocalOnlyWithMaster(),
		}
	)

	local observeCoreInfo = rxTest
		:observe( catalog:indexStore():query{
			sql = [[SELECT docId, syncStatus FROM coreInfo ]],
			quiesceUntilUpToDate = true,
		})

	observeCoreInfo:expectData { { assetId, 1 } }

	local observeBackedupAssetCount = rxTest:observe( LrmLibraryStateModelImp.catalogBackedupAssetCount( mockedModel ).value )

	observeBackedupAssetCount:expectData( 0 )

		rxTest:waitFor (
		catalog:docStore():applyOverlay {
			_id = assetId,
			_serverProvidedMetadata = {
				base = "https://photos.adobe.io/v2/catalogs/...",
				created = "2017-08-17T11:57:14.343553Z",
				links = {
					["/rels/master"] = {
						content_type = "image/jpeg",
						href = "/v2/catalogs/...",
					},
				},
				revision_ids = {	-- Makes use of special case - see LrmAssetUtils.isLatestMetadataUploaded()
									-- so to not have to guess the new revId.
				},
				updated = "2018-08-29T16:13:13.128931Z",
			},
		}
	)

	observeCoreInfo:expectData { { assetId, 6 } }

	observeBackedupAssetCount = rxTest:observe( LrmLibraryStateModelImp.catalogBackedupAssetCount( mockedModel ).value )

	observeBackedupAssetCount:expectData( 1 )

	local spaceId = KSIdGenerator.uuidInLowercaseHex()

	-- create asset with spaceId
	rxTest:waitFor (
		catalog:docStore():applyOverlay {
			type = 'asset',
			subtype = 'image',
			_id = assetId2,
			_spaceId = spaceId,
			importSource = {
				originalWidth = 30,
				originalHeight = 20,
			},
			captureDate = '2012-08-15T15:22:29',
			_localOnly = makeLocalOnlyWithMaster(),
			_serverProvidedMetadata = {
				base = "https://photos.adobe.io/v2/catalogs/...",
				created = "2017-08-17T11:57:14.343553Z",
				links = {
					["/rels/master"] = {
						content_type = "image/jpeg",
						href = "/v2/catalogs/...",
					},
				},
				revision_ids = {	-- Makes use of special case - see LrmAssetUtils.isLatestMetadataUploaded()
									-- so to not have to guess the new revId.
				},
				updated = "2018-08-29T16:13:13.128931Z",
			},
		}
	)

	observeCoreInfo = rxTest
		:observe( catalog:indexStore():query{
			sql = [[SELECT docId, syncStatus FROM coreInfo ORDER BY docId ASC ]],
			quiesceUntilUpToDate = true,
		})

	observeCoreInfo:expectData { { assetId, 6 }, { assetId2, 6 } }

	observeBackedupAssetCount = rxTest:observe( LrmLibraryStateModelImp.catalogBackedupAssetCount( mockedModel ).value )

	-- should still be 1, as assets with spaceId shouldn't count.
	observeBackedupAssetCount:expectData( 1 )
	
	local localOnly
	
	if WFPlatformInfo.isiOS then
	
		localOnly = {
			files = {
				preview = {
					path = 'blobs/...',
				}
			}
		}
		
	else
		
		localOnly = {
			preview = {
				path = 'blobs/...',
			}
		}
		
	end
	
	-- create asset with no local master or proxy
	rxTest:waitFor (
		catalog:docStore():applyOverlay {
			type = 'asset',
			subtype = 'image',
			_id = assetId3,
			importSource = {
				originalWidth = 30,
				originalHeight = 20,
			},
			captureDate = '2012-08-15T15:22:29',
			_localOnly = localOnly,
			_serverProvidedMetadata = {
				base = "https://photos.adobe.io/v2/catalogs/...",
				created = "2017-08-17T11:57:14.343553Z",
				links = {
					["/rels/master"] = {
						content_type = "image/jpeg",
						href = "/v2/catalogs/...",
					},
				},
				revision_ids = {	-- Makes use of special case - see LrmAssetUtils.isLatestMetadataUploaded()
									-- so to not have to guess the new revId.
				},
				updated = "2018-08-29T16:13:13.128931Z",
			},
		}
	)

	observeCoreInfo = rxTest
		:observe( catalog:indexStore():query{
			sql = [[SELECT docId, syncStatus FROM coreInfo ORDER BY docId ASC ]],
			quiesceUntilUpToDate = true,
		})

	observeCoreInfo:expectData { { assetId, 6 }, { assetId2, 6 },{ assetId3, 12 } }

	observeBackedupAssetCount = rxTest:observe( LrmLibraryStateModelImp.catalogBackedupAssetCount( mockedModel ).value )

	-- should still be 2
	observeBackedupAssetCount:expectData( 2 )

end

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

return WFRxTestCase.wrapSuite(  LrmLibraryStateTest )

