using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using QC.QsprShared.GlobalData;

namespace QC.QsprShared.Trace;

public class TraceMessage
{
	public static string fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Path.Combine(QTMData.FeatureConfigInfo.TraceMsgDirectory, "TraceLog.xml"));

	public static string suppressedMsgfileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Path.Combine(QTMData.FeatureConfigInfo.TraceMsgDirectory, "TraceLog.xml"));

	private static XmlDocument doc = null;

	private static XmlElement root;

	private static Mutex mutex = new Mutex();

	private static XmlDocument suppressedMsgDoc = null;

	private static XmlElement suppressedMsgRoot;

	private static string qsprIdentifier = Guid.NewGuid().ToString("N");

	public static bool EnableTracing { get; set; }

	public static void Write(string source, string strMessage, TraceMessageType type)
	{
		mutex.WaitOne();
		try
		{
			if (EnableTracing)
			{
				if (doc != null)
				{
					XmlElement xmlElement = doc.CreateElement("M");
					XmlElement xmlElement2 = doc.CreateElement("T");
					xmlElement2.InnerText = DateTime.Now.ToLongTimeString();
					XmlElement xmlElement3 = doc.CreateElement("Msg");
					xmlElement3.InnerText = strMessage;
					XmlElement xmlElement4 = doc.CreateElement("C");
					xmlElement4.InnerText = source;
					XmlElement xmlElement5 = doc.CreateElement("P");
					xmlElement5.InnerText = type.ToString();
					xmlElement.AppendChild(xmlElement2);
					xmlElement.AppendChild(xmlElement4);
					xmlElement.AppendChild(xmlElement3);
					xmlElement.AppendChild(xmlElement5);
					root.AppendChild(xmlElement);
				}
				else
				{
					StartXMLLogging();
				}
			}
		}
		catch
		{
		}
		mutex.ReleaseMutex();
	}

	public static void WriteAlways(string source, string strMessage, TraceMessageType type)
	{
		mutex.WaitOne();
		try
		{
			if (EnableTracing)
			{
				if (doc != null)
				{
					XmlElement xmlElement = doc.CreateElement("M");
					XmlElement xmlElement2 = doc.CreateElement("T");
					xmlElement2.InnerText = DateTime.Now.ToLongTimeString();
					XmlElement xmlElement3 = doc.CreateElement("Msg");
					xmlElement3.InnerText = strMessage;
					XmlElement xmlElement4 = doc.CreateElement("C");
					xmlElement4.InnerText = source;
					XmlElement xmlElement5 = doc.CreateElement("P");
					xmlElement5.InnerText = type.ToString();
					xmlElement.AppendChild(xmlElement2);
					xmlElement.AppendChild(xmlElement4);
					xmlElement.AppendChild(xmlElement3);
					xmlElement.AppendChild(xmlElement5);
					root.AppendChild(xmlElement);
				}
				else
				{
					StartXMLLogging();
				}
			}
		}
		catch
		{
		}
		mutex.ReleaseMutex();
	}

	public static void WriteSuppressedMsg(string source, string strMessage, TraceMessageType type)
	{
		try
		{
			if (suppressedMsgDoc != null)
			{
				XmlElement xmlElement = suppressedMsgDoc.CreateElement("M");
				XmlElement xmlElement2 = suppressedMsgDoc.CreateElement("T");
				xmlElement2.InnerText = DateTime.Now.ToLongTimeString();
				XmlElement xmlElement3 = suppressedMsgDoc.CreateElement("Msg");
				xmlElement3.InnerText = strMessage;
				XmlElement xmlElement4 = suppressedMsgDoc.CreateElement("C");
				xmlElement4.InnerText = source;
				XmlElement xmlElement5 = suppressedMsgDoc.CreateElement("P");
				xmlElement5.InnerText = type.ToString();
				xmlElement.AppendChild(xmlElement2);
				xmlElement.AppendChild(xmlElement4);
				xmlElement.AppendChild(xmlElement3);
				xmlElement.AppendChild(xmlElement5);
				suppressedMsgRoot.AppendChild(xmlElement);
			}
			else
			{
				StartSuppressedMsgLogging();
			}
		}
		catch
		{
		}
	}

	public static void StartXMLLogging()
	{
		doc = new XmlDocument();
		root = doc.CreateElement("root");
		doc.AppendChild(root);
	}

	public static void EndXMLLogging()
	{
		try
		{
			if (EnableTracing)
			{
				if (doc != null)
				{
					doc.Save(GetFilename("TraceLog "));
				}
				StartXMLLogging();
			}
		}
		catch
		{
		}
	}

	public static void EndXMLLoggingTemp()
	{
		try
		{
			if (doc != null)
			{
				doc.Save(GetFilenamePerQSPRRun("TraceLog "));
				doc = new XmlDocument();
			}
		}
		catch
		{
		}
	}

	public static void StartSuppressedMsgLogging()
	{
		suppressedMsgDoc = new XmlDocument();
		suppressedMsgRoot = suppressedMsgDoc.CreateElement("root");
		suppressedMsgDoc.AppendChild(suppressedMsgRoot);
	}

	public static void EndSuppressedMsgLogging()
	{
		try
		{
			if (suppressedMsgDoc != null)
			{
				suppressedMsgDoc.Save(GetFilename("SuppressedMsgLog "));
			}
			StartSuppressedMsgLogging();
		}
		catch
		{
		}
	}

	private static string GetFilenamePerQSPRRun(string fileName)
	{
		string text = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + " " + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + "-" + DateTime.Now.Millisecond;
		if (!Directory.Exists(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), QTMData.FeatureConfigInfo.TraceMsgDirectory)))
		{
			Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), QTMData.FeatureConfigInfo.TraceMsgDirectory));
		}
		return Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Path.Combine(QTMData.FeatureConfigInfo.TraceMsgDirectory, fileName + qsprIdentifier + text + ".xml"));
	}

	private static string GetFilename(string fileName)
	{
		string text = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + " " + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + "-" + DateTime.Now.Millisecond;
		if (!Directory.Exists(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), QTMData.FeatureConfigInfo.TraceMsgDirectory)))
		{
			Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), QTMData.FeatureConfigInfo.TraceMsgDirectory));
		}
		return Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Path.Combine(QTMData.FeatureConfigInfo.TraceMsgDirectory, Process.GetCurrentProcess().Id + "_" + fileName + text + ".xml"));
	}
}
