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

KSPrefsImpLua.lua

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

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

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

WARNING [matthew 2013-03-01]: Win32 doesn't have an application specific key/value
preference store (other than the registry) so keep a table that gets written out to
local app data storage.

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

	-- WF.core.os
local KSPathUtils = require 'KSPathUtils'
local WFFileUtils = require 'WFFileUtils'

	-- WF.core.table
local WFTableArchiving = require 'WFTableArchiving'


--============================================================================--
--[[!
    \brief Implementation for KSPrefs on Win32 and other platforms that don't have an API for
    a native app specific key/value storage area. Since Win32 doesn't have such a thing, we
    will serialize a prefs cache in the local app data and manage the prefs in Lua.  This lua file
    is loaded by the native code into its own universe to keep access to the prefs thread safe.
]]--

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

local prefsTable = false

local prefsDirectory = KSPathUtils.child( KSPathUtils.getStandardFilePath( "adobeAppData" ), "Preferences" )
WFFileUtils.mkdir( prefsDirectory )

local prefsFilename = KSPathUtils.child( prefsDirectory, "preferences.wfprefs" )
prefsFilename = KSPathUtils.shortPath( prefsFilename )

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

local function loadPreferences( )
	
	local file = io.open( prefsFilename, "r" )
	local prefsString
	
	if file then
		prefsString = file:read( "*a" )
		file:close()
	else
		-- io.open fails if the path contains unicode characters.
		prefsString = WFFileUtils.getFileContents( prefsFilename )
	end
	
	if prefsString then
	
		local loadfunc = loadstring( 'return ' .. prefsString )
		
		local ok, ret = pcall( loadfunc )
		
		return ok and ret or {}
		
	end
	
	return {}

end

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

local function savePreferences( )
	
	local prefsString = WFTableArchiving.tableToString( prefsTable or {} )
	
	local file = io.open( prefsFilename, "w+" )
	
	if file then
		file:write( prefsString )
		file:close()
	else
		-- io.open fails if the path contains unicode characters.
		WFFileUtils.saveToFile( prefsFilename, prefsString )
	end
	
end

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

prefsTable = loadPreferences() or {}

local KSPrefs = {}


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

function KSPrefs.fromNativeCodeGetValue( key )

	return prefsTable[ key ]

end

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

function KSPrefs.fromNativeCodeSetValue( key, value )
	
	local valueType = type( value )
	
	if valueType == "string" or valueType == "boolean" or
		valueType == "number" or valueType == "nil" then
		
		prefsTable[ key ] = value

		savePreferences()
		
	else
	
		error( "prefs values must be scalar types only (boolean, number, string)" )
	
	end
end

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

function KSPrefs.deletePrefsFile( maybePrefsFile )

	local oldPrefs = prefsTable
	prefsTable = {}
	WFFileUtils.deleteFile( maybePrefsFile or prefsFilename )
	prefsFilename = KSPathUtils.shortPath( prefsFilename )
	
	return oldPrefs
	
end

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

return KSPrefs
