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

WFCatCollaborationActivityTestUtils.lua

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

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

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

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

local KSIdGenerator = require 'KSIdGenerator'
local WFCatAlbums = require 'WFCatAlbums'
local WFCatCollaboration = require 'WFCatCollaboration'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFPlatformInfo = require 'WFPlatformInfo'
local WFCatMockCatalog = require 'WFCatMockCatalog'

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

local cachedSpaceId = "bogus"

local kTestDate1 = "2011-11-11T11:11:11.111111Z"
local kTestDate2 = "2020-11-11T11:11:11.111111Z"

local kDefaultShareIsPrivate = true

local expectedRequestResponse = {
		{
			url = "url", -- "/v2/catalogs/<albumId>/spaces/<spaceId>"
			data = "data",
			options = {
				requestHeaders = {
					{
						header = "Content-Type",
						value = "application/json",
					},
				},
				requestMethod = "PUT",
			},
			dataFilter = function( data ) return "data" end,
			urlFilter = function( url ) return "url" end,
			response = "none",
			headers = {
				status_code = '201',
			},
		},
		{
			url = "url", -- "/v2/catalogs/<albumId>/spaces/<spaceId>"
			data = "data",
			options = {},
			dataFilter = function( data ) return "data" end,
			urlFilter = function( url ) 
				cachedSpaceId = string.match( url, "spaces/(.+)" )
				return "url" 
			end,
			response = [[while (1) {} {"base":"https://photos-stage.adobe.io/v2/catalogs/0c20896ba35f42c9a3fb478fb82ad09a/","id":"]]..'<spaceId>'..[[",
				"type":"space","subtype":"shared","created":"2018-04-23T17:29:29.524635Z",
				"updated":"2018-04-23T17:29:30.126268Z","country_blocked":false,"createdOnClient":"AdobeNimbus-1.4.developer-OSX",
				"revision_ids":["d7c26c4d7f14139e6a1767f25bbdf3b5","0a236fccbf4ec0eed40fd2ef4e52580f"],"links":{"self":{"href":"spaces/<spaceId>"},
				"/rels/web_space":{"href":"http://bit.ly/2K7ltDy"},"/rels/space":{"href":"/v2/spaces/<spaceId>"},
				"/rels/space_resources":{"href":"/v2/spaces/<spaceId>/resources"}},
				"payload":{"private":]] .. tostring( kDefaultShareIsPrivate ) .. [[,"download":false,"metadata":false,"location":false,"allowInviteRequests":true}}]],
				headers = {
				status_code = '200',
			},

			responseFilter = function( response )
				response = response:gsub( '<spaceId>', cachedSpaceId )
				return response
			end,
			--fail = "Network connection lost",
		},
}

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

local WFCatCollaborationActivityTestUtils = {}

local function createAlbum( rxTest, catalog )

	local localId, globalId
	
	rxTest:expectOnlyCompleted(

			WFCatAlbums.createAlbum( catalog, 'WFCatCollaboration' )
				:tap( function( localAlbumId, globalAlbumId )
					localId, globalId = localAlbumId, globalAlbumId 
				end )
				:takeLast( 0 )
		)
		
	return localId, globalId
	
end

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

local function buildDocValue( rxTest, wfCatalog, assetId, overrides )

	assetId = assetId or KSIdGenerator.uuidInLowercaseHex()
	return WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId, overrides )

end


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

function WFCatCollaborationActivityTestUtils.createAlbumWithAsset( rxTest, catalog )
	
	-- Create and share album.
	local localAlbumId, globalAlbumId = createAlbum( rxTest, catalog )
	
	-- Add single asset to new album
	local globalAssetId = KSIdGenerator.uuidInLowercaseHex()
	local assetDoc = buildDocValue( rxTest, catalog, globalAssetId )

	rxTest:expectSingleValue( catalog:docStore():setDocValue( assetDoc ), ':ignore:' )
	
	local localAssetId
	rxTest:expectSingleValue(
		catalog:docStore():docValue( globalAssetId )
			:map( function( docValue )
				localAssetId = docValue._localId
				return true
			end ), true )
	
	rxTest:expectSingleValue( catalog:docStore():setDocValue {
			_id = KSIdGenerator.uuidInLowercaseHex(),
			type = 'album_asset',
			album = { id = globalAlbumId },
			asset = { id = globalAssetId }
		}, ':ignore:' )
	
	local albumInfo = {
		localId = localAlbumId,
		globalId = globalAlbumId,
	}
	
	local assetInfo = {
		localId = localAssetId,
		globalId = globalAssetId,

	}
	
	return albumInfo, assetInfo
	
end


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

function WFCatCollaborationActivityTestUtils.createSharedAlbumWithAsset( rxTest, catalog, opts )

	local useMockOz = true
	
	if type(opts) == 'table' and not (opts.useMockOz == nil) then
		useMockOz = opts.useMockOz
	end	
	
	-- NOTE: the semantics of useMockOz are more clear in use with true corresponding to "yes"
	--       this needs to be inverted to work with the semantics of WFCatCollaboration.shareAlbum	
	local createLocalShares = not useMockOz
	
	-- Create and share album.
	local localAlbumId, globalAlbumId = createAlbum( rxTest, catalog )
	
	local spaceId

	rxTest:expectSingleValue(
			WFCatCollaboration.shareAlbum( catalog, localAlbumId, {}, true, createLocalShares )
				:tap( function( result )
					spaceId = result.id 
				end ), ':ignore:' )
	
	-- Add single asset to new album
	local globalAssetId = KSIdGenerator.uuidInLowercaseHex()
	local assetDoc = buildDocValue( rxTest, catalog, globalAssetId )

	rxTest:expectSingleValue( catalog:docStore():setDocValue( assetDoc ), ':ignore:' )
	
	local localAssetId
	rxTest:expectSingleValue(
		catalog:docStore():docValue( globalAssetId )
			:map( function( docValue )
				localAssetId = docValue._localId
				return true
			end ), true )
	
	rxTest:waitFor( catalog:docStore():setDocValue {
			_id = KSIdGenerator.uuidInLowercaseHex(),
			type = 'album_asset',
			album = { id = globalAlbumId },
			asset = { id = globalAssetId }
		} )
	
	local albumInfo = {
		localId = localAlbumId,
		globalId = globalAlbumId,
		spaceId = spaceId
	}
	
	local assetInfo = {
		localId = localAssetId,
		globalId = globalAssetId,

	}
	
	return albumInfo, assetInfo
	
end


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

function WFCatCollaborationActivityTestUtils.createComment( spaceId, assetId, userId, body )
	return {
		_id = KSIdGenerator.uuidInLowercaseHex(),
		type = 'comment',
		subtype = 'user',
		space = { id = spaceId },
		asset = { id = assetId },
		author = { id = userId },
		body = body,
		userCreated = kTestDate1,
		userUpdated = kTestDate2,
	}
end

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

function WFCatCollaborationActivityTestUtils.createFavorite( spaceId, assetId, userId )
	
	return {
		_id = KSIdGenerator.uuidInLowercaseHex(),
		type = 'favorite',
		subtype = 'user',
		space = { id = spaceId },
		asset = { id = assetId },
		author = { id = userId },
		userCreated = kTestDate1,
		userUpdated = kTestDate2,
	}

end

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

function WFCatCollaborationActivityTestUtils.createFakeWFCatalog( rxTest, useEmptyRequestResponse )

	local optArgs = {}
	
	local expectedRequestResp = expectedRequestResponse
	
	if useEmptyRequestResponse == true then
		expectedRequestResp = nil
	end
	
	if WFPlatformInfo.isMobile() then
		optArgs.indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
	end
	
	return WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, expectedRequestResp, optArgs )
	
end

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

return WFCatCollaborationActivityTestUtils