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

namespace XmlForDB;

public class RunTitleOptonsRID
{
	protected static Dictionary<RunTitleInfo, string> runTitleInfo = new Dictionary<RunTitleInfo, string>(new RunTitleInfoComparer());

	protected static Dictionary<RunTitleInfo, string> NewRuntitleLookup = new Dictionary<RunTitleInfo, string>(new RunTitleInfoComparer());

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

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

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

	public static void ClearNewValues()
	{
		NewRuntitleLookup.Clear();
		NewRuntitleLookup = new Dictionary<RunTitleInfo, 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<RunTitleInfo, string> item in NewRuntitleLookup)
				{
					xmlWriter.WriteStartElement(Constants.NEWVALUE_ELEMENTNAME);
					xmlWriter.WriteStartElement("RunTitleName");
					xmlWriter.WriteString(item.Key.TitleName);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("RunTitleValue");
					xmlWriter.WriteString(item.Key.TitleValue);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteEndElement();
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			return stringWriter.ToString();
		}
		finally
		{
			stringWriter.Close();
		}
	}

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

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