using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace C4IT.LIAM
{
	public class cLIAMHelper
	{
		public static void AddXmlElement(XmlNode xNode, string Name, string Text)
		{
			var xItem = xNode.OwnerDocument.CreateElement(Name);
			xNode.AppendChild(xItem);
			if (!string.IsNullOrEmpty(Text))
			{
				var xText = xNode.OwnerDocument.CreateTextNode(Text);
				xItem.AppendChild(xText);
			}
		}

		public static string getStringFromObject(object o, string Default = "")
		{
			try
			{
				if (o == null)
					return Default;
				if (o is DBNull)
					return Default;
				return o.ToString();
			}
			catch { }
			return Default;
		}

		public static int getIntFromObject(object o, int Default = 0)
		{
			try
			{
				if (o == null)
					return Default;
				if (o is DBNull)
					return Default;
				if (o is int @int)
					return @int;
				if (o is long int1)
					return (int)int1;
				if (int.TryParse(o.ToString(), out var r))
					return r;
			}
			catch { }
			return Default;
		}

		public static Guid getGuidFromObject(object o)
		{
			try
			{
				if (o == null)
					return Guid.Empty;
				if (o is DBNull)
					return Guid.Empty;
				if (o is Guid G)
					return G;
				if (Guid.TryParse(o.ToString(), out var r))
					return r;
			}
			catch { }
			return Guid.Empty;
		}

		public static string getUidItem(ref string UID)
		{
			try
			{
				var p = UID.IndexOf('|');
				if (p >= 0)
				{
					var Item = UID.Substring(0, p);
					UID = UID.Remove(0, p + 1);
					UID = UID.TrimStart();
					return Item.Trim();
				}


			}
			catch { };

			var h2 = UID;
			UID = "";
			return h2;
		}
	}

	public abstract class aLIAMM42Base {

		}
}
