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

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.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local WFTableMerge = require 'WFTableMerge'
local KSDate = require 'KSDate'
local KSTableUtils = require "KSTableUtils"

	-- Lrmobile_models
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'
local LrmAllPhotosAlbumModel = require 'LrmAllPhotosAlbumModel'
local LrmDocValueTestFactory = require 'LrmDocValueTestFactory'

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

local derefOpt = KSTableUtils.derefOpt

local LrmAllPhotosAlbumModelTest = {}

local kAlbumId = KSIdGenerator.uuidInLowercaseHex()

local TEST_TIMEOUT = 10

local wfCatalog, userId

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

function LrmAllPhotosAlbumModelTest.setUp()

	local _

	wfCatalog, _, userId = LrmCatalogTestHelper.makeWFCatalog( )
end

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

function LrmAllPhotosAlbumModelTest.tearDown()

	if wfCatalog then

		wfCatalog:close()
		wfCatalog = nil

	end

end

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

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 )
	-- 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

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

function LrmAllPhotosAlbumModelTest.rxTestCoverImage( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	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:expectSingleValue( LrmDocValueTestFactory.insertAlbumAssets( wfCatalog:docStore(), kAlbumId, 1 ), ':ignore:' )

	local assetId

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

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

								end ), ':ignore:' )


	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 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 )

	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 = LrmDocValueTestFactory.makeCompleteAsset()

	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 )
