using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Xml.Linq;
using System.Xml.Serialization;
using QC.QsprShared.Config;
using QC.QsprTestObjects.Registry;

namespace QC.QsprTestObjects.Tests.Base;

[Serializable]
public class SerializeComponents
{
	private QTMRegistry Registry = new QTMRegistry();

	private RootTest RootTest = new RootTest();

	private TestTreeInfo TTInfo = new TestTreeInfo();

	private PluginConfig pluginInfo = new PluginConfig();

	private WorkspaceConfig wConfig = new WorkspaceConfig();

	private List<DockConfig> debugWinColl = new List<DockConfig>();

	public byte[] xmlCRC;

	public TestTreeInfo theTestTreeInfo
	{
		get
		{
			return TTInfo;
		}
		set
		{
			TTInfo = value;
		}
	}

	public RootTest theRootTest
	{
		get
		{
			return RootTest;
		}
		set
		{
			RootTest = value;
		}
	}

	public PluginConfig thePlugInConfig
	{
		get
		{
			return pluginInfo;
		}
		set
		{
			pluginInfo = value;
		}
	}

	public WorkspaceConfig WConfig
	{
		get
		{
			return wConfig;
		}
		set
		{
			wConfig = value;
		}
	}

	public List<DockConfig> DebugConfig
	{
		get
		{
			return debugWinColl;
		}
		set
		{
			debugWinColl = value;
		}
	}

	public QTMRegistry theRegistry
	{
		get
		{
			return Registry;
		}
		set
		{
			Registry = value;
		}
	}

	public bool ValidMD5(string fileName)
	{
		using (StreamReader streamReader = new StreamReader(fileName))
		{
			string fullText = streamReader.ReadToEnd();
			fullText = CleanXML(fullText);
			byte[] mD = GetMD5(fullText);
			string text = Convert.ToBase64String(xmlCRC);
			string text2 = Convert.ToBase64String(mD);
			if (text != text2)
			{
				return false;
			}
		}
		return true;
	}

	public void GenerateMD5()
	{
		using MemoryStream memoryStream = new MemoryStream();
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerializeComponents));
		xmlSerializer.Serialize(memoryStream, this);
		memoryStream.Seek(0L, SeekOrigin.Begin);
		using StreamReader streamReader = new StreamReader(memoryStream);
		string fullText = streamReader.ReadToEnd();
		fullText = CleanXML(fullText);
		byte[] mD = GetMD5(fullText);
		xmlCRC = mD;
	}

	private static string CleanXML(string fullText)
	{
		fullText = fullText.Remove(0, fullText.IndexOf("<theTestTreeInfo>"));
		fullText = RemoveXMLArea(fullText, "DebugConfig");
		fullText = RemoveXMLArea(fullText, "xmlCRC");
		fullText = RemoveXMLArea(fullText, "TestTreeVersion");
		fullText = RemoveXMLArea(fullText, "SourceApplication");
		return fullText;
	}

	public static void AddMD5(string filename)
	{
		SerializeComponents serializeComponents = new SerializeComponents();
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerializeComponents));
		using (StreamReader textReader = new StreamReader(filename))
		{
			serializeComponents = (SerializeComponents)xmlSerializer.Deserialize(textReader);
		}
		using (StreamReader streamReader = new StreamReader(filename))
		{
			string text = streamReader.ReadToEnd();
			XDocument xDocument = XDocument.Parse(text);
			(from e in xDocument.Descendants()
				where e.IsEmpty || string.IsNullOrWhiteSpace(e.Value)
				select e).Remove();
			text = CleanXML(text);
			Convert.ToBase64String(serializeComponents.xmlCRC = GetMD5(text));
		}
		using (new MemoryStream())
		{
		}
		using StreamWriter textWriter = new StreamWriter(filename, append: false);
		xmlSerializer.Serialize(textWriter, serializeComponents);
	}

	private static string RemoveXMLArea(string fullText, string areaName)
	{
		int num = fullText.IndexOf("<" + areaName + ">");
		if (num == -1)
		{
			num = fullText.IndexOf("<" + areaName);
		}
		int num2 = fullText.IndexOf("</" + areaName + ">\r\n");
		if (num2 == -1)
		{
			num2 = (num2 = fullText.IndexOf("</" + areaName + ">"));
		}
		if (num == -1 || num2 == -1)
		{
			return fullText;
		}
		int count = num2 - num + 5 + areaName.Length;
		return fullText.Remove(num, count);
	}

	private static byte[] GetBytes(string str)
	{
		byte[] array = new byte[str.Length * 2];
		Buffer.BlockCopy(str.ToCharArray(), 0, array, 0, array.Length);
		return array;
	}

	public static byte[] GetMD5(string text)
	{
		using MD5 mD = new MD5CryptoServiceProvider();
		byte[] bytes = GetBytes(text);
		return mD.ComputeHash(bytes);
	}
}
