/***********************************************************************/
/*                                                                     */
/*                      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.                                    */
/*                                                                     */
/***********************************************************************/

//////////////////////////////////////////////////////////////
//
// Initialize extension
//
function onLoaded() 
{
	var settings = new Settings();
	settings.load();

	var contentStorage = new LocalContentStorage(getSurfaceFolder());
	var job = contentStorage.getString(null, '$$$/onboarding/audition/title');
	job.wait(function(/*[String]*/ inTitle)
	{
		var cs = new CSInterface();
		cs.setWindowTitle(inTitle);
	});

    initAppearance();
    setPageFont(FONT_WIN, FONT_MAC);

	gBroadcaster = new MessageBroadcaster();
    gCtrl = new Controller(onEventDispatch);
}

//////////////////////////////////////////////////////////////
//
// Content storage creator
//
IContentStorage.create = function()
{
    return new MultiContentStorage([getOnBoardingFolder(true) + 'Content/', getOnBoardingFolder(false) + 'Content/'], 
    							   (gUseRemoteContentStorage ? getBaseURL() : null), 
    							   (gCacheRemoteContent ? (getCacheFolder() + 'Content/') : null));
}

//////////////////////////////////////////////////////////////
//
// Workflow storage creator
//
IWorkflowStorage.create = function()
{
	return new MultiWorkflowStorage([getOnBoardingFolder(true) + 'Workflows/', getOnBoardingFolder(false) + 'Workflows/'], 
									(gUseRemoteWorkflowStorage ? getBaseURL() : null), 
									(gCacheRemoteContent ? (getCacheFolder() + 'Workflows/') : null));
}

//////////////////////////////////////////////////////////////
//
// Dispatch workflow selected
//
function onWorkflowSelect(inWorkflowID)
{
	if (isValidProperty(Controller.instance))
	{
		Controller.instance.selectWorkflow(inWorkflowID);
	}
}

//////////////////////////////////////////////////////////////
//
// Dispatch events
//
function onEventDispatch(/*[String]*/ inReason, /*[Any]*/ inData)
{
	gBroadcaster.sendMessage(inReason, inData);
}

//////////////////////////////////////////////////////////////////////////////
//
// Get path to Onboarding content
//
function getOnBoardingFolder(/*[Boolean]*/ inInternal)
{
	var obFolder = '';
	var cs = new CSInterface();
	
	if (inInternal)
	{
		obFolder = cs.getSystemPath(SystemPath.EXTENSION) + '/content/';
	}
	else
	{
		var path = getApplicationFolder();
		var pos = path.lastIndexOf('/');

		if (pos >= 0)
		{
			path = path.substring(0, pos);
		}
		else
		{
			path = '';
		}
		
		if (path.length)
		{
			obFolder = path + '/test_content/';
		}
	}
			
	return obFolder;
}

//////////////////////////////////////////////////////////////////////////////
//
// Get path to WorkflowTriggerComponent files
//
function getComponentFolder()
{
	var cs = new CSInterface();
	return cs.getSystemPath(SystemPath.EXTENSION) + '/component/';
}

//////////////////////////////////////////////////////////////////////////////
//
// Get path to surface files
//
function getSurfaceFolder()
{
	var cs = new CSInterface();
	return cs.getSystemPath(SystemPath.EXTENSION) + '/surfaces/';
}

//////////////////////////////////////////////////////////////////////////////
//
// Get path to cached content
//
function getCacheFolder()
{
	return getPreferencesFolder() + 'onboarding_cache/';
}

//////////////////////////////////////////////////////////////////////////////
//
// Get base url for remote content
//
function getBaseURL()
{
	var baseURL = REMOTE_URL_RELEASE;
	baseURL = baseURL.trim();
	
	if (baseURL.lastIndexOf('/') != baseURL.length-1)
	{
		baseURL += '/';
	}
	
	return baseURL;
}

//////////////////////////////////////////////////////////////////////////////
//
// Validate app version and platform
//
function validateSystem(/*String*/ inVersion, /*String*/ inPlatform)
{
	var ret = true;

	var cs = new CSInterface();

	if (isValidProperty(inPlatform) && inPlatform.length > 0)
	{
		var platform = cs.getOSInformation().indexOf("Windows") == 0 ? "windows" : "macos";
		ret = (platform == inPlatform);
	}

	if (ret && isValidProperty(inVersion) && inVersion.length > 0)
	{
		var appVersionParts = cs.getHostEnvironment().appVersion.toString().split(".");
		var versionParts = inVersion.toString().split(".");

		function makeNumber(/*Array*/ inNumberParts)
		{
			var number = parseInt(inNumberParts[0]);

			if (!isNaN(number) && inNumberParts.length > 1)
			{
				for (var i=1; i<inNumberParts.length; i++)
				{
					var num = parseInt((inNumberParts[i]));

					if (!isNaN(num))
					{
						number += (num / (i * 10));
					}
				}
			}

			return number;
		}

		var appVersion = makeNumber(appVersionParts);
		var version = makeNumber(versionParts);

		ret = (appVersion >= version);
	}

	return ret;
}

//////////////////////////////////////////////////////////////////////////////
//
// Global error handler
//
window.onerror = function(messageOrEvent, source, lineno, colno, error)
{
	dbglogFatal("EXCEPTION: Unhandled exception\nError: " + error + "\nSource: " + source + "\nLine: " + lineno);
    
    // restart
    gCtrl = new Controller(onEventDispatch);
};