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

namespace XmlForDB;

public class ErrorCodeOptonsRID
{
	protected static Dictionary<ErrorCodeWithSubErrorCodeRIDInfo, string> errorCodeInfo = new Dictionary<ErrorCodeWithSubErrorCodeRIDInfo, string>(new ErrorCodeInfoComparer());

	protected static Dictionary<ErrorCodeWithSubErrorCodeRIDInfo, string> NewErrorCodeLookup = new Dictionary<ErrorCodeWithSubErrorCodeRIDInfo, string>(new ErrorCodeInfoComparer());

	public void AddToNewLookUpList(string mainErrorCode, string subErrorCodeCombinedOptionsRID)
	{
		ErrorCodeWithSubErrorCodeRIDInfo errorCodeWithSubErrorCodeRIDInfo = new ErrorCodeWithSubErrorCodeRIDInfo();
		errorCodeWithSubErrorCodeRIDInfo.MainErrorCode = mainErrorCode.Trim();
		errorCodeWithSubErrorCodeRIDInfo.SubErrorCodeCombinedOptionsRID = subErrorCodeCombinedOptionsRID.Trim();
		if (!NewErrorCodeLookup.ContainsKey(errorCodeWithSubErrorCodeRIDInfo))
		{
			NewErrorCodeLookup.Add(errorCodeWithSubErrorCodeRIDInfo, Constants.NEWEMPTYVALUE);
		}
	}

	public string GetIDFromLookup(string mainErrorCode, string subErrorCodeCombinedOptionsRID)
	{
		ErrorCodeWithSubErrorCodeRIDInfo errorCodeWithSubErrorCodeRIDInfo = new ErrorCodeWithSubErrorCodeRIDInfo();
		errorCodeWithSubErrorCodeRIDInfo.MainErrorCode = mainErrorCode.Trim();
		errorCodeWithSubErrorCodeRIDInfo.SubErrorCodeCombinedOptionsRID = subErrorCodeCombinedOptionsRID.Trim();
		string value = string.Empty;
		if (errorCodeInfo.TryGetValue(errorCodeWithSubErrorCodeRIDInfo, 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)
		{
			ErrorCodeWithSubErrorCodeRIDInfo errorCodeWithSubErrorCodeRIDInfo = new ErrorCodeWithSubErrorCodeRIDInfo();
			errorCodeWithSubErrorCodeRIDInfo.MainErrorCode = row[Constants.MAINERROR_CODE].ToString().Trim();
			errorCodeWithSubErrorCodeRIDInfo.SubErrorCodeCombinedOptionsRID = row[Constants.SUBERRORCODE_COMBINED_OPTIONSRID].ToString().Trim();
			if (!errorCodeInfo.ContainsKey(errorCodeWithSubErrorCodeRIDInfo))
			{
				errorCodeInfo.Add(errorCodeWithSubErrorCodeRIDInfo, row[Constants.ERRORCODE_OPTIONSRID].ToString());
			}
		}
	}

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

	public void ClearNewValues()
	{
		NewErrorCodeLookup.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<ErrorCodeWithSubErrorCodeRIDInfo, string> item in NewErrorCodeLookup)
				{
					xmlWriter.WriteStartElement(Constants.NEWVALUE_ELEMENTNAME);
					xmlWriter.WriteStartElement("ErrorCode");
					xmlWriter.WriteString(item.Key.MainErrorCode);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteStartElement("SubErrorCodeCombinedOptionsRID");
					xmlWriter.WriteString(item.Key.SubErrorCodeCombinedOptionsRID);
					xmlWriter.WriteEndElement();
					xmlWriter.WriteEndElement();
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			return stringWriter.ToString();
		}
		finally
		{
			stringWriter.Close();
		}
	}

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

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