using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace QC.QSPRTesterConfigSchema;

public class QSPRTesterConfigSchema
{
	private ArrayOfSerialConfig currentConfig = new ArrayOfSerialConfig();

	public ArrayOfSerialConfig CurrentConfig
	{
		get
		{
			return currentConfig;
		}
		set
		{
			currentConfig = value;
		}
	}

	public SerialConfig LoadConfigFromFile(string fileName)
	{
		currentConfig = LoadConfig(fileName);
		return (SerialConfig)currentConfig.SerialConfig[0];
	}

	private ArrayOfSerialConfig LoadConfig(string fileName)
	{
		FileStream fileStream = null;
		ArrayOfSerialConfig arrayOfSerialConfig = new ArrayOfSerialConfig();
		try
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(ArrayOfSerialConfig));
			fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
			if (fileStream != null)
			{
				arrayOfSerialConfig = (ArrayOfSerialConfig)xmlSerializer.Deserialize(fileStream);
			}
			arrayOfSerialConfig.AbsoluteName = fileName;
			arrayOfSerialConfig.Name = Path.GetFileName(fileName);
			arrayOfSerialConfig.SaveFlag = false;
			return arrayOfSerialConfig;
		}
		catch
		{
			throw;
		}
		finally
		{
			fileStream?.Close();
		}
	}

	public bool SaveConfig(SerialConfig configInfo, string fileName)
	{
		try
		{
			ArrayList arrayList = new ArrayList();
			arrayList.Add(configInfo);
			currentConfig.SerialConfig = arrayList;
			currentConfig.AbsoluteName = fileName;
			currentConfig.Name = Path.GetFileName(fileName);
			WriteDataToXml(CurrentConfig);
			return true;
		}
		catch (Exception)
		{
			throw;
		}
	}

	private bool WriteDataToXml(ArrayOfSerialConfig aSerialConfig)
	{
		TextWriter textWriter = null;
		try
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(ArrayOfSerialConfig));
			if (aSerialConfig.AbsoluteName.Trim() != "")
			{
				textWriter = new StreamWriter(aSerialConfig.AbsoluteName);
				xmlSerializer.Serialize(textWriter, aSerialConfig);
			}
			if (textWriter != null)
			{
				textWriter.Close();
				textWriter = null;
			}
			return true;
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally
		{
			textWriter?.Close();
		}
	}

	private bool ValidateString(string InputText, string regExp)
	{
		Regex regex = new Regex(regExp);
		MatchCollection matchCollection = regex.Matches(InputText);
		for (int i = 0; i < matchCollection.Count; i++)
		{
			Match match = matchCollection[i];
			if (match.Value == InputText)
			{
				return true;
			}
		}
		return false;
	}

	public bool ValidateConfigEntry(string inputText, Config testerConfigInfo, out string errorMsg)
	{
		errorMsg = "";
		if (testerConfigInfo.RegEx.Trim() != "" && !ValidateString(inputText, testerConfigInfo.RegEx))
		{
			if (testerConfigInfo.ErrorMsg != string.Empty)
			{
				errorMsg = "RegEx: " + testerConfigInfo.RegEx + " " + testerConfigInfo.ErrorMsg;
			}
			else
			{
				errorMsg = "Value does not match regular expression. RegEx: " + testerConfigInfo.RegEx + " " + testerConfigInfo.ErrorMsg;
			}
			return false;
		}
		return true;
	}
}
