/************************************************************************ 
  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.IO = 
{
	/**
	 * Return text contents of file with path equal to 'fileName'
	 */
	ReadFile: function(fileName)
	{
		return _ReadFile(fileName);
	},
	
	/**
	 * Return text contents of file with path equal to 'fileName' assuming it has UTF8 encoding
	 */
	ReadFileUTF8: function(fileName)
	{
		return _ReadFileUTF8(fileName);	
	},
	
	/**
	 * Write text contents to a file with path equal to 'fileName'
	 */
	WriteFile: function(fileName, text)
	{
		return _WriteFile(fileName, text);
	},
	
	/**
	 * Write text contents to a file with path equal to 'fileName' in UTF8 encoding
	 */
	WriteFileUtf8: function(fileName, text)
	{
		return _WriteFileUTF8(fileName, text);
	},

	/**
	 * Returns 'true' when file exists
	 */
	CheckFileExists: function(fileName)
	{
		return _CheckFileExists(fileName);
	},
	
	/**
	 * Delete a file with path specified by 'fileName'
	 */
	DeleteFile: function(fileName)
	{
		return _DeleteFile(fileName);
	},
	
	/**
	 * Copy a file with path specified by 'src' to a file specified by 'dst'
	 */
	CopyFile: function(src, dst)
	{
		var dstParent = Util.IO.GetParentFolder(dst);
		
		if(dstParent&&!Util.IO.CheckFileExists(dstParent))
		{
			Util.IO.CreateDirectory( dstParent );
		}
	
		return _CopyFile(src, dst);
	},
	
	/**
	 * Move a file with path specified by 'src' to a file specified by 'dst'
	 */
	MoveFile: function(src, dst)
	{
		return _MoveFile(src, dst);
	},
	
	/**
	 * Create directory with specified path
	 */
	CreateDirectory: function(path)
	{
		return _EnsureDirectoryExists(path);
	},
	
	/**
	 * Delete directory with specified path
	 */
	DeleteDirectory: function(path)
	{
		return _RemoveDirectoryWithContents(path);
	},
	
	CombinePath: function(path1, path2)
	{
		// Trim trailing separators in path1
		path1 = path1.replace(new RegExp("[\\/\\\\]+$", "g"), "");
		return path1+"\\"+path2;
	},
		
	/**
	 * Return folder name if 'path' represents a folder or file name if 'path' represents file.
	 */
	GetLastPathComponent: function(/**String*/path)
	{
		// Trim trailing separators in path1
		path = path.replace(new RegExp("[\\/\\\\]+$", "g"), "");
		path = path.replace("/", "\\");
		var parts = path.split("\\");
		
		if( parts && parts.length>0 )
		{
			return parts[parts.length-1];
		}
		return "";
	},
	
	/**
	 * For path like c:\a/b/c/d return c:\a/b/c (without trailing separator)
	 * for 'd' being either folder or file.
	 */
	GetParentFolder: function(/**String*/path)
	{
		path = path.replace(new RegExp("[\\/\\\\]+$", "g"), "");
		
		for(var li=path.length-1;li--;li>=0)
		{
			var c = path.charAt(li);
			if(c=='\\'||c=='/')
			{
				path = path.substr(0, li);
				path = path.replace(new RegExp("[\\/\\\\]+$", "g"), "");
				return path;
			}
		}
		return "";
	},
	
	ReadKey: function()
	{
		return _ReadKey();
	},
	
	GetCurrentDirectory: function()
	{
		return _GetCurrentDirectory();
	},
	
	SetCurrentDirectory: function(newDir)
	{
		return _SetCurrentDirectory(newDir);
	},
	
	GetFileName: function(newDir)
	{
		return _GetFileName(newDir);
	},
	
	GetFilePath: function(newDir)
	{
		return _GetFilePath(newDir);
	},
	
	ReadJsonFromFile: function(fileName)
	{
		var text = Util.IO.ReadFile(fileName);
		var obj = null;
		try
		{
			obj = JSON.parse(text);
		}
		catch(e)
		{
		
		}
		return obj;
	},
	
	WriteJsonToFile: function(fileName, obj)
	{
		var text = JSON.stringify(
						obj,
						function(key, value){
							if(typeof key==="string" && key.indexOf("parent")==0 || key=="rule") return undefined;
							return value;
						},
						"\t"
					);
		this.WriteFile(fileName, text);
	},
	
	GetFilesInFolder: function(path)
	{
		return _GetFilesInFolder(path);
	},
	
	CreateShortcut: function(targetFile, arguments, workingDir, shortcutPath, shortcutComment, iconLocation, iconIndex)
	{
		return _CreateShortcut(targetFile, arguments, workingDir, shortcutPath, shortcutComment, iconLocation, iconIndex);
	},
	
	ShiftBytes: function(str, shift)
	{
		return _ShiftBytes(str, shift);
	},
	
	ShiftFileBytes: function(f1, f2, shift)
	{
		return _ShiftFileBytes(f1, f2, shift);
	}
}
