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

NmbOfflineAssetsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

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

	-- RxLua
local Rx = require 'Rx'
local Observable = Rx.Observable

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

	-- WF.oz_client.managed_catalog
local NmbOfflineAssets = require 'NmbOfflineAssets'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFCatAlbums = require 'WFCatAlbums'

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

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

local child = KSPathUtils.child

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog


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

local NmbOfflineAssetsTest = {}

local kAsset1 = KSIdGenerator.uuidInLowercaseHex()

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

function NmbOfflineAssetsTest.rxTestBasicAssets( rxTest )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )

	local docStore = fakeWFCatalog:docStore()

	rxTest:expectSingleValue(

		docStore:setDocValue {
				_id = kAsset1,
				type = 'asset',
				subtype = 'jpeg',
				captureDate = '2001',
				importSource = {
					filePath = '/path/',
					originalHeight = 150,
					originalWidth = 100,
				}
			},

		':ignore:' )

	rxTest:expectOnlyCompleted(
		NmbOfflineAssets.enableAssetsOfflineUse( fakeWFCatalog, { kAsset1 } )
	)

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( kAsset1 )
			:pluck( '_localOnly', 'isMarkedForOfflineUse' ),
		true
	)

	rxTest:expectOnlyCompleted(
		NmbOfflineAssets.disableAssetsOfflineUse( fakeWFCatalog, { kAsset1 } )
	)

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( kAsset1 )
			:pluck( '_localOnly', 'isMarkedForOfflineUse' ),
		nil
	)

end

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

function NmbOfflineAssetsTest.rxTestBasicAlbums( rxTest )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )
	local albumId
	
	rxTest:expectOnlyCompleted(
		WFCatAlbums.createAlbum( fakeWFCatalog, 'My Album' )
			:map( function( _, globalAlbumId )
				albumId = globalAlbumId
				return albumId
			end )
			:takeLast( 0 )
	)

	rxTest:expectOnlyCompleted(
		NmbOfflineAssets.enableLocalAlbumStorage( fakeWFCatalog, { albumId } )
	)

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( albumId )
			:pluck( '_localOnly', 'isMarkedForOfflineUse' ),
		true
	)	
	
	rxTest:expectOnlyCompleted(
		NmbOfflineAssets.disableLocalAlbumStorage( fakeWFCatalog, { albumId } )
	)

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( albumId )
			:pluck( '_localOnly', 'isMarkedForOfflineUse' ),
		nil
	)

	local model = NmbOfflineAssets.offlineAlbumsModel(
		Observable.just( nil )
			:concat( Observable.just( fakeWFCatalog ) ),
		{ albumId } )

	rxTest:expectOnlyCompleted(
		model.value
	)

end

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

local testDataPath = child( KSPathUtils.getStandardFilePath( 'testdata' ), 'wffileutils' )
local originalPath = child( testDataPath, 'original.txt' )
local originalRelativePath = WFFileUtils.removeVolumeFromPath( originalPath )
local originalVolumeGuid = WFFileUtils.getVolumeGuidForPath( originalPath )

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

-- This test verifies that if an asset is marked for offline use, but a path
-- annotation already exists that points to nothing, we clear that path annotation
-- so that we can redownload the asset.

function NmbOfflineAssetsTest.rxTestExistingAnnotation( rxTest )

	rxTest:setTimeLimit( 5 )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )

	local docStore = fakeWFCatalog:docStore()

	rxTest:expectSingleValue(

		docStore:setDocValue {
				_id = kAsset1,
				type = 'asset',
				subtype = 'jpeg',
				captureDate = '2001',
				importSource = {
					filePath = '/path/',
					originalHeight = 150,
					originalWidth = 100,
				},
				_localOnly = {
					files = {
						original = {
							master = {
								relativePath = originalRelativePath,
								volumeGuid = originalVolumeGuid
							}
						}
					}
				}
			},

		':ignore:' )

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( kAsset1 )
			:pluck( '_localOnly', 'files', 'original', 'master' , 'relativePath' ),
		originalRelativePath
	)

	rxTest:expectOnlyCompleted(
		NmbOfflineAssets.enableAssetsOfflineUse( fakeWFCatalog, { kAsset1 } )
	)

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( kAsset1 )
			:pluck( '_localOnly', 'isMarkedForOfflineUse' ),
		true
	)

	rxTest:expectSingleValue(
		fakeWFCatalog:docStore():docValue( kAsset1 )
			:pluck( '_localOnly', 'files', 'original', 'master' , 'relativePath' ),
		nil
	)

end

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

return WFRxTestCase.wrapSuite( 'NmbOfflineAssetsTest', NmbOfflineAssetsTest )
