using System;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace ASPNET.StarterKit.TimeTracker.BusinessLogicLayer
{
	//*********************************************************************
	//
	// TTSecurity Class
	//
	// The TimeTrackerSecurity class encapsulates two helper methods that enable
	// developers to easily check the role status of the current browser client.
	//
	//*********************************************************************

	public class TTSecurity
	{																																
		//*********************************************************************
		//
		// TTSecurity.IsInRole() Method
		//
		// The IsInRole method enables developers to easily check the role
		// status of the current browser client.
		//
		//*********************************************************************

		public static bool IsInRole(String role) 
		{
			return HttpContext.Current.User.IsInRole(role);
		}

		//*********************************************************************
		//
		// TTSecurity.Encrypt() Method
		//
		// The Encrypt method encrypts a clean string into hashed string
		//
		//*********************************************************************

		public static string Encrypt(string cleanString)
		{
			Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
			Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
			
			return BitConverter.ToString(hashedBytes);
		}

		public static int GetUserID()
		{
			return ((CustomPrincipal)HttpContext.Current.User).UserID;
		}

		public static string GetUserRole()
		{
			return ((CustomPrincipal)HttpContext.Current.User).UserRole;
		}

		public static string GetName()
		{
			return ((CustomPrincipal)HttpContext.Current.User).Name;
		}

				
		//*********************************************************************
		//
		// <summary>
		// Validates the input text using a Regular Expression and replaces any input expression
		// characters with empty string.Removes any characters not in [a-zA-Z0-9_]. 
		// <summary>
		// <remarks>
		// For a good reference on Regular Expressions, please see
		//	 - http://regexlib.com
		//	 - http://py-howto.sourceforge.net/regex/regex.html
		// </remarks>
		// <param name="inputText">The text to validate.</param>
		// <returns>Sanitized string</returns>
		//
		//*********************************************************************

		public static string CleanStringRegex(string inputText)
		{
			RegexOptions options = RegexOptions.IgnoreCase;
			return ReplaceRegex(inputText,@"[^\\.!?""',\-\w\s@]",options);
		}

		//*********************************************************************
		//
		// <summary>
		// Removes designated characters from an input string input text using a Regular Expression.
		// </summary>
		// <remarks>
		// For a good reference on Regular Expressions, please see
		//	 - http://regexlib.com
		//	 - http://py-howto.sourceforge.net/regex/regex.html
		// </remarks>
		// <param name="inputText">The text to clean.</param>
		// <param name="regularExpression">The regular expression</param>
		// <returns>Sanitized string.</returns>
		//
		//*********************************************************************

		private static string ReplaceRegex(string inputText, string regularExpression, RegexOptions options)
		{
			Regex regex = new Regex(regularExpression,options);
			return regex.Replace(inputText,"");
		}
	}
}
