using System;
using System.Windows.Forms; // for RichTextBox
using System.Reflection;	// for Assembly. ...

namespace MyNamespace
{
	/// <summary>
	///		Global services for several of Harry's programs
	///		Developed in TURBO Pascal 5.5 and 6.0, converted to Visual Basic 6
	///		and now in Visual C# .Net
	///
	/// ---------------- *** LEGAL NOTICE: *** ------------------
	///
	/// This program is free software; you can redistribute it and/or modify
	/// it under the terms of the GNU General Public License (GPL) as published
	/// by the Free Software Foundation. cf. the file COPYING.txt.
	///
	/// ------------ *** end of legal notice *** ------------------
	/// </summary>

	public class Global
	{
		public Global()	// constructor logic
		{
			command = "";
			helpUp = 0;
			myURL  = "hjsmithh@sbcglobal.net  http://www.geocities.com/hjsmithh/";
			programURL = "http://www.geocities.com/hjsmithh/download.html";
			runFormUp = false;
			GetAssemblyAttributes();
		}

		public static bool IsInt32(string sInt32)	// is input string a good 32-bit signed integer?
		{
			int iInt32;
			try
			{
				iInt32 = System.Int32.Parse(sInt32);
				return true;
			}
			catch	// (FormatException ex) and (OverflowException ex)
			{
				return false;
			}
		}

		public static bool IsDouble(string sInt32)	// is input string a good double?
		{
			double dDouble;
			try
			{
				dDouble = System.Double.Parse(sInt32);
				return true;
			}
			catch // (FormatException ex)
			{
				return false;
			}
		}

		public static string DeleteExtraDelimiters(string st)
		// ' ', '\"', and ',' are considered delimiters, multiple delimiters considered as one delimiter.
		{
			try
			{
				string line = st;					// an exception is raised if st == "" or all delimiters
				line = line.Replace(',', ' ');
				line = line.Replace('\"', ' ');
				line = line.Trim(' ');						// remove leading and trailing blanks
				for (int i = 1; i < line.Length - 2; i++)	// change "  " to " "
				{
					if (line[i] == ' ' && line[i+1] == ' ')
					{
						line = line.Substring(0, i) + line.Substring(i+1);
						i--;
					}
				}
				return line;
			}
			catch											// easiest way to handle nothing
			{
				return "";
			}
		}

		public static void GetAssemblyAttributes() // and version
		{
			system = Environment.GetEnvironmentVariable("SYSTEM");
			if (system == null)
				system = "";
			else
				system += Environment.NewLine;
			system += Environment.GetEnvironmentVariable("COMPUTERNAME");
			system += " by ";
			system += Environment.GetEnvironmentVariable("USERNAME");
			if (system == null || system == "")
				system = "<Computer system name not set>";
			foreach (object attr in Assembly.GetExecutingAssembly().GetCustomAttributes(false))
			{
				AssemblyTitleAttribute aTitle=attr as AssemblyTitleAttribute;
				if (aTitle != null) title = aTitle.Title;

				AssemblyDescriptionAttribute aDescription=attr as AssemblyDescriptionAttribute;
				if (aDescription != null) description = aDescription.Description;

				AssemblyConfigurationAttribute aConfiguration=attr as AssemblyConfigurationAttribute;
				if (aConfiguration != null) configuration = aConfiguration.Configuration;

				AssemblyCompanyAttribute aCompany=attr as AssemblyCompanyAttribute;
				if (aCompany != null) company = aCompany.Company;

				AssemblyProductAttribute aProduct=attr as AssemblyProductAttribute;
				if (aProduct != null) product = aProduct.Product;

				AssemblyCopyrightAttribute aCopyright=attr as AssemblyCopyrightAttribute;
				if (aCopyright != null) copyright = aCopyright.Copyright;

				AssemblyTrademarkAttribute aTrademark=attr as AssemblyTrademarkAttribute;
				if (aTrademark != null) trademark = aTrademark.Trademark;
			}
			Version aVersion = Assembly.GetExecutingAssembly().GetName().Version;
			int versionMajor = aVersion.Major;
			int versionMinor = aVersion.Minor;
			int versionBuild = aVersion.Build;			// the number of days since January 1, 2000 local time
			int versionRevision = aVersion.Revision;	// the number of seconds since midnight local time, divided by 2
			version = "C# Version " + aVersion.ToString();
			name1	= product;
			name2	= description;
			author  = copyright;
			address = trademark;
			string[] fields;
			fields = title.Split(' ');		// convert the string to an array each, number is separated by a space
			pName  = fields[0].ToString();
		} // GetAssemblyAttributes

		public static void Prn(string st)	//Print string to TextOut window
		{
			runTextOut.AppendText(st);
		}

		public static void PLn(string st)	//Print number or string ... and new line
		{
			Prn(st + "\n");
		}

		public static string system;			// EnvironmentVariable("SYSTEM")
		public static string title;				// this is an example of retrieving AssemblyInfo
		public static string description;		// see GetAssemblyAttributes() above
		public static string configuration;
		public static string company;
		public static string product;
		public static string copyright;
		public static string trademark;

		public static string name1;				// = product
		public static string name2;				// = description
		public static string version;			// = "C# Version x.x.xxxx.xxxxx"
		public static string author;			// = copyright
		public static string address;			// = trademark

		public static string command;			// commands from command line arguments
		public static int helpUp;				// number of Help form up and running
		public static string myURL;				// my e-mail address and homepage URL
		public static string programURL;		// URL of related web page
		public static RunForm objRunForm;		// the RunForm object for function calls
		public static string pName;				// from title
		public static bool parmsDone;			// true if command line parameters have been processed
		public static bool runFormUp;			// the Run Form is up and running
		public static RichTextBox runTextIn;	// main input window on Run form
		public static RichTextBox runTextOut;	// main output window on Run form
	}
}
