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

namespace XmlForDB;

public class ParamDetailsRID
{
	protected static Dictionary<ParamDetailsInfo, long> paramDetailsInfo = new Dictionary<ParamDetailsInfo, long>(new ParamDetailsInfoComparer());

	protected static Dictionary<ParamDetailsInfo, string> NewParamDetailsLookup = new Dictionary<ParamDetailsInfo, string>(new ParamDetailsInfoComparer());

	public void AddToNewLookUpList(string nodeDetailsRID, string parmRID, string paramAttributesRID)
	{
		ParamDetailsInfo paramDetailsInfo = new ParamDetailsInfo();
		paramDetailsInfo.NodeDetailsRID = nodeDetailsRID.Trim();
		paramDetailsInfo.ParamRID = parmRID.Trim();
		paramDetailsInfo.ParamAttributesRID = paramAttributesRID.Trim();
		if (!NewParamDetailsLookup.ContainsKey(paramDetailsInfo))
		{
			NewParamDetailsLookup.Add(paramDetailsInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string nodeDetailsRID, string parmRID, string paramAttributesRID)
	{
		ParamDetailsInfo paramDetailsInfo = new ParamDetailsInfo();
		paramDetailsInfo.NodeDetailsRID = nodeDetailsRID.Trim();
		paramDetailsInfo.ParamRID = parmRID.Trim();
		paramDetailsInfo.ParamAttributesRID = paramAttributesRID.Trim();
		long value = 0L;
		if (ParamDetailsRID.paramDetailsInfo.TryGetValue(paramDetailsInfo, out value))
		{
			return value.ToString();
		}
		return string.Empty;
	}

	public void PopulateValues(DataSet ds)
	{
		if (ds == null || ds.Tables.Count <= 0)
		{
			return;
		}
		foreach (DataRow row in ds.Tables[0].Rows)
		{
			ParamDetailsInfo paramDetailsInfo = new ParamDetailsInfo();
			paramDetailsInfo.NodeDetailsRID = row[Constants.NODEDETAILSRID].ToString().Trim();
			paramDetailsInfo.ParamRID = row[Constants.PARAMRID].ToString().Trim();
			paramDetailsInfo.ParamAttributesRID = row[Constants.PARAMATTRIBUTESRID].ToString().Trim();
			if (!ParamDetailsRID.paramDetailsInfo.ContainsKey(paramDetailsInfo))
			{
				long result = 0L;
				long.TryParse(row[Constants.PARAMDETAILSRID].ToString(), out result);
				ParamDetailsRID.paramDetailsInfo.Add(paramDetailsInfo, result);
			}
		}
	}

	public static void ClearLookupValues()
	{
		paramDetailsInfo.Clear();
		paramDetailsInfo = new Dictionary<ParamDetailsInfo, long>();
	}

	public void ClearNewValues()
	{
		NewParamDetailsLookup.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<ParamDetailsInfo, string> item in NewParamDetailsLookup)
				{
					xmlWriter.WriteStartElement(Constants.NEWVALUE_ELEMENTNAME);
					if (item.Key.ParamAttributesRID == string.Empty)
					{
						xmlWriter.WriteStartElement("NodeDetailsRID");
						xmlWriter.WriteString(item.Key.NodeDetailsRID);
						xmlWriter.WriteEndElement();
						xmlWriter.WriteStartElement("ParamRID");
						xmlWriter.WriteString(item.Key.ParamRID);
						xmlWriter.WriteEndElement();
					}
					else
					{
						xmlWriter.WriteStartElement("NodeDetailsRID");
						xmlWriter.WriteString(item.Key.NodeDetailsRID);
						xmlWriter.WriteEndElement();
						xmlWriter.WriteStartElement("ParamRID");
						xmlWriter.WriteString(item.Key.ParamRID);
						xmlWriter.WriteEndElement();
						xmlWriter.WriteStartElement("ParamAttributesRID");
						xmlWriter.WriteString(item.Key.ParamAttributesRID);
						xmlWriter.WriteEndElement();
					}
					xmlWriter.WriteEndElement();
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			return stringWriter.ToString();
		}
		finally
		{
			stringWriter.Close();
		}
	}

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

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