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

WFCatUtils.lua

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

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

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

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

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFPlatformInfo = require 'WFPlatformInfo'
local KSDate = require 'KSDate'
local KSTableUtils = require 'KSTableUtils'

local kGlobalDocumentId

if WFPlatformInfo.isMobile() then

	-- WF.oz_client.lrmobile_only
	local WFOzSyncConstants = require 'WFOzSyncConstants'

	kGlobalDocumentId = WFOzSyncConstants.kGlobalDocumentId

else

	kGlobalDocumentId = '00000000000000000000000000000000'

end

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

local check = assert( WFArguments.check )
local derefOpt = assert( KSTableUtils.derefOpt )

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

local WFCatUtils = {}

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

function WFCatUtils.createBaseOverlayWithUserUpdatedField( id )

	check( '?string|number' )

	return {
		_id = id,
		userUpdated = KSDate.timeInIso8601Format()
	}

end

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

function WFCatUtils.createBaseOverlayWithUserCreatedFields( id )

	check( '?string|number' )
	
	local now = KSDate.timeInIso8601Format()
	
	return {
		_id = id,
		userCreated = now,
		userUpdated = now
	}

end

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

function WFCatUtils.addUserUpdatedFields( doc )
	
	check( 'table' )
	
	doc.userUpdated = KSDate.timeInIso8601Format()
	
	return doc
end

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

function WFCatUtils.addUserCreatedFields( doc )
	
	check( 'table' )
	
	local now = KSDate.timeInIso8601Format()
	
	if not doc.userCreated then
		doc.userCreated = now
	end
	
	doc.userUpdated = now
	
	return doc
end

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

function WFCatUtils.isUnsupportedFormat( doc )
	assert( require 'WFPlatformInfo'.hasACR, "ACR required for HEIC" )
	
	local fileName = derefOpt( doc, 'importSource', 'fileName' )
	fileName = fileName and string.lower( fileName )
	
	local isUnsupported = false
	local WFCrInfo = assert( require 'NmbAcr'.WFCrInfo )
	if not WFCrInfo.supportsHeic() and fileName and 
			( string.match( fileName, '.heic$' ) 
			or string.match( fileName, '.heif$' ) 
			or string.match( fileName, '.hif$' ) ) then
		isUnsupported = true
	end
	
	return isUnsupported
end

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

function WFCatUtils.isEdited( doc )

	check( 'table' )

	local createdDateTime = derefOpt( doc, 'userCreated' ) 
	local updatedDateTime = KSDate.getLatestDate( 
								derefOpt( doc, 'develop', 'userUpdated' ),
								derefOpt( doc, 'develop', 'adjustments','userUpdated' ),
								derefOpt( doc, 'develop', 'temporal','userUpdated' )
							)
	
	local isEdited = updatedDateTime ~= nil and updatedDateTime ~= createdDateTime

	return isEdited
end

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

function WFCatUtils.needsUpdateAISettings( doc )

	return not not derefOpt( doc, 'develop', 'needsUpdateAISettings' )

end

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

function WFCatUtils.catalogIsAssociatedWithThisAccount( catalog )

	local kAccountCheckLocalDocId = 'accountCheck'

	return catalog:docStore():localDocValue( kAccountCheckLocalDocId )

		:flatMap( function( docValue )

			local cachedAccountId = KSTableUtils.derefOpt( docValue, 'accountId' )

			local expectedAccountId = catalog:ozUserId()

			if not cachedAccountId then

				local isGlobalCatalog = catalog:ozCatalogId() == kGlobalDocumentId

				if not isGlobalCatalog then

					-- There is no previous association

					local accountCheckDocument = {
						_id = kAccountCheckLocalDocId,
						accountId = catalog:ozUserId()
					}

					return catalog:docStore():setLocalDocValue( accountCheckDocument )

						:map( function()

							return true, expectedAccountId, cachedAccountId

						end )

				else

					-- https://jira.corp.adobe.com/browse/WF-2377 : The way LriOS handles the
					-- freemium -> subscriber catalog migration, they always copy any freemium,
					-- global catalog (0000..) and use it as a starting point for the subscriber.
					-- This is true, even if you log in to a *different* subscriber account, but
					-- have a freemium catalog on the device. Because of this, we do not bother
					-- setting the accountCheckDocument for global catalogs, as they may
					-- intentionally become associated with a different user ID.

					return Observable.just( true, expectedAccountId, cachedAccountId )

				end

			else

				local accountMatches = cachedAccountId == expectedAccountId

				return Observable.just( accountMatches, expectedAccountId, cachedAccountId )

			end

		end )

end

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

return WFCatUtils
