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

namespace XmlForDB;

public class ScenarioSplitOptionsRID
{
	protected static Dictionary<ScenarioSplitOptionsInfo, string> scenarioSplitOptionsInfo = new Dictionary<ScenarioSplitOptionsInfo, string>(new ScenarioSplitOptionsInfoComparer());

	protected static Dictionary<ScenarioSplitOptionsInfo, string> NewScenarioSplitOptionsLookup = new Dictionary<ScenarioSplitOptionsInfo, string>(new ScenarioSplitOptionsInfoComparer());

	public void AddToNewLookUpList(string titleName, string titleValue)
	{
		ScenarioSplitOptionsInfo scenarioSplitOptionsInfo = new ScenarioSplitOptionsInfo();
		scenarioSplitOptionsInfo.TitleName = titleName.Trim();
		scenarioSplitOptionsInfo.TitleValue = titleValue.Trim();
		if (!NewScenarioSplitOptionsLookup.ContainsKey(scenarioSplitOptionsInfo))
		{
			NewScenarioSplitOptionsLookup.Add(scenarioSplitOptionsInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string titleName, string titleValue)
	{
		ScenarioSplitOptionsInfo scenarioSplitOptionsInfo = new ScenarioSplitOptionsInfo();
		scenarioSplitOptionsInfo.TitleName = titleName.Trim();
		scenarioSplitOptionsInfo.TitleValue = titleValue.Trim();
		string value = string.Empty;
		if (ScenarioSplitOptionsRID.scenarioSplitOptionsInfo.TryGetValue(scenarioSplitOptionsInfo, 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)
		{
			ScenarioSplitOptionsInfo scenarioSplitOptionsInfo = new ScenarioSplitOptionsInfo();
			scenarioSplitOptionsInfo.TitleName = row[Constants.TITLENAME].ToString().Trim();
			scenarioSplitOptionsInfo.TitleValue = row[Constants.TITLEVALUE].ToString().Trim();
			if (!ScenarioSplitOptionsRID.scenarioSplitOptionsInfo.ContainsKey(scenarioSplitOptionsInfo))
			{
				ScenarioSplitOptionsRID.scenarioSplitOptionsInfo.Add(scenarioSplitOptionsInfo, row["ScenarioSplitOptionsRID"].ToString());
			}
		}
	}

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

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

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

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