/***********************************************************************/
/*                                                                     */
/*                      ADOBE CONFIDENTIAL                             */
/*                   _ _ _ _ _ _ _ _ _ _ _ _ _                         */
/*                                                                     */
/*  Copyright 2016 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.                                    */
/*                                                                     */
/***********************************************************************/

function Settings(/*[String]*/ inSettingsFilePath)
{
    var filepath = inSettingsFilePath;

    if (!isValidProperty(filepath))
    {
        filepath = getPreferencesFolder() + SETTINGS_FILENAME;
    }

    this.load = function()
    {
        var res = cep.fs.readFile(filepath);

        if (isValidProperty(res) && res.err == cep.fs.NO_ERROR)
        {
            try
            {
                var obj = JSON.parse(res.data);

                if (isValidProperty(obj))
                {
                    setSettings(obj);
                }
            }
            catch(exc)
            {
                exclog(exc);
                wf = null;
            }
        }
        else
        {
            dbglogInfo("Can't load settings file (" + filepath + "): " + res.err);
        }
    }

    function setSettings(/*[Object]*/ inJSON)
    {
        if (isValidProperty(inJSON['UseRemoteWorkflowStorage']))
        {
			dbglogInfo('Settings: Set global property: gUseRemoteWorkflowStorage = ' + inJSON['UseRemoteWorkflowStorage']);
            gUseRemoteWorkflowStorage = inJSON['UseRemoteWorkflowStorage'];
        }

        if (isValidProperty(inJSON['UseRemoteContentStorage']))
        {
			dbglogInfo('Settings: Set global property: gUseRemoteContentStorage = ' + inJSON['UseRemoteContentStorage']);
            gUseRemoteContentStorage = inJSON['UseRemoteContentStorage'];
        }

        if (isValidProperty(inJSON['CacheRemoteContent']))
        {
			dbglogInfo('Settings: Set global property: gCacheRemoteContent = ' + inJSON['CacheRemoteContent']);
            gCacheRemoteContent = inJSON['CacheRemoteContent'];
        }

        if (isValidProperty(inJSON['Remote_URL_Release']))
        {
			dbglogInfo('Settings: Set global property: REMOTE_URL_RELEASE = ' + inJSON['Remote_URL_Release']);
            REMOTE_URL_RELEASE = inJSON['Remote_URL_Release'];
        }

        if (isValidProperty(inJSON['IgnoreContentBlackList']))
        {
			dbglogInfo('Settings: Set global property: gIgnoreContentBlackList = ' + inJSON['IgnoreContentBlackList']);
            gIgnoreContentBlackList = inJSON['IgnoreContentBlackList'];
        }

        if (isValidProperty(inJSON['DebugLog']))
        {
			__loglevel__ = inJSON['DebugLog'] ? DebugLog.LOGLEVEL_FATAL : DebugLog.LOGLEVEL_NONE;
			dbglogInfo('Settings: Set global property: __loglevel__ = ' + __loglevel__);
        }

        if (isValidProperty(inJSON['DebugLogLevel']))
        {
            try
            {
                var level = parseInt(inJSON['DebugLogLevel']);

                if (!isNaN(level))
                {
                    __loglevel__ = level;
					dbglogInfo('Settings: Set global property: __loglevel__ = ' + __loglevel__);
                }
            }
            catch(e)
            {
                exclog(e);
            }
        }

        if (isValidProperty(inJSON['DebugLogOutput']))
        {
            try
            {
                var output = parseInt(inJSON['DebugLogOutput']);

                if (!isNaN(output))
                {
                    __logoutput__ = output;
					dbglogInfo('Settings: Set global property: __logoutput__ = ' + __logoutput__);
                }
            }
            catch(e)
            {
                exclog(e);
            }
        }

        if (isValidProperty(inJSON['Globals']))
		{
			var globals = inJSON['Globals'];

			for (var p in globals)
			{
				var propSrc = p + '=' + globals[p];
				dbglogInfo('Settings: Set global property: ' + propSrc);
				globalEval(propSrc);
			}
		}
    }
}
