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


//---------------   GLOBAL VARIABLES   ---------------

var helpDoc = MM.HELP_cmdNativeAppSetup; //help API

var _LIST_TARGET_OS_ANDROID = null;
var _LIST_TARGET_OS_IPHONE = null;
var _LIST_TARGET_OS_IPAD = null;

//--------------------------------------------------------------------
// FUNCTION:
//   canAcceptCommand()
//
// DESCRIPTION:
//  DW API called to determine if this menu item is enabled or not
//
// ARGUMENTS: 
//   Nothing.
//
// RETURNS:
//   boolean
//--------------------------------------------------------------------
function canAcceptCommand()
{
    return dw.nativeAppFramework.hasAppFWSettings() && site.getCurrentSite() != "";
}

//--------------------------------------------------------------------
// FUNCTION:
//   isDOMRequired()
//
// DESCRIPTION:
//  DW API called to determine if this menu item is available in code view
//
// ARGUMENTS: 
//   Nothing.
//
// RETURNS:
//   false because this doesnt' require a dom and can work in code view
//--------------------------------------------------------------------
function isDOMRequired()
{ 
	return false;
}

//--------------------------------------------------------------------
// FUNCTION:
//   commandButtons()
//
// DESCRIPTION:
//  DW API called to get the command Buttons for the dialog
//
// ARGUMENTS: 
//   Nothing.
//
// RETURNS:
//   Array of Buttons.
//--------------------------------------------------------------------
function commandButtons()
{
   return new Array("PutButtonsOnBottom", "OkButton defaultButton", dw.loadString('Startup/MMinit/MM.BTN_Save'), "if (saveSettings()) window.close()",
                    "PutButtonOnLeft", dw.loadString('Startup/MMinit/MM.BTN_Help'), "displayHelp()",
                    "CancelButton", dw.loadString('Startup/MMinit/MM.BTN_Cancel'), "window.close()" );
}


function initializeUI()
{
	//Init or hide the settings that aren't relevant - we hide them first
	//because populating the dropdowns can take a bit so if we hide everything
	//onw we can force the window to update once before we finish to it looks 
	//correct while we load the rest
	var iOSSDLList;
	var length;
	var iOSSupported = false; 
	
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPHONE) )
		iOSSupported = true;
	else
		document.getElementById("iPhoneTargetGroup").style.display = "none";
	
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPAD) )
		iOSSupported = true;
	else
		document.getElementById("iPadTargetGroup").style.display = "none";
	
	//The startup screen is only supported on iOS for now
	if( !iOSSupported )
		document.getElementById("startupScreenGroup").style.display = "none";
		
	if (!dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_ANDROID))
		document.getElementById("androidTargetGroup").style.display = "none";

	//udpate our view before we populate the SDHs below
	window.resizeToContents();
	window.updateWindow();
	
	 var siteName = site.getCurrentSite();

    //load the settings
    var appData = dw.nativeAppFramework.loadAppData(siteName);
	
	//Set them to the controls
	document.getElementById("bundleId").value = appData.bundleId;
	document.getElementById("appName").value = appData.name;
	document.getElementById("author").value = appData.author;
	document.getElementById("version").value = appData.version;
	document.getElementById("iconPath").value = appData.iconPath;
	document.getElementById("startupScreenPath").value = appData.startupScreenPath;
	document.getElementById("targetPath").value = appData.targetPath;
	
	window.updateWindow();

	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPHONE) )
	{
    	iOSSDLList = dw.nativeAppFramework.getiOSSDKList(NativeAppFramework.DEVICE_IPHONE);
    
		_LIST_TARGET_OS_IPHONE = new ListControl("targetOSiPhone");
		_LIST_TARGET_OS_IPHONE.setAll(iOSSDLList, iOSSDLList);
        
        // if this is our 1st time in, pre-select the highest numbered SDK
        length = iOSSDLList.length;
        if (appData.targetOSiPhone == "" && length > 0)
            _LIST_TARGET_OS_IPHONE.pickValue(iOSSDLList[length - 1]);
        else
            _LIST_TARGET_OS_IPHONE.pickValue(appData.targetOSiPhone);
	}
	
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPAD) )
	{
    	iOSSDLList = dw.nativeAppFramework.getiOSSDKList(NativeAppFramework.DEVICE_IPAD);
    
		_LIST_TARGET_OS_IPAD = new ListControl("targetOSiPad");
		_LIST_TARGET_OS_IPAD.setAll(iOSSDLList, iOSSDLList);
        
        // if this is our 1st time in, pre-select the highest numbered SDK
        length = iOSSDLList.length;
        if (appData.targetOSiPad == "" && length > 0)
            _LIST_TARGET_OS_IPAD.pickValue(iOSSDLList[length - 1]);
        else
            _LIST_TARGET_OS_IPAD.pickValue(appData.targetOSiPad);
		iOSSupported = true;
	}

	if (dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_ANDROID)) 
	{
		//If there are no AVDs created, the function will automatically create one. We 
		//can optionall pass in the targetID we want this default AVD to use
		var defaultTargetId = getAndroidMaxTargetIdForMaxSDKVersion(g_androidMaxSDKVersion);
		if( !defaultTargetId || defaultTargetId < 1 )
		{
			dwscripts.debugAlert("There's no targets meeting our version, the SDK might not be installing correctly!");
		}
		
		var avdList = dw.nativeAppFramework.getAndroidAVDList(defaultTargetId);
		_LIST_TARGET_OS_ANDROID = new ListControl("targetOSAndroid");
		_LIST_TARGET_OS_ANDROID.setAll(avdList, avdList);
        
        // if this is our 1st time in, pre-select the first AVD
        length = avdList.length;
        if (appData.targetOSAndroid == "" && length > 0)
            _LIST_TARGET_OS_ANDROID.pickValue(avdList[0]);
        else
            _LIST_TARGET_OS_ANDROID.pickValue(appData.targetOSAndroid);
			
		//add an app activate event listner so if someone leaves DW and 
		//defines a new AVD we can reload the list
		document.documentElement.addEventListener("DWAppActivate", function(e){ reloadAVDs(); } , true);
	}
}

function saveSettings()
{
	var siteName = site.getCurrentSite();
	
	var appData = new Object();
	
	var bundleId = document.getElementById("bundleId").value;
	var appName = document.getElementById("appName").value;
	var author = document.getElementById("author").value;
	var version = document.getElementById("version").value;
	var iconPath = document.getElementById("iconPath").value;
	var startupScreenPath = document.getElementById("startupScreenPath").value;
	var targetPath = document.getElementById("targetPath").value;
	var targetOSAndroid;
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_ANDROID) )
		targetOSAndroid = _LIST_TARGET_OS_ANDROID.getValue();
	var targetOSiPhone;
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPHONE) )
		targetOSiPhone = _LIST_TARGET_OS_IPHONE.getValue();
	var targetOSiPad;
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPAD) )
		targetOSiPad = _LIST_TARGET_OS_IPAD.getValue();
	
	//do validateion here...
	
	//First check for required values
	if( !validateNonEmpty( appName, dw.loadString("NAF/configureAppSettings/appName/label") ) ||
		!validateNonEmpty( author, dw.loadString("NAF/configureAppSettings/author/label") ) ||
		!validateNonEmpty( version, dw.loadString("NAF/configureAppSettings/version/label") ) ||
		!validateNonEmpty( targetPath, dw.loadString("NAF/configureAppSettings/targetPath/label") ) )
	{
		return false;
	}
	
	if( !validateBundleId(bundleId) )
		return false;
	
	if( !validateAppName( appName ) )
		return false;
	
	if( !validateVersion( version ) )
		return false;
	
    // browseForFile filter isn't working
    var pathLength = iconPath.length;
    if (iconPath != "")
    {
        if (iconPath.indexOf('.png') != (pathLength - 4)
        &&  iconPath.indexOf('.PNG') != (pathLength - 4))
        {
            alert( dw.loadString('NAF/configureAppSettings/imageBadIconType/alert' ) );
            return false;
        }
    }
    
    // browseForFile filter isn't working
    pathLength = startupScreenPath.length;
    if (startupScreenPath != "")
    {
        if (startupScreenPath.indexOf('.png') != (pathLength - 4)
        &&  startupScreenPath.indexOf('.PNG') != (pathLength - 4))
        {
            alert( dw.loadString('NAF/configureAppSettings/imageBadStartupType/alert' ) );
            return false;
        }
    }
	
	//validate the target path has no special characters
	if((targetPath != "") && !validateFolderPath(targetPath, dw.loadString('NAF/configureAppSettings/targetPath/label' )) )
		return false;

   // target path cannot reside within the site itself.
    if (dw.nativeAppFramework.targetInSite(targetPath))
    {
        alert( dw.loadString('NAF/configureAppSettings/targetPathInSite/alert' ) );
        return false;
    }

	appData.bundleId = bundleId;
	appData.name = appName;
	appData.author = author;
	appData.version = version;
	appData.iconPath = iconPath;
	appData.startupScreenPath =startupScreenPath;
	appData.targetPath = targetPath;
	
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_ANDROID) )
		appData.targetOSAndroid = targetOSAndroid;
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPHONE) )
		appData.targetOSiPhone = targetOSiPhone;
	if( dw.nativeAppFramework.getDeviceIsSupported(NativeAppFramework.DEVICE_IPAD) )
		appData.targetOSiPad = targetOSiPad;
		
	dw.nativeAppFramework.saveAppData(siteName, appData);
	
	//for the bundleId, also save the last global one as a hint for the next new app
	NativeAppFramework.setGlobalPathToBundleId(bundleId);
	
	return true;
}

function reloadAVDs()
{
	var defaultTargetId = getAndroidMaxTargetIdForMaxSDKVersion(g_androidMaxSDKVersion);
	var avdList = dw.nativeAppFramework.getAndroidAVDList(defaultTargetId);
	var curSel = _LIST_TARGET_OS_ANDROID.get();
	_LIST_TARGET_OS_ANDROID.setAll(avdList, avdList);

	if( !_LIST_TARGET_OS_ANDROID.pick(curSel) && avdList.length > 0)
		_LIST_TARGET_OS_ANDROID.pickValue(avdList[0]);
}
