using System;
using System.IO;
using System.Reflection;

namespace Medivators.Common
{
	/// <summary>
	/// Summary description for Converters.
	/// </summary>
	public class Utilities
	{
		public Utilities()
		{
		}

		#region comm device-specific utilities
		
		/// <summary>
		/// FUNCTION reads values in a string
		/// and casts each Char into a new byte array
		/// example: "Hello" translates to {72,101,108,108,111}
		/// </summary>
		/// <param name="Value"></param>
		/// <returns></returns>
		public static Byte[] StringToByteArray( string Value )
		{
			try
			{
				if ( ( Value == null ) || ( Value.Length == 0 ) )
				{
					Byte[] rtnVal = {0};
					return rtnVal;
				}
				else
				{
					Byte[] rtnval = new Byte[Value.Length];
					int idx;
					for ( idx = 0; idx < Value.Length; idx++ )
						rtnval[idx] = Convert.ToByte(Value[idx]);
					return rtnval;
				}
			}
			catch ( Exception CastEx )
			{
				throw CastEx;
			}
		}

		/// <summary>
		/// FUNCTION converts an array of Bytes to a string 
		/// that is formatted for printing. 
		/// </summary>
		/// <param name="Value"></param>
		/// <returns></returns>
		public static string ByteArrayToString(Byte[] Value )
		{
			return ByteArrayToString(Value, false);
		}
	
		/// <summary>
		/// FUNCTION converts an array of Bytes to a string 
		/// that is formatted for printing. When MakePrintable is TRUE, 
		/// Unprintable characters are replaced with the ASCII value inside of angle brackets
		/// example: CR would appear <13> 
		/// </summary>
		/// <param name="Value"></param>
		/// <param name="MakePrintable"></param>
		/// <returns></returns>
		public static string ByteArrayToString(Byte[] Value, bool MakePrintable )
		{
			string			rtnVal		= string.Empty;
			const string	printable	= "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~`@#$%^&*()_-+={[}]|:;'<,>.?/ " + "\\";
			try
			{
				if ( ( null != Value ) && ( Value.Length > 0 ) )
				{
					foreach ( Byte wrkByte in Value )
					{
						if (printable.IndexOf((char)wrkByte) < 0  && MakePrintable)
							rtnVal += String.Format("<{0}>", wrkByte);
						else
							rtnVal += Convert.ToChar(wrkByte);
					}
				}
				return rtnVal;
			}
			catch ( Exception CastEx )
			{
				throw CastEx;
			}
		}


		public static bool[] ByteToBits (Byte Value )
		{
			bool[] rtnVal = new bool[7];
			rtnVal[0] = (Value & 128) == 128;
			rtnVal[1] = (Value & 64) == 64;
			rtnVal[2] = (Value & 32) == 32;
			rtnVal[3] = (Value & 16) == 16;
			rtnVal[4] = (Value & 8) == 8;
			rtnVal[5] = (Value & 4) == 4;
			rtnVal[6] = (Value & 2) == 2;
			rtnVal[7] = (Value & 1) == 1;
			return rtnVal;
		}
		public static byte BitsToByte ( bool[] bits )
		{
			byte rtnVal = 0;
			if ( bits[0] ) rtnVal += 128;
			if ( bits[1] ) rtnVal += 64;
			if ( bits[2] ) rtnVal += 32;
			if ( bits[3] ) rtnVal += 16;
			if ( bits[4] ) rtnVal += 8;
			if ( bits[5] ) rtnVal += 4;
			if ( bits[0] ) rtnVal += 2;
			if ( bits[7] ) rtnVal += 1;
			return rtnVal;
		}

		#endregion

		#region path and folder utilities

		public static string PathSlash(string Path)
		{
			if (Path.EndsWith("\\"))
				return Path;
			else
				return Path + "\\";
		}
		public static string GetAppName()
		{
			return Assembly.GetEntryAssembly().GetName().Name;
		}
		public static string GetAppFolder()
		{
			return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
		}

		#endregion
	}
}
