using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using QC.QDM.Libraries.DBAccessLibrary;

namespace QTMDataFormat;

public class Utility
{
	public static string GetListAsDelimitedString(List<string> sourceList, string valueDelimiter)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (valueDelimiter.Length > 0)
		{
			foreach (string source in sourceList)
			{
				stringBuilder.Append(source);
				stringBuilder.Append(valueDelimiter);
			}
			if (stringBuilder.Length > 0)
			{
				stringBuilder.Remove(stringBuilder.Length - valueDelimiter.Length, valueDelimiter.Length);
			}
		}
		return stringBuilder.ToString();
	}

	public static string GetArrayAsDelimitedString(string[] sourceList, string valueDelimiter)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (valueDelimiter.Length > 0)
		{
			foreach (string value in sourceList)
			{
				stringBuilder.Append(value);
				stringBuilder.Append(valueDelimiter);
			}
			if (stringBuilder.Length > 0)
			{
				stringBuilder.Remove(stringBuilder.Length - valueDelimiter.Length, valueDelimiter.Length);
			}
		}
		return stringBuilder.ToString();
	}

	public static List<string> GetListFromDelimitedText(string delimitedText, string valueDelimiter, out string errorMessage)
	{
		errorMessage = "";
		List<string> list = new List<string>();
		try
		{
			if (valueDelimiter.Length > 0 && delimitedText.Length > 0)
			{
				string[] separator = new string[1] { valueDelimiter };
				string[] array = delimitedText.Split(separator, StringSplitOptions.RemoveEmptyEntries);
				string[] array2 = array;
				foreach (string item in array2)
				{
					list.Add(item);
				}
			}
		}
		catch (Exception ex)
		{
			errorMessage = "Error creating the column list - " + ex.Message;
		}
		return list;
	}

	public static bool IsQmtDatabase(string storedProcedureName)
	{
		bool result = false;
		try
		{
			if (DataAccessManager.DataAccessor.ServerName != null && DataAccessManager.DataAccessor.ServerName.Length > 0 && DataAccessManager.DataAccessor.DatabaseName.Length > 0)
			{
				DataSet dataSet = DataAccessManager.DataAccessor.RunStoredProcedure(storedProcedureName);
				result = false;
				if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Columns.Contains("Lot"))
				{
					result = true;
				}
			}
		}
		catch
		{
		}
		return result;
	}

	public static bool IsDataSetValid(ref DataSet dataSetResult)
	{
		if (dataSetResult != null)
		{
			if (dataSetResult.Tables.Count > 0)
			{
				if (dataSetResult.Tables[0].Rows.Count == 0)
				{
					dataSetResult = null;
					return false;
				}
				return true;
			}
			dataSetResult = null;
			return false;
		}
		return false;
	}

	public static bool IsTypeNumeric(Type type)
	{
		bool result = false;
		if (type != null && (type == Type.GetType("System.Byte") || type == Type.GetType("System.SByte") || type == Type.GetType("System.Int16") || type == Type.GetType("System.Int32") || type == Type.GetType("System.Int64") || type == Type.GetType("System.UInt16") || type == Type.GetType("System.UInt32") || type == Type.GetType("System.UInt64") || type == Type.GetType("System.Single") || type == Type.GetType("System.Double") || type == Type.GetType("System.Decimal")))
		{
			result = true;
		}
		return result;
	}

	public static bool IsNumericColumn(DataColumn col)
	{
		bool result = true;
		if (col != null)
		{
			result = IsTypeNumeric(col.DataType);
		}
		return result;
	}

	public static string GetProperXML(string xmlString)
	{
		try
		{
			if (xmlString != null && xmlString.Trim() != "")
			{
				xmlString = xmlString.Replace("&amp;", "&");
				xmlString = xmlString.Replace("&lt;", "<");
				xmlString = xmlString.Replace("&gt;", ">");
			}
		}
		catch
		{
		}
		return xmlString;
	}

	public static string GetCommaDelimitedValues(StringCollection collection)
	{
		StringBuilder stringBuilder = new StringBuilder();
		bool flag = true;
		if (collection != null)
		{
			StringEnumerator enumerator = collection.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.Current;
					if (flag)
					{
						flag = false;
					}
					else
					{
						stringBuilder.Append(", ");
					}
					stringBuilder.Append(current);
				}
			}
			finally
			{
				if (enumerator is IDisposable disposable)
				{
					disposable.Dispose();
				}
			}
		}
		return stringBuilder.ToString();
	}

	public static string AppendDateTimeToFileName()
	{
		StringBuilder stringBuilder = new StringBuilder();
		try
		{
			stringBuilder.Append(DateTime.Now.Day.ToString("00"));
			stringBuilder.Append(DateTime.Now.Month.ToString("00"));
			stringBuilder.Append(DateTime.Now.Year.ToString("0000"));
			stringBuilder.Append("_");
			stringBuilder.Append(DateTime.Now.Hour.ToString("00"));
			stringBuilder.Append(DateTime.Now.Minute.ToString("00"));
			stringBuilder.Append(DateTime.Now.Second.ToString("00"));
		}
		catch
		{
		}
		return stringBuilder.ToString();
	}
}
