/************************************************************************ 
  Copyright 2011-2012, IceJS Team. All rights reserved.
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of Update Freezer Team nor the names of its
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/
 
if (typeof(Util) == "undefined")
	Util = {};

Util.LightUI = 
{
	Eval: function(str)
	{
		_LightUIEval(str);
	},
	
	Alert: function(str)
	{
		_LightUIEval("alert(" + JSON.stringify(str) + ")");
	},
	
	Confirm: function(str)
	{
		var str = "var res = confirm(" + JSON.stringify(str) + "); document.getElementById('#tmplogbuff').innerHTML = res ? 'yes' : 'no';";
		Util.LightUI.Eval(str);
		var res = _LightUIGetElementInnerText('#tmplogbuff');
		return res;
	},
	
	ShowDialog: function (title, entryPoint)
	{
		_LightUIShowDialog(title, entryPoint);
	},
	
	Close: function()
	{
		_LightUIClose();
	},
	
	SetElementInnerText: function(id, text)
	{
		_LightUISetElementInnerText(id, text);
	},
		
	GetElementInnerText: function(id)
	{
		return _LightUIGetElementInnerText(id);
	},
	
	SetDialogSize: function(width, height)
	{
		return _LightUISetDialogSize(width, height);
	},
	
	SetDialogPosition: function(left, top)
	{
		return _LightUISetDialogPosition(left, top);
	},

	/**
     * @returns {x:0, y:0, w:0, h:0} 
     */
	GetDialogSize: function()
	{
		return _LightUIGetDialogSize();
	},
	
    /**
     * @returns [{x:0, y:0, w:0, h:0}, {x:0, y:0, w:0, h:0}]
     */
	GetDisplayInfo: function()
	{
		return _LightUIGetDisplayInfo();
	},
	
	CenterDialog: function()
	{
		return _LightUICenterDialog();
	},
	
	/** ShowWindow contants
		#define SW_HIDE             0
		#define SW_SHOWNORMAL       1
		#define SW_NORMAL           1
		#define SW_SHOWMINIMIZED    2
		#define SW_SHOWMAXIMIZED    3
		#define SW_MAXIMIZE         3
		#define SW_SHOWNOACTIVATE   4
		#define SW_SHOW             5
		#define SW_MINIMIZE         6
		#define SW_SHOWMINNOACTIVE  7
		#define SW_SHOWNA           8
		#define SW_RESTORE          9
		#define SW_SHOWDEFAULT      10
		#define SW_FORCEMINIMIZE    11
		#define SW_MAX              11	
	*/
	
	MinimizeConsoleWindow: function()
	{
		return _LightUIShowConsoleWindow(6);
	},
	
	HideConsoleWindow: function()
	{
		return _LightUIShowConsoleWindow(0);
	},
	
	BringToFront: function()
	{
		return _LightUIBringToFront();
	},
	
	WaitCursor: function()
	{
		return _WaitCursor();
	},
	
	DefaultCursor: function()
	{
		return _DefaultCursor();
	},
	
	CreateDesktopShortcut: function()
	{
		var info = Util.IO.ReadJsonFromFile("info.json");
		if (info)
		{
			var lnkPath = Util.Shell.GetDesktopFolder() + "\\" + info.shortcutText + ".lnk";
			if (!info.shortcutInstalled)
			{
				// do not bug user with this in the future
				info.shortcutInstalled  = true;
				Util.IO.WriteJsonToFile("info.json", info)
				
				var res = "yes";
				var shortcutExists = Util.IO.CheckFileExists(lnkPath);
				if (!shortcutExists)
				{
					res = Util.LightUI.Confirm(SF("UI.InstallShortcutConfirm"));
				}
				if (res == "yes")
				{
				 	Util.IO.DeleteFile(lnkPath);
					var workingDirectory = Util.IO.GetCurrentDirectory();//Util.LightUI.GetInstallationPath(info);
					var icejsPath = Util.IO.CombinePath(workingDirectory, "Bin\\icejs.exe");
					var cmdLineParams = info.entryPoint;
					var iconPath =  Util.IO.CombinePath(workingDirectory, "Resources\\" + info.scenarioName + ".ico");
					Util.IO.CreateShortcut(icejsPath, cmdLineParams, workingDirectory, lnkPath, info.scenarioName, iconPath, 0);
					
					if (shortcutExists)
					{
						// the shortcut was updated
						return false;
					}
					
					return true;
				}
			}
		}	
		return false;
	},
	
	GetInstallationPath: function(info)
	{
		return Util.IO.CombinePath(Util.Shell.GetProgramDataFolder(), "Application Data\\IceJS\\" + info.scenarioName + "\\" + info.version);
	},
	
	SetIcon: function(fileName, index)
	{
		if(typeof(index) == 'undefined' || index < 0)
		{
			index = 0;
		}
		return _LightUISetIcon(fileName, index);
	},
	
	SetTitle: function(t)
	{
		return _LightUISetTitle(t);
	},
	
	SetFocus: function()
	{
		_LightUIEval("document.body.focus();");
	}
}