using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml;

namespace XmlForDB;

public class ParamArrayRID
{
	protected static Dictionary<ParamArrayInfo, string> hashParamArrayInfo = new Dictionary<ParamArrayInfo, string>(new ParamArrayInfoComparer());

	protected static Dictionary<ParamArrayInfo, string> NewParamArrayRIDLookup = new Dictionary<ParamArrayInfo, string>(new ParamArrayInfoComparer());

	public void AddToNewLookUpList(string paramRID, string arrayName, string paramIndex, string columnIndex, string lowerLimit, string upperLimit)
	{
		ParamArrayInfo paramArrayInfo = new ParamArrayInfo();
		paramArrayInfo.LL = lowerLimit.Trim();
		paramArrayInfo.UL = upperLimit.Trim();
		paramArrayInfo.ParamRID = paramRID.Trim();
		paramArrayInfo.ArrayName = arrayName.Trim();
		paramArrayInfo.ParamIndex = paramIndex;
		paramArrayInfo.ColumnIndex = columnIndex;
		if (!NewParamArrayRIDLookup.ContainsKey(paramArrayInfo))
		{
			NewParamArrayRIDLookup.Add(paramArrayInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string paramRID, string arrayName, string paramIndex, string columnIndex, string lowerValue, string upperValue)
	{
		ParamArrayInfo paramArrayInfo = new ParamArrayInfo();
		paramArrayInfo.ArrayName = arrayName.Trim();
		paramArrayInfo.LL = lowerValue.Trim();
		paramArrayInfo.UL = upperValue.Trim();
		paramArrayInfo.ParamRID = paramRID.Trim();
		paramArrayInfo.ParamIndex = paramIndex;
		paramArrayInfo.ColumnIndex = columnIndex;
		string value = string.Empty;
		if (hashParamArrayInfo.TryGetValue(paramArrayInfo, out value))
		{
			return value;
		}
		return string.Empty;
	}

	public bool CheckIfKeyExists(ParamArrayInfo paramArray)
	{
		return hashParamArrayInfo.ContainsKey(paramArray);
	}

	public void PopulateValues(DataSet ds)
	{
		if (ds == null || ds.Tables.Count <= 0)
		{
			return;
		}
		foreach (DataRow row in ds.Tables[0].Rows)
		{
			ParamArrayInfo paramArrayInfo = new ParamArrayInfo();
			paramArrayInfo.ParamRID = row[Constants.PARAMRID].ToString();
			paramArrayInfo.ArrayName = row[Constants.ARRAYNAME].ToString().Trim();
			paramArrayInfo.LL = row[Constants.LOWERVALUE].ToString();
			paramArrayInfo.UL = row[Constants.UPPERVALUE].ToString();
			paramArrayInfo.ParamIndex = row[Constants.PARAMINDEX].ToString();
			paramArrayInfo.ColumnIndex = row[Constants.ARRAYCOLUMNINDEX].ToString();
			if (!hashParamArrayInfo.ContainsKey(paramArrayInfo))
			{
				hashParamArrayInfo.Add(paramArrayInfo, row[Constants.PARAMARRAYRID].ToString());
			}
		}
	}

	public static void ClearLookupValues()
	{
		hashParamArrayInfo.Clear();
		hashParamArrayInfo = new Dictionary<ParamArrayInfo, string>();
	}

	public void ClearNewValues()
	{
		NewParamArrayRIDLookup.Clear();
	}

	public string GetNewValues()
	{
		StringWriter stringWriter = new StringWriter();
		try
		{
			stringWriter.NewLine = string.Empty;
			XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
			xmlWriterSettings.OmitXmlDeclaration = true;
			using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
			{
				xmlWriter.WriteStartDocument();
				xmlWriter.WriteStartElement(Constants.ROOT_ELEMENTNAME);
				foreach (KeyValuePair<ParamArrayInfo, string> item in NewParamArrayRIDLookup)
				{
					xmlWriter.WriteStartElement("ParameterCls");
					xmlWriter.WriteStartElement("Name");
					xmlWriter.WriteString(item.Key.ArrayName);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("ParamRid");
					xmlWriter.WriteString(item.Key.ParamRID);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("ParamIndex");
					xmlWriter.WriteString(item.Key.ParamIndex);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("ColumnIndex");
					xmlWriter.WriteString(item.Key.ColumnIndex);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("LowerLimit");
					xmlWriter.WriteString(item.Key.LL);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("UpperLimit");
					xmlWriter.WriteString(item.Key.UL);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteEndElement();
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			return stringWriter.ToString();
		}
		finally
		{
			stringWriter.Close();
		}
	}

	public bool LookupHasValues()
	{
		return hashParamArrayInfo.Count > 0;
	}

	public int NewValueCount()
	{
		return NewParamArrayRIDLookup.Count;
	}
}
