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

NmbCrPathOverrides.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.

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

local WFFileUtils = require "WFFileUtils"
local KSPathUtils = require "KSPathUtils"
local WFLogger = require "WFLogger"

--------------------------------------------------------------------------------
local logger = WFLogger 'NmbCrPathOverrides'

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

local kCRSettingsFolderName = "cr_settings"
local kCRPrefsFolderName = "cr_defaults"
local kCRCameraProfilesFolderName = "cr_cameraprofiles"

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

local function maybeCreateFolder( path )

	local ok, maybeError = pcall( WFFileUtils.mkdir, path )
	if not ok then
		logger:warnf( "Creating directory failed. Path:%s Error:%s",
			path, maybeError )
	end
	
	return ok
end

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

local function createFolderAndSet( pathType, fn, path )

	if not maybeCreateFolder( path ) then
		logger:warnf( "Setting ACR user directory failed. Type:%s Err: Folder creation failed",
				pathType )
			return false
	end

	local ok, maybeError = pcall( fn, path )
	if not ok then
		logger:warnf( "Setting ACR user directory failed. Type:%s Path:%s Error:%s",
			pathType, path, maybeError )
		return false
	end
	return true
end

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

--[[!
	\brief Returns the ACR user directories
	assocaited with a particular catalog.

	\param catalogPath (string) Base catalog path.

	\return (string,string,string)
		path to user settings directory
		path to user preferences directory
		path to user camera profile directory
]]--

local function get( catalogPath )
	
	local crSettingsDir = KSPathUtils.child( 
							catalogPath, kCRSettingsFolderName )

	local crPrefsDir = KSPathUtils.child( 
							catalogPath, kCRPrefsFolderName )
							
	local crCameraProfilesDir = KSPathUtils.child(
							catalogPath, kCRCameraProfilesFolderName )
			
	return crSettingsDir, crPrefsDir, crCameraProfilesDir

end

--------------------------------------------------------------------------------
--[[!
	\brief Sets and initializes ACR user directories.
	
	This should be called only once per app session.
	this will break if we ever support multiple catalogs 
	in the future. However, the cr_sdk currently only 
	supports calling these directory override APIs once 
	each, so we would also have to address that problem
	as well.

	\param catalogPath (string) Base catalog path.

	\return (boolean) true if the setting was successful
]]--

local function set( catalogPath )
	
	local WFCrInfo = require 'WFCrInfo'

	local crSettingsDir, crPrefsDir, crCameraProfilesDir = get( catalogPath )
			
	local ok = createFolderAndSet( "Presets", WFCrInfo.setUserPresetDir, crSettingsDir )
	
	if ok then
		ok = createFolderAndSet( "Preferences", WFCrInfo.setUserPreferencesDir, crPrefsDir )
	end
	
	if ok then
		ok = createFolderAndSet( "CameraProfiles", WFCrInfo.setUserCameraProfilesDir, crCameraProfilesDir )
	end
	
	return ok

end



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

return {
	get = get,
	set = set,
}
