using System;
using System.Collections.Generic;
using QC.QTMXmlSchema;

namespace QC.XmlToDBPreProcess;

public class ConfigParametricPreProcessor : IPreProcess
{
	public ConversionType ApplyPreProcessType(ref ROOT qtmXml, string[] Arguments)
	{
		string[] valueSeperator = new string[1] { ",," };
		ConversionType result = ConversionType.Session;
		if (Arguments != null)
		{
			Dictionary<string, string> dictionary = PreProcessorUtils.NameValueSeperator(Arguments);
			foreach (KeyValuePair<string, string> item in dictionary)
			{
				if (PreProcessorUtils.PropertyExists(qtmXml.ConfigInfo, item.Key))
				{
					string propertyValue = PreProcessorUtils.GetPropertyValue(qtmXml.ConfigInfo, item.Key);
					if (ValueExistsInValueCollection(valueSeperator, item.Value, propertyValue))
					{
						result = ConversionType.All;
						break;
					}
				}
				else
				{
					if (qtmXml.ConfigInfo.Information == null || qtmXml.ConfigInfo.Information.Length <= 0)
					{
						continue;
					}
					ROOTConfigInfo[] information = qtmXml.ConfigInfo.Information;
					foreach (ROOTConfigInfo rOOTConfigInfo in information)
					{
						if (string.Compare(rOOTConfigInfo.Key, item.Key, ignoreCase: true) == 0 && ValueExistsInValueCollection(valueSeperator, item.Value, rOOTConfigInfo.Value))
						{
							result = ConversionType.All;
							break;
						}
					}
				}
			}
		}
		return result;
	}

	private static bool ValueExistsInValueCollection(string[] valueSeperator, string valueCollection, string qtmXmlValue)
	{
		string[] array = valueCollection.Split(valueSeperator, StringSplitOptions.RemoveEmptyEntries);
		foreach (string strB in array)
		{
			if (!string.IsNullOrEmpty(qtmXmlValue) && string.Compare(qtmXmlValue, strB, ignoreCase: true) == 0)
			{
				return true;
			}
		}
		return false;
	}
}
