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

namespace XmlForDB;

public class ScenarioSplitConditionsRID
{
	protected static Dictionary<ScenarioSplitConditionsInfo, string> scenarioSplitConditionsInfo = new Dictionary<ScenarioSplitConditionsInfo, string>(new ScenarioSplitConditionsInfoComparer());

	protected static Dictionary<ScenarioSplitConditionsInfo, string> NewScenarioSplitConditionsLookup = new Dictionary<ScenarioSplitConditionsInfo, string>(new ScenarioSplitConditionsInfoComparer());

	public void AddToNewLookUpList(string nodeDetailsRID, string scenarioSplitOptionsRID)
	{
		ScenarioSplitConditionsInfo scenarioSplitConditionsInfo = new ScenarioSplitConditionsInfo();
		scenarioSplitConditionsInfo.NodeDetailsRID = nodeDetailsRID.Trim();
		scenarioSplitConditionsInfo.ScenarioSplitOptionsRID = scenarioSplitOptionsRID.Trim();
		if (!NewScenarioSplitConditionsLookup.ContainsKey(scenarioSplitConditionsInfo))
		{
			NewScenarioSplitConditionsLookup.Add(scenarioSplitConditionsInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string nodeDetailsRID, string scenarioSplitOptionsRID)
	{
		ScenarioSplitConditionsInfo scenarioSplitConditionsInfo = new ScenarioSplitConditionsInfo();
		scenarioSplitConditionsInfo.NodeDetailsRID = nodeDetailsRID.Trim();
		scenarioSplitConditionsInfo.ScenarioSplitOptionsRID = scenarioSplitOptionsRID.Trim();
		string value = string.Empty;
		if (ScenarioSplitConditionsRID.scenarioSplitConditionsInfo.TryGetValue(scenarioSplitConditionsInfo, 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)
		{
			ScenarioSplitConditionsInfo scenarioSplitConditionsInfo = new ScenarioSplitConditionsInfo();
			scenarioSplitConditionsInfo.NodeDetailsRID = row[Constants.NODEDETAILSRID].ToString().Trim();
			scenarioSplitConditionsInfo.ScenarioSplitOptionsRID = row[Constants.SCENARIOSPLITOPTIONSRID].ToString().Trim();
			if (!ScenarioSplitConditionsRID.scenarioSplitConditionsInfo.ContainsKey(scenarioSplitConditionsInfo))
			{
				ScenarioSplitConditionsRID.scenarioSplitConditionsInfo.Add(scenarioSplitConditionsInfo, row[Constants.SCENARIOSPLITCONDITIONSRID].ToString());
			}
		}
	}

	public void ClearLookupValues()
	{
		scenarioSplitConditionsInfo.Clear();
	}

	public void ClearNewValues()
	{
		NewScenarioSplitConditionsLookup.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<ScenarioSplitConditionsInfo, string> item in NewScenarioSplitConditionsLookup)
				{
					xmlWriter.WriteStartElement(Constants.NEWVALUE_ELEMENTNAME);
					xmlWriter.WriteStartElement("NodeDetailsRID");
					xmlWriter.WriteString(item.Key.NodeDetailsRID);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("ScenarioSplitOptionsRID");
					xmlWriter.WriteString(item.Key.ScenarioSplitOptionsRID);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteEndElement();
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			return stringWriter.ToString();
		}
		finally
		{
			stringWriter.Close();
		}
	}

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

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