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

namespace XmlForDB;

public class ParamRID
{
	protected static Dictionary<ParamInfo, string> hashParamInfo = new Dictionary<ParamInfo, string>(new ParamInfoComparer());

	protected static Dictionary<ParamInfo, string> NewParamRIDLookup = new Dictionary<ParamInfo, string>(new ParamInfoComparer());

	public void AddToNewLookUpList(string nodeRID, string paramName, string lowerLimit, string upperLimit)
	{
		ParamInfo paramInfo = new ParamInfo();
		paramInfo.LL = lowerLimit.Trim();
		paramInfo.UL = upperLimit.Trim();
		paramInfo.NodeRID = nodeRID.Trim();
		paramInfo.ParamName = paramName.Trim();
		if (!NewParamRIDLookup.ContainsKey(paramInfo))
		{
			NewParamRIDLookup.Add(paramInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string nodeRID, string paramName, string lowerValue, string upperValue)
	{
		ParamInfo paramInfo = new ParamInfo();
		paramInfo.NodeRID = nodeRID.Trim();
		paramInfo.ParamName = paramName.Trim();
		paramInfo.LL = lowerValue.Trim();
		paramInfo.UL = upperValue.Trim();
		string value = string.Empty;
		if (hashParamInfo.TryGetValue(paramInfo, out value))
		{
			return value;
		}
		return string.Empty;
	}

	public bool CheckIfKeyExists(ParamInfo param)
	{
		return hashParamInfo.ContainsKey(param);
	}

	public void PopulateValues(DataSet ds)
	{
		if (ds == null || ds.Tables.Count <= 0)
		{
			return;
		}
		foreach (DataRow row in ds.Tables[0].Rows)
		{
			ParamInfo paramInfo = new ParamInfo();
			paramInfo.NodeRID = row[Constants.NODERID].ToString();
			paramInfo.ParamName = row[Constants.PARAMNAME].ToString().Trim();
			paramInfo.LL = row[Constants.LOWERVALUE].ToString();
			paramInfo.UL = row[Constants.UPPERVALUE].ToString();
			if (!hashParamInfo.ContainsKey(paramInfo))
			{
				hashParamInfo.Add(paramInfo, row[Constants.PARAMRID].ToString());
			}
		}
	}

	public static void ClearLookupValues()
	{
		hashParamInfo.Clear();
	}

	public void ClearNewValues()
	{
		NewParamRIDLookup.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<ParamInfo, string> item in NewParamRIDLookup)
				{
					xmlWriter.WriteStartElement("ParameterCls");
					xmlWriter.WriteStartElement("Name");
					xmlWriter.WriteString(item.Key.ParamName);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("NodeRID");
					xmlWriter.WriteString(item.Key.NodeRID);
					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 hashParamInfo.Count > 0;
	}

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