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

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.self_test
local WFRxTestCase = require "WFRxTestCase"

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

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local WFTableMerge = require 'WFTableMerge'
local KSDate = require 'KSDate'
local KSTableUtils = require "KSTableUtils"

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

	-- Lrmobile_models
local LrmAllPhotosAlbumModel = require 'LrmAllPhotosAlbumModel'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFCatAlbumTestUtils = require 'WFCatAlbumTestUtils'

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

local LrmAllPhotosAlbumModelTest = {}

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

local derefOpt = KSTableUtils.derefOpt

local kAlbumId = KSIdGenerator.uuidInLowercaseHex()
local TEST_TIMEOUT = 10

local catalogOptions = {
	indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
}

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

local function getSubmodel( model, submodelName )

	return model.models[submodelName]

end

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

local function makeSubmodel( model, submodelName, ... )

	return getSubmodel( model, submodelName )( model, ... )

end

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

function LrmAllPhotosAlbumModelTest.rxTestCannotInitModelWithoutWFCatalog( rxTest )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, catalogOptions )

	-- fails with a nil value
	rxTest:expectSpecificError('bad argument #1 (wfCatalog) to makeModel (WFCatalog expected, got nil)', function ()
		LrmAllPhotosAlbumModel.makeModel( nil, kAlbumId )
	end)

	-- succeeds with a wfCatalog instance
	LrmAllPhotosAlbumModel.makeModel( wfCatalog, kAlbumId )
end

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

if WFPlatformInfo.isAndroid then

function LrmAllPhotosAlbumModelTest.rxTestAssetList( rxTest )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, catalogOptions )

	local allPhotosAlbumId = '10002000300040005000600070008000'
	local allPhotosAlbumModel = LrmAllPhotosAlbumModel.makeModel( wfCatalog, allPhotosAlbumId )

	local assetArgs = {
		captureDate = "2022-04-25T10:29:10.805Z",
	}

	local docValue, localOnlySettings = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, nil, assetArgs )
	rxTest:waitFor( wfCatalog:docStore():applyOverlay( docValue ) )

	local args = {
			'captureDate', 	-- sortCriteria
			true, 			-- ascending
			{}, 			-- assetFilter
			{},				-- assetTypeFilter
			{},				-- keyword
			{},				-- edited
			0, 				-- ratingFilter
			'>=', 			-- ratingComparisonOperator,
		}

	local assetList = makeSubmodel( allPhotosAlbumModel, 'assetListModel', unpack( args ) ).value
		:pluck( 'albumInfo', 'photos' )

	local assetListObs = rxTest:observe( assetList )
	rxTest:finally( function()
		assetListObs:dispose()
	end )

	assetListObs:expectData( {
		{
			id = docValue._id,
			captureDate = "2022-04-25T10:29:10.805Z",
			croppedHeight = 1000,
			croppedWidth = 1000,
			fileName = "Image01.jpg",
			importedByUser = "",
			importDate = "2023-12-20T17:19:16.448Z",
			isPopulated = true,
			isVideo = false,
			modifiedDate = "2022-04-25T10:29:10.805Z",
			thumbnailMD5 = localOnlySettings.thumbMd5,
			needsUpdateAISettings = false,
		}
	} )

end

end 

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

function LrmAllPhotosAlbumModelTest.rxTestCoverImage( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, catalogOptions )

	rxTest:expectSingleValue( wfCatalog:docStore():applyOverlay {
				type = 'album',
				_id = kAlbumId,
				private = false,
				download = false,
				updated = '2012-08-15T15:22:29',
				created = '2012-08-15T15:22:29',
			},
			':ignore:' )

	rxTest:waitFor( WFCatAlbumTestUtils.insertAlbumAssets( rxTest, wfCatalog, kAlbumId, 1 ) )

	local assetId

	rxTest:waitFor( wfCatalog:indexStore():queryOnce{ sql = 'SELECT docId FROM coreInfo' }
								:map( function( rows )

									assetId = derefOpt( rows, 1, 1 )
									return assetId

								end ) )


	local allPhotosAlbumModel = LrmAllPhotosAlbumModel.makeModel( wfCatalog, KSIdGenerator.uuidInLowercaseHex() )

	local albumObs = rxTest:observe( getSubmodel( allPhotosAlbumModel, 'coverImageModel' )( allPhotosAlbumModel ).value )
	rxTest:finally( function()
		albumObs:dispose()
	end )

	albumObs:expectData( assetId )

end

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

function LrmAllPhotosAlbumModelTest.rxTestCountForTrash( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, catalogOptions )

	local currentTime = KSDate.timeInIso8601Format()
	local purgeTime = KSDate.shiftIso8601DateTime( currentTime, 120 )

	rxTest:expectSingleValue( wfCatalog:docStore():applyOverlay {
            type = 'gen-album',
            _id = LrmAllPhotosAlbumModel.kTrashAlbumId,
            updated = '2012-08-15T15:22:29',
            created = '2012-08-15T15:22:29',
		},
		':ignore:' )

	local albumModel = LrmAllPhotosAlbumModel.makeModel( wfCatalog, LrmAllPhotosAlbumModel.kTrashAlbumId )

	local albumObs = rxTest:observe( albumModel.value )
	rxTest:finally( function()
		albumObs:dispose()
	end )

    local assetId = KSIdGenerator.uuidInLowercaseHex()

	rxTest:expectSingleValue( wfCatalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'deleted_image',
			purgeDate = purgeTime,
			userCreated = '2012-01-15T11:12:15',
			userUpdated = '2012-09-14T15:22:29',
			_localOnly = {
				localUrl = "file://someUrl.jpg"
			}
		},
		':ignore:' )

	local trashAssetCountModel = makeSubmodel( albumModel, 'trashAssetCountModel' )

	local trashAssetCountObs = rxTest:observe( trashAssetCountModel.value )
	rxTest:finally( function()
		trashAssetCountObs:dispose()
	end )

	trashAssetCountObs:expectData( {
        {
			1,
			purgeTime
		}
    } )

end

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

-- @mark flag status

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

function LrmAllPhotosAlbumModelTest.rxTestFlagStatus( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, catalogOptions )
	local userId = wfCatalog:ozUserId()

	rxTest:expectSingleValue( wfCatalog:docStore():applyOverlay {
			type = 'gen-album',
			_id = kAlbumId,
			updated = '2012-08-15T15:22:29',
			created = '2012-08-15T15:22:29',
		},
		':ignore:' )


	local albumModel = LrmAllPhotosAlbumModel.makeModel( wfCatalog, kAlbumId )

	local albumObs = rxTest:observe( albumModel.value )
	rxTest:finally( function()
		albumObs:dispose()
	end )

	local docValue = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog )

	WFTableMerge.applyOverlay( docValue, {
		reviews = {
			[ userId ] = {
				flag = 'pick',
			}
		}
	} )


	rxTest:expectSingleValue( wfCatalog:docStore():applyOverlay( docValue ), ':ignore:' )

	local flagStatusCountModel = makeSubmodel( albumModel, 'flagStatusCountModel' )

	local flagStatusCountObs = rxTest:observe( flagStatusCountModel.value )
	rxTest:finally( function()
		flagStatusCountObs:dispose()
	end )

	flagStatusCountObs:expectData {
			pick = 1,
			reject = 0,
			unflagged = 0,
		}


	rxTest:expectSingleValue( wfCatalog:docStore():applyOverlay {
			type = 'asset',
			_id = docValue._id,
			reviews = {
				[ userId ] = {
					flag = 'reject',
				}
			},
		},
		':ignore:' )

	flagStatusCountObs:expectData( {
			pick = 0,
			reject = 1,
			unflagged = 0,
		} )

end

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

return WFRxTestCase.wrapSuite( 'LrmAllPhotosAlbumModelTest', LrmAllPhotosAlbumModelTest )
