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

namespace XmlForDB;

public class NodeDetailsRID
{
	protected static Dictionary<NodeDetailsInfo, string> nodeDetailsInfo = new Dictionary<NodeDetailsInfo, string>(new NodeDetailsInfoComparer());

	protected static Dictionary<NodeDetailsInfo, string> NewNodeDetailsLookup = new Dictionary<NodeDetailsInfo, string>(new NodeDetailsInfoComparer());

	public void AddToNewLookUpList(string nodeRID, string scenarioCombinedOptionsRID)
	{
		NodeDetailsInfo nodeDetailsInfo = new NodeDetailsInfo();
		nodeDetailsInfo.NodeRID = nodeRID.Trim();
		nodeDetailsInfo.ScenarioCombinedOptionsRID = scenarioCombinedOptionsRID.Trim();
		if (!NewNodeDetailsLookup.ContainsKey(nodeDetailsInfo))
		{
			NewNodeDetailsLookup.Add(nodeDetailsInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string nodeRID, string scenarioCombinedOptionsRID)
	{
		NodeDetailsInfo nodeDetailsInfo = new NodeDetailsInfo();
		nodeDetailsInfo.NodeRID = nodeRID.Trim();
		nodeDetailsInfo.ScenarioCombinedOptionsRID = scenarioCombinedOptionsRID.Trim();
		string value = string.Empty;
		if (NodeDetailsRID.nodeDetailsInfo.TryGetValue(nodeDetailsInfo, out value))
		{
			return value;
		}
		return string.Empty;
	}

	public void PopulateValues(DataSet ds)
	{
		if (ds == null || ds.Tables.Count <= 0)
		{
			return;
		}
		foreach (DataRow row in ds.Tables[0].Rows)
		{
			NodeDetailsInfo nodeDetailsInfo = new NodeDetailsInfo();
			nodeDetailsInfo.NodeRID = row[Constants.NODERID].ToString().Trim();
			nodeDetailsInfo.ScenarioCombinedOptionsRID = row[Constants.SCENARIOCOMBINEDOPTIONSRID].ToString().Trim();
			if (!NodeDetailsRID.nodeDetailsInfo.ContainsKey(nodeDetailsInfo))
			{
				NodeDetailsRID.nodeDetailsInfo.Add(nodeDetailsInfo, row[Constants.NODEDETAILSRID].ToString());
			}
		}
	}

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

	public static void ClearNewValues()
	{
		NewNodeDetailsLookup.Clear();
		NewNodeDetailsLookup = new Dictionary<NodeDetailsInfo, string>();
	}

	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<NodeDetailsInfo, string> item in NewNodeDetailsLookup)
				{
					xmlWriter.WriteStartElement(Constants.NEWVALUE_ELEMENTNAME);
					if (item.Key.ScenarioCombinedOptionsRID == string.Empty)
					{
						xmlWriter.WriteStartElement("NodeRID");
						xmlWriter.WriteString(item.Key.NodeRID);
						xmlWriter.WriteEndElement();
					}
					else
					{
						xmlWriter.WriteStartElement("NodeRID");
						xmlWriter.WriteString(item.Key.NodeRID);
						xmlWriter.WriteEndElement();
						xmlWriter.WriteStartElement("ScenarioCombinedOptionsRID");
						xmlWriter.WriteString(item.Key.ScenarioCombinedOptionsRID);
						xmlWriter.WriteEndElement();
					}
					xmlWriter.WriteEndElement();
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			return stringWriter.ToString();
		}
		finally
		{
			stringWriter.Close();
		}
	}

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

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