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

WFCatPersonTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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.

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

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

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

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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog

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

local WFCatPersonTest = {}

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

local function getPeopleList( wfCatalog, quiesceUntilUpToDate )
	return wfCatalog:indexStore():query {
		sql = [[SELECT docId, personId, parentId, hidden, name
				FROM person]],
		quiesceUntilUpToDate = quiesceUntilUpToDate,
	}
	:map( function( rows )
		local people = {}
		for i, row in ipairs( rows ) do
			local person = {}
			person.id = row[ 1 ]
			person.personId = row[ 2 ]
			person.parentId = row[ 3 ]
			person.hidden = row[ 4 ] ~= 0
			person.name = row[ 5 ]
			
			people[ #people + 1 ] = person
		end
	
		return people
	end )
end

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

function WFCatPersonTest.rxTestAddPersons( rxTest )

	rxTest:setTimeLimit( 50 )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )

	local personList = {
		{ 
			name = 'John',
			uuid = '1',
			hidden = false,
			cover = "a"
		},
		{ 
			name = 'Jane',
			uuid = '2',
			hidden = false,
			cover = "b"
		},
	}
	
	local addedDocs
	rxTest:expectSingleValue( 
		WFCatPerson.addPersons( fakeWFCatalog, personList )
			:flatMap( function( )
				
				return fakeWFCatalog:docStore():changedDocs { fromChangeCounter = 1 }
					:tap( function( docs )
							addedDocs = docs
					end )
			end ),
		':ignore:' 
	)
	
	
	assert( addedDocs and #addedDocs == 2, "Persons documents not added" )
	assert( addedDocs[ 1 ].name == 'John', "Person name has not been added correctly" )
	assert( addedDocs[ 2 ].name == 'Jane', "Person name has not been added correctly" )
	assert( addedDocs[ 2 ]._id == '2', "Person uuid has not been added correctly" )

end

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

function WFCatPersonTest.rxTestBasicPerson( rxTest )
	
	rxTest:setTimeLimit( 60 )

	local wfCatalog = createFakeWFCatalog( rxTest )

	local peopleData = rxTest:observe( getPeopleList( wfCatalog, true ) )

	local personList = {
		{ 
			name = 'John',
			uuid = 'ca09100b9fe0ba5c14e784e886642604',
			hidden = false,
			cover = "a"
		},
		{ 
			name = 'Jane',
			uuid = 'ca09100b9fe0ba5c14e784e886642605',
			hidden = false,
			cover = "b"
		},
	}
	rxTest:expectSingleValue( WFCatPerson.addPersons( wfCatalog, personList ), ':ignore:' )
	
	local expectedPeopleData = {
		{
			name = 'John',
			personId = 'ca09100b9fe0ba5c14e784e886642604',
			hidden = false,
			id = 1
		},
		{
			name = 'Jane',
			personId = 'ca09100b9fe0ba5c14e784e886642605',
			hidden = false,
			id = 2
		}
	}
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	rxTest:expectOnlyCompleted( WFCatPerson.updatePersonName( wfCatalog, "ca09100b9fe0ba5c14e784e886642605", "Mary" ):takeLast(0) )
	expectedPeopleData[ 2 ].name = 'Mary'
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	
	local peopleIdNames = {
		{
			id = 2,
			name = "Jill"
		},
		{
			id = 1,
			name = "Jill"
		},
	}
	rxTest:expectOnlyCompleted( WFCatPerson.updatePeopleName( wfCatalog, peopleIdNames ):takeLast(0) )
	expectedPeopleData[ 1 ].name = 'Jill'
	expectedPeopleData[ 2 ].name = 'Jill'
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	
	rxTest:expectOnlyCompleted( WFCatPerson.updatePersonName( wfCatalog, "ca09100b9fe0ba5c14e784e886642605", nil ):takeLast(0) )
	expectedPeopleData[ 2 ].name = nil
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	
	rxTest:expectOnlyCompleted( WFCatPerson.hidePerson( wfCatalog, "ca09100b9fe0ba5c14e784e886642605", true ):takeLast(0) )
	expectedPeopleData[ 2 ].hidden = true
	peopleData:expectDataWithinTime( 5, expectedPeopleData )

	local hidemap = {}
	hidemap["ca09100b9fe0ba5c14e784e886642604"] = true
	hidemap["ca09100b9fe0ba5c14e784e886642605"] = false
	rxTest:expectOnlyCompleted( WFCatPerson.hidePeople( wfCatalog, hidemap ):takeLast(0) )
	expectedPeopleData[ 1 ].hidden = true
	expectedPeopleData[ 2 ].hidden = false
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	
	local personList2 = {
		{ 
			name = 'John',
			uuid = 'ca09100b9fe0ba5c14e784e886642606',
			hidden = false,
			cover = "b"
		},
		{ 
			name = 'Jane',
			uuid = 'ca09100b9fe0ba5c14e784e886642607',
			hidden = false,
			cover = "b"
		},
	}
	rxTest:expectSingleValue( WFCatPerson.addPersons( wfCatalog, personList2 ), ':ignore:' )
	

	local data = {
			name = 'Hello',
			personId = 'ca09100b9fe0ba5c14e784e886642606',
			hidden = false,
			id = 3
		}
	expectedPeopleData[ #expectedPeopleData + 1 ] = data
	
	local data1 = {
			name = 'World',
			personId = 'ca09100b9fe0ba5c14e784e886642607',
			hidden = false,
			id = 4
		}
	expectedPeopleData[ #expectedPeopleData + 1 ] = data1
	
	local personIds = {}
	personIds[1] = "ca09100b9fe0ba5c14e784e886642606"
	personIds[2] = 'ca09100b9fe0ba5c14e784e886642607'
	personIds[3] = "ca09100b9fe0ba5c14e784e886642604"
	rxTest:expectOnlyCompleted( WFCatPerson.shallowMergePeople( wfCatalog, "ca09100b9fe0ba5c14e784e886642604", personIds, 'Matt' ):takeLast(0) )
	expectedPeopleData[ 1 ].name = 'Matt'
	expectedPeopleData[ 3 ].name = 'Matt'
	expectedPeopleData[ 3 ].parentId = "ca09100b9fe0ba5c14e784e886642604"
	expectedPeopleData[ 4 ].name = 'Matt'
	expectedPeopleData[ 4 ].parentId = "ca09100b9fe0ba5c14e784e886642604"
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	
	personIds = {
		{
			id = 3,
			name = "Matt"
		},
		{
			id = 4,
			name = "Matt"
		}
	}
	rxTest:expectOnlyCompleted( WFCatPerson.splitPeople( wfCatalog, personIds ):takeLast(0) )
	expectedPeopleData[ 3 ].parentId = nil
	expectedPeopleData[ 4 ].parentId = nil
	peopleData:expectDataWithinTime( 5, expectedPeopleData )
	
	peopleData:dispose()
end

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

return WFRxTestCase.wrapSuite( 'WFCatPersonTest', WFCatPersonTest )
